41
votes

-systemFontOfSize is too thin, and boldSystemFontOfSize too thick. I need something inbetween.

When I create a UIFont like this:

[UIFont boldSystemFontOfSize:14];

then the debugger prints this font info:

font-family: ".Helvetica NeueUI"; font-weight: bold; font-style: normal; font-size: 14px

Sometimes fonts have a medium font weight. How can I create a font of this type but with a medium weight?

6
do note that on this VERY OLD question, scroll to the correct modern answer @AxelGuilmin which worksFattie

6 Answers

85
votes

Starting with iOS 8.2 you can use:

[UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
25
votes

Calling NSLog(@"%@", [UIFont fontNamesForFamilyName:@"Helvetica Neue"]); prints all available font styles for Helvetica Neue, among them is HelveticaNeue-Medium which sounds like the one you want:

UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0f];

If you want to make sure that the font also changes when the system font changes (eg. like it did with retina devices from Helvetica to Helvetica Neue) you could first get the system font and then take its familyName and pointSize to retrieve one with medium weight using + fontNamesForFamilyName and then + fontWithName:size:

17
votes

Swift 2.0, xcode7.1

if #available(iOS 8.2, *) {
   titleLabel?.font = UIFont.systemFontOfSize(15, weight: UIFontWeightMedium)
} else {
    titleLabel?.font = UIFont(name: "HelveticaNeue-Medium", size: 15)
}
8
votes

Swift 3:

UIFont.systemFont(ofSize: 14, weight: UIFontWeightMedium)
8
votes

With Swift 4 or 5, add this extension :

extension UIFont {
    class func mediumSystemFont(ofSize pointSize: CGFloat) -> UIFont {
        return self.systemFont(ofSize: pointSize, weight: .medium)
    }
}

Then :

myLabel.font = UIFont.mediumSystemFont(ofSize: 16)

Same principle works with .semibold, .black, etc, check UIFont.Weight

0
votes

Swift 2.3 and not changing the font size:

extension UIFont {
    var mediumFont: UIFont {
        return UIFont.systemFontOfSize(self.pointSize, weight: UIFontWeightMedium)
    }
}

Use with:

titleLabel.font = titleLabel.font.mediumFont