问题
-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?
回答1:
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:
回答2:
Starting with iOS 8.2 you can use:
[UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
回答3:
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)
}
回答4:
Swift 3:
UIFont.systemFont(ofSize: 14, weight: UIFontWeightMedium)
回答5:
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
回答6:
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
来源:https://stackoverflow.com/questions/10400320/is-there-a-medium-weight-font-between-systemfontofsize-and-boldsystemfontofsi