Posts

Showing posts with the label text

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?...