propstm
propstm

Reputation: 3629

How should I determine the height of an NSAttributedString for a fixed width

I have an attributed string with "Date: Heading - Description."
The Date, Heading, and Description text values are dynamically generated, and all have different attributed fonts which I've assigned to the appropriate substring.

How should I determine the total width, and then consequentially the determine the height for a given width which this content should fill?

I've found this SO post but it is from 2010 and a more current solution seems like it would be available.

Additionally, how should the width of an attributed string with content changing text style by substring be determined? The iOS7 sizeWithAttributes method seems to apply the attributes for the entire string.

Upvotes: 0

Views: 158

Answers (1)

matt
matt

Reputation: 534885

This is an attributed string, so the place to look is the NSAttributedString documentation. In particular:

https://developer.apple.com/library/ios/documentation/uikit/reference/NSAttributedString_UIKit_Additions/Reference/Reference.html#//apple_ref/occ/instm/NSAttributedString/boundingRectWithSize:options:context:

As to your last paragraph: simply make this a mutable attributed string, change the styles as desired, and proceed as above.

Upvotes: 1

Related Questions