5

I am trying to convert a CTFont to a UIFont without losing any of the styles and attributes such as:

  • Font Name
  • Font Size
  • Font color
  • Underlines
  • Bold
  • Italic
  • etc
aryaxt
  • 76,198
  • 92
  • 293
  • 442

2 Answers2

14
CTFontRef ctFont = ...;
NSString *fontName = [(NSString *)CTFontCopyName(ctFont, kCTFontPostScriptNameKey) autorelease];
CGFloat fontSize = CTFontGetSize(ctFont);
UIFont *font = [UIFont fontWithName:fontName size:fontSize];

Color and underline are not attributes of the font. Bold and italic are part of the font name.

omz
  • 53,243
  • 5
  • 129
  • 141
  • 2
    This is wrong if your font doesn't appear in UIFont.familyNames presumably because it was loaded at runtime without GSFontAddFromFile. – Texas Dec 31 '12 at 23:50
0

With ARC:

UIFont *uiFont = [UIFont fontWithName:(__bridge NSString *)CTFontCopyPostScriptName(ctFont) size:CTFontGetSize(ctFont)];
Cœur
  • 37,241
  • 25
  • 195
  • 267