iPhone - Convert CTFont to UIFont?
Asked Answered
D

2

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
Dyeline answered 16/7, 2011 at 2:20 Comment(0)
A
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.

Apc answered 16/7, 2011 at 2:29 Comment(1)
This is wrong if your font doesn't appear in UIFont.familyNames presumably because it was loaded at runtime without GSFontAddFromFile.Monteith
C
0

With ARC:

UIFont *uiFont = [UIFont fontWithName:(__bridge NSString *)CTFontCopyPostScriptName(ctFont) size:CTFontGetSize(ctFont)];
Citrine answered 2/5, 2016 at 8:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.