ios -systemFontOfSize: 和 -boldSystemFontOfSize: 之间是否有中等粗细的字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10400320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a medium weight font between -systemFontOfSize: and -boldSystemFontOfSize:?
提问by Proud Member
-systemFontOfSize is too thin, and boldSystemFontOfSize too thick. I need something inbetween.
-systemFontOfSize 太细,而 boldSystemFontOfSize 太粗。我需要介于两者之间的东西。
When I create a UIFont like this:
当我创建这样的 UIFont 时:
[UIFont boldSystemFontOfSize:14];
then the debugger prints this font info:
然后调试器打印此字体信息:
font-family: ".Helvetica NeueUI"; font-weight: bold; font-style: normal; font-size: 14px
字体系列:“.Helvetica NeueUI”;字体粗细:粗体;字体样式:正常;字体大小:14px
Sometimes fonts have a medium font weight. How can I create a font of this type but with a medium weight?
有时字体具有中等字体粗细。如何创建这种类型但中等重量的字体?
采纳答案by JustSid
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:
调用NSLog(@"%@", [UIFont fontNamesForFamilyName:@"Helvetica Neue"]);
打印 Helvetica Neue 的所有可用字体样式,HelveticaNeue-Medium
其中听起来像您想要的字体样式:
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:
如果您想确保在系统字体更改时字体也会更改(例如,就像使用从 Helvetica 到 Helvetica Neue 的 Retina 设备所做的那样),您可以先获取系统字体,然后使用它familyName
并pointSize
检索中等重量的字体+ fontNamesForFamilyName
进而+ fontWithName:size:
回答by Andrei
回答by Tai Le
Swift 2.0, xcode7.1
斯威夫特 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)
}
回答by Anton Plebanovich
Swift 3:
斯威夫特 3:
UIFont.systemFont(ofSize: 14, weight: UIFontWeightMedium)
回答by Axel Guilmin
With Swift 4 or 5, add this extension :
使用 Swift 4 或 5,添加此扩展:
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
同样的原则也适用与.semibold
,.black
等,检查UIFont.Weight
回答by hashier
Swift 2.3 and not changing the font size:
Swift 2.3 并且不改变字体大小:
extension UIFont {
var mediumFont: UIFont {
return UIFont.systemFontOfSize(self.pointSize, weight: UIFontWeightMedium)
}
}
Use with:
与:
titleLabel.font = titleLabel.font.mediumFont