I'm trying to get to grips with non-trivial string drawing for the
I have a restricted area in which to render a short multi-word string
(which I'd like it justified vertically in the middle of the bounding
area with any required line wrapping done according to the word
wrapping rule I have set up in the paragraph style attributes).
A perusal of the docs suggest the following approaches:
1. NSString's -boundingRectWithSize:options:attributes, setting the
constraining area, followed by a justification of the returned
rectangle within this area according to taste (i.e. to get my
vertically centered position). Then, naturally, NSString's drawInRect
to actually render the string.
2. Using NSLayoutManager (per the "Calculating Text Height" section of
the Text Layout Programming Guide) to get the
'usedRectForTextContainer', then proceed with justification of the
returned rectangle and drawing as (1).
My initial efforts have been focussed on approach (1) as, at face
value at least, the documentation and the API seem to suggest it does
what I need. However, try as I might (so far) I cannot get this to
work. It appears that this method ought to differ from the simpler -
sizeWithAttributes by taking into consideration the provided size as
constraints for rendering the text, and then returning the bounds that
actual text would have if rendered with all the attributes/typesetting
rules. Indeed, I would have expected -boundingRectWithSize to have
been implemented in terms of the much more verbose method of (2).
Instead, method (1) appears to always result in a single line of text
that violates the horizontal bounds of the size that I set - despite
there clearly being opportunity in the text I'm supplying to break
lines with the default line breaking style to avoid this. I have
tried asserting my own paragraph style with appropriate values, but
nothing seems to get -boundingRectWithSize to consider breaking the
text into multiple lines.
Experimentation indicates that -boundingRectWithSize does something I
didn't expect: notably returning a Y-negative origin. This leads me
to suspect that it is intended to do something entirely different from
the determination of the bounding rectangle of some text within
constrained space. However, it's not at all clear from the
documentation what this function actual is.
So, while I'm about to go off and write my sizing code in terms of
method (2), I'm curious what -boundingRectWithSize is _supposed_ to
do, and there remains a nagging doubt that it would do what I want if
I was driving it properly.
Can anyone enlighten me to the simplest/right way to size the area of
text that will be word-wrapped? And assuming -boundingRectWithSize is
not intended to handle text that will be automatically set into
multiple lines, what it is _supposed_ to be doing that -
sizeWithAttributes does not? Perhaps it is just for the determination
of base-line positioned rendering of a single line of text?
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