NSAttributedString - get font attributes

adam picture adam · Nov 4, 2013 · Viewed 17.8k times · Source

I need to get info about my attributed string but can't figure out how. I'm getting this dictionary:

2013-11-04 18:06:10.628 App[1895:60b] {
    NSColor = "UIDeviceWhiteColorSpace 0.3 1";
    NSFont = "<UICTFont: 0x17d8d4c0> font-family: \".HelveticaNeueInterface-MediumP4\"; font-weight: bold; font-style: normal; font-size: 17.00pt";
    NSUnderline = 0;
}

It is easy to check the underline with:

[attrs objectForKey:@"NSUnderline"]

But how to get info about font like the font-style, font-weight etc.

Thanks for any help

Answer

rmaddy picture rmaddy · Nov 4, 2013

You get the font from:

UIFont *font = attrs[NSFontAttributeName];

The issue is determining whether it is bold or not. There is no property for that. The only option is to look at the fontName of the font and see if contains "Bold" or some other similar term. Not all bold fonts have "Bold" in the name.

The same issue applies for determining of the font is italic or note. You have to look at the fontName and look for things like "Italic" or "Oblique".