Re: Height and location of text within a line in NSLayoutManager ignoring spacing?
Re: Height and location of text within a line in NSLayoutManager ignoring spacing?
- Subject: Re: Height and location of text within a line in NSLayoutManager ignoring spacing?
- From: Keith Blount <email@hidden>
- Date: Tue, 19 Feb 2008 05:01:43 -0800 (PST)
Thanks again.
I can now successfully draw an arbitrary character on the baseline of a line in the layout manager in -drawGlyphsForGlyphRange:atPoint: as follows:
// First get the bottom left location for the character (the baseline within the line rect).
NSPoint location = [self locationForGlyphAtIndex:index];
NSPoint lineRect = [self lineFragmentGlyphRectForGlyphAtIndex:index effectiveRange:NULL];
NSPoint baseLoc = containerOrigin;
baseLoc.x += location.x + lineRect.origin.x;
baseLoc.y += location.y + lineRect.origin.y - [invisiblesFont descender]; // Account for the descender of the string to draw!
// Get the height of the string we are going to draw
NSRect invisibleBounds = [invisibleCharString boundingRectWithSize:NSMakeSize(1e7,100)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:invisibleAttribs];
NSRect drawingRect = NSMakeRect(baseLoc.x, baseLoc.y, invisibleBounds.size.width, invisibleBounds.size.height);
// Draw the string
[invisibleCharString drawWithRect:drawingRect options:NSStringDrawingUsesLineFragmentOrigin attributes:invisibleAttribs];
The above works fine (it seems one pixel out, but I can live with that). Except that what I really want is to centre the string that I am drawing vertically against the X-height of the line. I could get the attributes of the character at the current index, grab the font and call -xHeight, of course, but in an attributed string there could be different fonts within the line which would mean the invisibles being drawn at different vertical locations which wouldn't be too pretty.
So, what I really want, it seems, is the largest X-height of the whole line, so that I can centre my arbitrary control characters against that value. Is there any way to get this without having to cycle through all of the characters in the line, get the font for each, check the X-height and save the maximum value? This option doesn't seem so good for a method that gets constantly during user typing. (I suppose I could go through using -attribute:atIndex:effectiveRange: to get font runs, but even that doesn't sound too efficient.)
Many thanks and all the best,
Keith
----- Original Message ----
From: Douglas Davidson <email@hidden>
To: Keith Blount <email@hidden>
Cc: email@hidden
Sent: Monday, February 18, 2008 8:53:38 PM
Subject: Re: Height and location of text within a line in NSLayoutManager ignoring spacing?
On
Feb
18,
2008,
at
11:53
AM,
Keith
Blount
wrote:
>
Many
thanks
for
your
reply,
much
appreciated.
The
trouble
is
that
I
>
don't
just
need
the
measurements
of
the
text
that
is
laid
out
in
the
>
text
container,
but
also
of
the
arbitrary
control
string
that
I
am
>
going
to
draw
behind
the
text,
using
NSString's
-
>
drawWithAttributes:.
Would
I
have
to
set
up
a
separate
layout
>
manager
to
calculate
all
of
the
measurements
for
the
control
glyphs?
>
(I
suppose
I
could
cache
that
information
given
that
the
control
>
glyph
attributes
and
thus
sizes
won't
change.)
That
sounds
like
a
reasonable
plan,
if
it
really
is
an
arbitrary
text
string.
Another
option
might
be
to
use
drawWithRect:options:attributes:,
which
can
do
baseline-relative
drawing.
Douglas
Davidson
____________________________________________________________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden