How to find a height of attributedString that contains multiple fonts in iOS?


How to find a height of attributedString that contains multiple fonts in iOS?



Assume you have an NSAttributedString that contains some random number of different fonts. For normal text, you can use


NSAttributedString


boundingRect(
with size: CGSize,
options: NSStringDrawingOptions = ,
attributes: [NSAttributedStringKey : Any]? = nil,
context: NSStringDrawingContext?
)



to calculate its height.



But how can someone calculate an NSAttributedString containing multiple fonts? For example, location 0 to 5 has UIFont.systemFont(with: 15) and 6 to 10 has UIFont.systemFont(with: 17) and 11 to 16 has UIFont.systemFont(with: 5).


NSAttributedString


UIFont.systemFont(with: 15)


UIFont.systemFont(with: 17)


UIFont.systemFont(with: 5)



Since boundingRect method does not recognize which attributes apply to at what range, it cannot be used. What else can I do to achieve this?


boundingRect





NSAttributedString has boundingRect function itself. Did you try it? developer.apple.com/documentation/foundation/nsattributedstring/…
– Quoc Nguyen
Jun 29 at 6:57


boundingRect





for what do you need to calc the height? Maybe there is another way to make it work.
– zero3nna
Jun 29 at 8:01










By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV