Search code examples
objective-cnsstringnsattributedstringuifont

Getting Font Size of NSAttributed string


I'm trying to get the font size of an NSAttributedString, however, I'm having trouble doing this. How do you get the font size of an NSAttributedString in objective-c?


Solution

  • An attributed string does not have "a font size". The used font is an attribute, which can vary over the String. Look at your Q: There are two different fonts in one paragraph. (One for the usual text and one for the keywords.)

    Therefore you can only ask for the existing attributes (including font) at a specific location. I. e. - attributesAtIndex:effectiveRange: does this job for you. The attribute key for the font is NSFontAttributeName. If yo do not find this key in the attributes dictionary, it is Helvetica(Neue), 12 pt.