Re: more about NSAttributedString and truncation in iOS 6
Re: more about NSAttributedString and truncation in iOS 6
- Subject: Re: more about NSAttributedString and truncation in iOS 6
- From: Kyle Sluder <email@hidden>
- Date: Sat, 26 Jan 2013 09:45:03 -0800
On Jan 26, 2013, at 9:20 AM, Matt Neuburg <email@hidden> wrote:
> I suppose no one remembers this:
>
> http://www.cocoabuilder.com/archive/cocoa/323722-nsattributedstring-mysteriously-truncated-too-soon.html
>
> Anyhow, I have finally realized after all this time that what I was seeing here was in fact the _standard_ behavior of NSAttributedString under iOS 6. If an NSAttributedString's paragraph style includes a `lineBreakMode` of `NSLineBreakByTruncatingTail`, and if that string is going to wrap in the width within which it is being drawn, then only the _first_ line is drawn (truncated).
>
> The reason this confused me is that it isn't what I was expecting. It isn't what I was expecting because UILabel's truncation works (and always has worked) a different way: its `lineBreakMode` applied to a multiline string draws the _whole_ string, wrapping, as much as will fit in the full height of the label, and _then_ truncates the _last_ line.
>
> So what we have now is that NSParagraphStyle and UILabel both have a `lineBreakMode` that can have a value of `NSLineBreakByTruncatingTail`, but they implement it very differently. (That is why I ended up having to achieve the same end using the NSAttributedString drawing option `NSStringDrawingTruncatesLastVisibleLine`.) Just wanted to make that perfectly clear for generations to come... :) m.
The difference you're describing here sounds like the difference between line-fragment mode (passing NSStringDrawingUsesLineFragmentOrigin) and baseline-origin mode (not passing that option). As the docs state, baseline-origin mode cannot render more than a single line; subsequent lines will not be drawn, and the NSLineBreakByTruncating* lineBreakModes are all treated as NSLineBreakByClipping.
It sounds like UILabel passes (NSStringDrawingUsesLineFragmentOrigin | NSSTringDrawingTruncatesLastVisibleLine) and creates a paragraph style with whatever lineBreakMode value is set on itself.
It's fortuitous that you posted this to the list at the same time I happened to be working on a bug where I needed to optically center the contents of a string in a label. The only way I've found to do this is in single-line mode, because as my demo app shows NSStringDrawingUsesDeviceMetrics is annoyingly no-op in line-fragment mode.
Yet somehow NSTextFieldCell is able to draw its content vertically centered while still enabling truncation, which implies that it uses line-fragment mode. Perhaps NSTextFieldCell is satisfied with vertically centering the whole line fragment height rather than trying to actually center the pixels. One might be able to investigate this by setting a lineHeightMultiple on an attributed string value of a vertically-centering text field cell.
--Kyle Sluder
_______________________________________________
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