iPhone - Convert CTFont to UIFont?

5.8k views Asked by At

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
2

There are 2 answers

1
omz On BEST ANSWER
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.

0
Cœur On

With ARC:

UIFont *uiFont = [UIFont fontWithName:(__bridge NSString *)CTFontCopyPostScriptName(ctFont) size:CTFontGetSize(ctFont)];