iPhone - Convert Ctfont to Uifont

iPhone - Convert CTFont to UIFont?

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.

How to convert CGFontRef to UIFont?

Conrad's answer is close but doesn't quite work. You need to provide UIFont with the PostScript name, rather than the full name.

NSString *fontName = (NSString *)CGFontCopyPostScriptName(fontRef);
UIFont *font = [UIFont fontWithName:fontName size:someSize]

Convert CATextLayer font to UIFont

I just wrote the following extension which should handle: CTFont, CGFont, string name or UIFont:

extension CATextLayer {
public var typeFace:UIFont? {
let name:String
switch font {
case let ctFont as CTFont :
// If you assign a UIFont to CATextLayer.font
// it is implicitly converted to CTFont,
// so this case will be called.
guard let _name = CTFontCopyName(ctFont, kCTFontPostScriptNameKey) else {
return nil
}
name = _name as NSString as String
case let cgFont as CGFont :
guard let _name = cgFont.postScriptName else {
return nil
}
name = _name as NSString as String
case let string as NSString :
name = string as String
case let string as String :
name = string
default:
return nil
}
return UIFont(name:name, size:fontSize)
}
}

On a side note, I found this comment in docs inaccurate:

In iOS, you cannot assign a UIFont object to this property.

From my tests I was able to set a UIFont on CATextLayer.font and it appears to work fine.


EDIT

It turns out when you assign a UIFont to CATextLayer.font it is converted to a CTFont behind the scenes, so in order to get the UIFont back out it's necessary to convert from CTFont to UIFont (which my code does). Simply casting to UIFont would not work.

Convert UIFont to CTFontRef and add italic on Retina display

A bit old but for whomever it might still concern:

That happens because the new Neue Helvetica system font used in new retina devices (iPhone 4, iPad) doesn't have an italic version. Not every font has a bold and/or italic variant.

So, if you need italic text, you need to use a different font, for example, the old Helvetica.

CTFontRef ref = CTFontCreateWithName((CFStringRef)@"Helvetica", 12, NULL);
CTFontRef italicFont = CTFontCreateCopyWithSymbolicTraits(ref, 12, NULL, kCTFontItalicTrait, kCTFontItalicTrait);

iPhone iOS UIFont conversion to CTFontRef causes size misalignment

1) You can't mix text drawn by UIKit with text drawn by CoreText. This is mentioned in the Apple docs (See Text, Web and Editing Programming for iOS in the section "Core Text and the UIKit Framework" ). (What appears to be going on is that UIKit does some rounding and takes some shortcuts to boost performance and also does not do kerning by default.)

2) Nope, you're not missing some property.

3) CoreText does not provide a simple means of truncating the text, you would have to write your own code to do it.

Why are UIFont and CGFont/CTFont separated completely?

The short answer : "Only Apple folks knows"

The more detailed answer, after digging (reversing) frameworks a bit:

  • UIKit.framework doesn't use at all CGFont type
  • UIKit.framework use instead CoreText CTFont and GSFont...
  • GSFont comes from... the GraphicsServices private framework
  • GraphicsServices... use CGFont

So here is my guess (Might be completely wrong) :

  • UIFont > GSFont(Private) > CGFont
  • UIKit > GraphicsServices > CoreGraphics :

    At least Concerning fonts, Apple engineers have set up an abstraction layer between UIKit
    and CoreGraphics frameworks, via the obscur GraphicsServices one.

  • iOS > iOS (Private) > Mac OSX :

    Why they did this, I would say CoreGraphics, which comes from Mac OSX, wasn't optimal for iOS devices, and UIKit being iOS only, they added a layer between both apis. Or more generally they plan one day to separate completely UIKit from CoreGraphics... iOS from Mac OSX... Who knows! The apple folks does, my short answer :)

About using custom fonts with UIKit, you might be interested reading this.

How to cast a UIFont object to CTFont in Swift

This has apparently been fixed in a later version of Swift and/or CoreText. At least it works now in my testing using Xcode 7.0.1.

SwiftUI/UIKit How to convert SwiftUI Font to UIKit UIFont?

If you need share same font, then use UIFont as model property and convert in SwiftUI where needed as, for example

Text("Some text")
.font(Font(uiFont as CTFont))

Loading a Custom Font to iOS dynamically

As of iOS 5.1, there is no public API that creates a UIFont from a CGFont or a CTFont. You are out of luck. :(

Open a feature request at http://bugreport.apple.com if you want them to add an API for this.

Converting UIFont to CGFont in swift?

Here is a small example:

var font = UIFont.systemFontOfSize(15.0)
var fontName = font.fontName as NSString
var cgFont = CGFontCreateWithFontName(fontName);

var copiedFont = CGFontCreateCopyWithVariations(cgFont, nil)


Related Topics



Leave a reply



Submit