Text vertical spacing - Yet even more text woes
Text vertical spacing - Yet even more text woes
- Subject: Text vertical spacing - Yet even more text woes
- From: Robert Clair <email@hidden>
- Date: Mon, 13 Dec 2004 10:53:45 -0500
I'm working on something that is intended to do display (as opposed to
body) text -
posters and graphic arts, not word processing. I'd like to control the
vertical spacing
between lines (of often mixed font) text.
I think the controls with the text ruler are very unfriendly for this -
I think a non-technical
user would want something more like: 1) select some lines 2) move
slider to move them
closer or further apart.
NSMutableParagraphStyle has setLineSpacing: to control the leading -
extra space
added between lines. But there's a problem: it's supposed to be
non-negative. Once, in
high school, I actually set some real metal type for a letterpress so I
understand that a physical
piece of lead can't have a negative height, but why would a computer
care ? So I
tried it. It works just fine with negative numbers - the space between
the lines shrinks and
everything is in the correct place... except...
For other reasons I need to draw my own text highlighting. I do this by
breaking the selected
text into lines and getting the rectangles from
[NSLayoutManager boundingRectForGlyphRange:inTextContainer:]
when you setLineSpacing: the rectangle returned is fine vertically, but
it is wrong *horizontally* -
there is a (font dependent) extra amount on either side - on the order
of half a character or so
for some fonts. (The left is too far to the left and the right is too
far to
the right.)
I know all the caveats about doing undocumented things(it's not nice
to fool Mother Nature, etc),
but does anyone out there know what the extra amounts might be so I can
calculate and remove them ????
Two reasons:
1) I really need this to work and the alternative is both ugly and has
its own bug (see below).
2) I'm intellectually curious as to why a vertical dimension would mess
up the horizontal part.
A more "official" way: I could go through each line, calculate the
"natural" line height for
each line, add my offset and use the result with
NSMutableParagraphStyle setMinimumLineHeight:
and setMaximumLineHeight to pin the line height. But this requires
going through the line
each time to figure out the natural line height considering all the
fonts present and also
adding my own attribute to remember what the correction is - to update
my efficiently
UI if the selection changes.
Not only is this ugly - it has a serious bug: I move the textview's
origin as the line height of the first line
changes so that the baseline of the first line stays where the user
clicked. I get the offset
from
NSRect lineRect =
[[self layoutManager] boundingRectForGlyphRange:
[self getFirstLineRange]
inTextContainer: [self
textContainer]];
if ( [[self textStorage] length] == 0 )
{
NSFont* font = [self myRoutineThatKnowsTheCurrentFont];
yOffset = ( [font defaultLineHeightForFont] + [font descender]);
}
else
{
float baseline = [[[self layoutManager] typesetter]
baselineOffsetInLayoutManager:[self
layoutManager]
glyphIndex:0];
yOffset = lineRect.origin.y + lineRect.size.height - baseline;
}
When setMinimumLineHeight: is used to set the height above the natural
height,
there is a one time jump in the returned rectangle's height - so my
offset is too big and
the text on the screen hops up.
Again, this is a one time effect as you cross the threshold, it doesn't
get bigger as the line height
gets bigger.
Any help very much appreciated - this is driving me nuts.
......Bob Clair
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden