41

-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?

Proud Member
  • 40,078
  • 47
  • 146
  • 231
  • do note that on this VERY OLD question, scroll to the correct modern answer @AxelGuilmin which works – Fattie Jan 29 '20 at 12:55

6 Answers6

84

Starting with iOS 8.2 you can use:

[UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
Andrei
  • 2,607
  • 1
  • 23
  • 26
25

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:

JustSid
  • 25,168
  • 7
  • 79
  • 97
  • This is not the exact same font as the UI version. In my experience it differs in at least in vertical spacing attributes and returns a different result when you get the size property from an attributed string. – Yoav Sep 17 '15 at 11:30
  • 1
    @Ben-Uri The system font has changed since I wrote that answer. Andrei's answer is more spot on now. – JustSid Sep 17 '15 at 11:31
17

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)
}
Tai Le
  • 8,530
  • 5
  • 41
  • 34
  • what happens in iOS 8.2 if I don't use the if statement and I just use titleLabel?.font = UIFont(name: "HelveticaNeue-Medium", size: 15) – Jace Dec 01 '16 at 06:31
8

Swift 3:

UIFont.systemFont(ofSize: 14, weight: UIFontWeightMedium)
Anton Plebanovich
  • 1,296
  • 17
  • 17
8

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

Axel Guilmin
  • 11,454
  • 9
  • 54
  • 64
0

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
hashier
  • 4,670
  • 1
  • 28
  • 41