0

I know how to set font with using fontWithName: and how to set systemFont, both bold and italic. But how do I set a font with name if I want that to be italic?

myLabel.font = [UIFont fontWithName:@"Georgia" size:15]; 

or

myLabel.font = [UIFont italicSystemFontOfSize:15];

I want an italic Georgia, how?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Lars -
  • 491
  • 1
  • 10
  • 25
  • According to [comment](https://stackoverflow.com/questions/9052570/how-to-set-fontwithname-and-italic/36974547#comment11373039_9052690), Lars actually wants it Italic and Bold. – Cœur May 02 '16 at 03:29

5 Answers5

6

To get it in italic you will need to do something like:

[UIFont fontWithName:@"Georgia-Italic" size:15];

Or:

[UIFont fontWithName:@"GeorgiaItalic" size:15];

To find out which you would be best off using :

NSArray *fontArray = [UIFont fontNamesForFamilyName:@"Georgia"];

And then print this in the debugger to see which subtypes are available.

To use the system font there is:

[UIFont boldSystemFontOfSize:SIZE];

And:

[UIFont italicSystemFontOfSize:SIZE];

Hope this helps :)

Thomas Besnehard
  • 1,996
  • 3
  • 21
  • 46
George Green
  • 4,577
  • 5
  • 28
  • 45
  • 2
    Thanks, that helped a lot. What I really wanted was bold and italic so after some experimenting Georgia-BoldItalic worked. – Lars - Jan 30 '12 at 10:16
  • you should accept the answer, since it seems it has helped you –  Jan 30 '12 at 15:15
2

Don't try to play with the font names. With iOS7+, using the font descriptor you need no names:

// original font to modify
UIFont *font = [UIFont fontWithName:@"Georgia" size:15];
// adding bold and italic descriptor
UIFontDescriptor *fontDescriptor = [font.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic];
// making the new font; size:0 means 'keep the size as is'
myLabel.font = [UIFont fontWithDescriptor:fontDescriptor size:0];

source

With iOS3.2+, you can try CoreText:

// original font to modify
UIFont *font = [UIFont fontWithName:@"Georgia" size:15];
// CoreText font equivalent
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL);
// adding bold and italic
CTFontRef ctFont2 = CTFontCreateCopyWithSymbolicTraits(ctFont, 0.0, NULL, kCTFontBoldTrait | kCTFontItalicTrait, kCTFontBoldTrait | kCTFontItalicTrait);
CFRelease(ctFont);
// back to UIFont
UIFont *newfont = [UIFont fontWithName:CFBridgingRelease( CTFontCopyPostScriptName(ctFont2)) size:CTFontGetSize(ctFont2)];
CFRelease(ctFont2);
Community
  • 1
  • 1
Cœur
  • 32,421
  • 21
  • 173
  • 232
2

you must know the variant name, something like "GeorgiaIT" or "GeorgiaItalic".

  • I know this is really old, but http://iosfonts.com/ gives you a good rundown of available fonts and their variant names. – Andrew May 31 '13 at 08:52
1

Same problem here. I solved it this way, which will yield either (1) The Italic face you're looking for (assuming it's named ...-Italic), or (2) At least the base font so things (hopefully) don't crash. Incidentally, the whole NSRange thing is so that we ignore -ItalicBold, for example, although it's usually -BoldItalic, so we'd ignore it anyway. Cheers -W

UIFont *myBaseForItalicFont = [UIFont preferredFontForTextStyle:UIFontDescriptorTextStyleBody];
NSString *myFontFamilyName = myBaseForItalicFont.familyName;
NSArray *myFontNames = [UIFont fontNamesForFamilyName:myFontFamilyName];
for (NSString *aFontName in myFontNames)
{
    NSRange myRange = [aFontName rangeOfString:@"-Italic"];
    if (myRange.location != NSNotFound)
    {
        if (myRange.location + myRange.length >= aFontName.length)
        {
            // This is it! Nothing before the hyphen, nothing after.
            myBaseForItalicFont = [UIFont fontWithName:aFontName size:myRVGItalicFont.pointSize];
            break;
        }
    }
}
0

Here Cœur's answer in Swift:

let originalFont = UIFont(name: "Georgia", size: 15)
// or: UIFont.preferredFont(forTextStyle: .body)
let originalFontDesc = originalFont.fontDescriptor
let italicFontDesc = originalFontDesc.withSymbolicTraits(.traitItalic)
let italicFont = UIFont(descriptor: italicFontDesc, size: 0)
myLabel.font = italicFont
Motine
  • 1,206
  • 12
  • 14