Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UIFont to CTFontRef and add italic on Retina display

I have a custom subclass of UILabel which makes custom drawing using CoreText. I used to draw my strings using the UIFont that is set on the label accessing it using the font property. I also add some traits to the font. Here is what it looks like:

UIFont* font = [self font];
CTFontRef ref = CTFontCreateWithName((CFStringRef)[font name], [font pointSize], NULL);
CTFontRef italicFont = CTFontCreateCopyWithSymbolicTraits(ref, [font pointSize], NULL, kCTFontItalicTrait, kCTFontItalicTrait);

This used to work fine until the new ipad appeared with its retina display. When I perform the CTFontCreateCopyWithSymbolicTraits call, it produces a nil return value. I found out [font name] returns ".HelveticaNeueUI" on these new devices. The CTFontCreateWithName seems to work fine with this private font name, but CTFontCreateCopyWithSymbolicTraits doesn't. Yet if I create an italic font using [UIFont italicSystemFontOfSize:[font pointSize]] it creates an italic font for which [font name] still returns ".HelveticaNeueUI".

How should I convert my UIFont to CTFontRef so that I can still apply new traits to my CTFontRef on both retina and non-retina displays?

like image 862
Dalzhim Avatar asked Mar 19 '12 16:03

Dalzhim


People also ask

What is the use of systemfont in uifont?

class func systemFont(ofSize: CGFloat, weight: UIFont.Weight, width: UIFont.Width) -> UIFont Beta Returns the font object for standard interface items in boldface type in the specified size. Returns the font object for standard interface items in italic type in the specified size.

How to create an italic font to go with an upright?

How to create an italic font to go with an existing upright font. Start a new (renamed) font file based on upright If you created the original upright font in a font editor, then the source file you used is the ideal starting point. If not, or if it’s a format FontLab does not handle, you can start with a final-output format such as .otf or .ttf.

How do I change the angle of an italic font?

In the Font Info panel, or in the Font Info dialog in the Font Dimensions section, set the Italic Angle, and then reset the auto-calculated Caret Offset by clicking the button next to the Caret Offset. (That tells the app/OS how much to shift the cursor after slanting it, usually a negative number for an italic font.

How do I change the font in a UIKit project?

In order to use the new font in a UIKit project, set the label’s font to custom UIFont like that: You can find out what other fonts are already available on iOS to use in your app by running the following code which prints out all font names:


2 Answers

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);
like image 127
unexpectedvalue Avatar answered Oct 29 '22 17:10

unexpectedvalue


Converting UIFont/NSFont to CTFontRef is very simple if you use ARC.

UIFont:

UIFont *uiFont = [UIFont fontWithName:@"Helvetica" size:10];
CTFontRef ctFont = (__bridge CTFontRef)uiFont;

NSFont:

NSFont *nsFont = [NSFont fontWithName:@"Helvetica" size:10];
CTFontRef ctFont = (__bridge CTFontRef)nsFont;
like image 23
Slyv Avatar answered Oct 29 '22 17:10

Slyv