Is there a medium weight font between -systemFontOfSize: and -boldSystemFontOfSize:?
Solution 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:
Solution 2
Starting with iOS 8.2 you can use:
[UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
Solution 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)
}
Solution 4
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
Solution 5
Swift 3:
UIFont.systemFont(ofSize: 14, weight: UIFontWeightMedium)
Comments
-
Proud Member almost 2 years
-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?
-
Yoav almost 9 yearsThis 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.
-
JustSid almost 9 years@Ben-Uri The system font has changed since I wrote that answer. Andrei's answer is more spot on now.
-
onmyway133 almost 8 yearsNote that
UIFontWeightMedium
is a CGFloat, not an enum -
Jace over 7 yearswhat 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)