Setting Up a Ruler View

Adding a ruler view to a scroll view can be as simple as invoking the NSScrollView method setHasHorizontalRuler: and setHasVerticalRuler: methods. These create instances of the default ruler view class, which you can change using the NSScrollView class method setRulerViewClass:. You can also set ruler views directly on a per-instance basis using setHorizontalRulerView: and setVerticalRulerView:. Once you’ve added rulers to a scroll view, you can hide and reveal them using setRulersVisible:.

Beyond creating the rulers, you need take only two steps to set them up properly for use by the views contained within the scroll view: locate the zero marks of the rulers and reserve room for accessory views. You normally perform these steps only once, when setting up the NSScrollView object with rulers. However, if you allow the user to reset document attributes such as margins, you should change the zero mark locations as well. Also, if you reuse the scroll view by swapping in a new document view you may need to set up the rulers again with different settings.

The first step is to determine where you want the zero marks of the rulers to be located relative to the bounds origin of the document view. The zero marks are coincident with the bounds origin by default, but you can change this with the method setOriginOffset:. This method takes an offset specified in the document view’s coordinate system. If you need to set the origin offset based on a point in a subview of the document view, such as a text view that’s inset on a page, use convertPoint:fromView: to realize it in the document view’s coordinate system. This Objective-C code fragment places the zero marks at the bounds origin of a client view, which lies somewhere inside the document view:

zero = [docView convertPoint:[clientView bounds].origin fromView:clientView];
[horizRuler setOriginOffset:zero.x - [docView bounds].origin.x];

After placing the zero marks, you should set up your rulers so that they don’t change in size as the user works within the document view. For example, if two different subviews of the document view use different accessory views, the ruler view enlarges itself as necessary each time you change the accessory view. Such changes are at best unsightly and at worst confusing to the user. To avoid this problem, calculate ahead of time the sizes of the largest accessory view and the largest markers, and set the ruler view’s required thickness for these elements using setReservedThicknessForAccessoryView: and setReservedThicknessForMarkers:. For example, if you have two accessory views for the horizontal ruler, one 16.0 PostScript units high and the other 24.0, invoke setReservedThicknessForAccessoryView: with an argument of 24.0.