Is there a medium weight font between -systemFontOfSize: and -boldSystemFontOfSize:?

谁都会走 提交于 2019-12-03 22:03:59

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:

Starting with iOS 8.2 you can use:

[UIFont systemFontOfSize:14 weight:UIFontWeightMedium];

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)
}

Swift 3:

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

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!