iOS - 如何检测字体是否为 bold/black/heavy...?
iOS - How to detect whether a font is bold/black/heavy...?
我想检测a的样式(bold ,heavy, black)字体。但是我只能检测字体是否为粗体。
BOOL isBold = (font.fontDescriptor.symbolicTraits & UIFontDescriptorTraitBold)!=0;
UIFontDescriptorSymbolicTraits
.
中没有黑色或沉重的特征
一种方法是检查字体名称是否包含'black'或'heavy'字符串,但这似乎不可靠。
有UIFontWeightTrait
,但只有UIFont systemFontOfSize: weight:
如果有可用的样式,我想创建带有样式的自定义字体。
Swift检测Heavy/Black字体样式的版本
let fontUsage = font.fontDescriptor.fontAttributes["NSCTFontUIUsageAttribute"] as! String
if fontUsage == "CTFontHeavyUsage"{
print("It is heavy")
}
else if fontUsage == "CTFontBlackUsage"{
print("it's black")
}
并创建具有属性的字体:
let fontDescriptor = UIFontDescriptor(fontAttributes: ["NSCTFontUIUsageAttribute" : "CTFontHeavyUsage"])
let font = UIFont(descriptor: fontDescriptor, size: 17)
检查它是 Heavy 还是 Black:
NSString *fontUsage = font.fontDescriptor.fontAttributes[@"NSCTFontUIUsageAttribute"];
if ([fontUsage isEqualToString:@"CTFontHeavyUsage"]) {
NSLog(@"It's Heavy");
}
else if ([fontUsage isEqualToString:@"CTFontBlackUsage"]) {
NSLog(@"It's Black");
}
其他用法选项列表很简单,把用法写成"CTFont......Usage"格式即可,我测试的列表是:
//CTFontUltraLightUsage,CTFontThinUsage,CTFontLightUsage,CTFontMediumUsage,CTFontDemiUsage
以及如何创建带有用法的字体,例如 heavy:
UIFontDescriptor *fontDescriptor = [[UIFontDescriptor alloc] initWithFontAttributes:@{@"NSCTFontUIUsageAttribute":@"CTFontHeavyUsage"}];
UIFont *font = [UIFont fontWithDescriptor:fontDescriptor size:17];
Swift3 检查版本:
if let fontUsage = font.fontDescriptor.fontAttributes["NSCTFontUIUsageAttribute"] as? String {
if fontUsage == "CTFontHeavyUsage" {
print("It's Heavy")
}
else if fontUsage == "CTFontBlackUsage" {
print("It's Black")
}
}
这会告诉您字体是否为粗体:
var isBold = label.font.fontDescriptor.symbolicTraits.contains(.traitBold)
这里有一些经验:即使设置了粗体字体,或者如果您手动将字体的 symbolicTraits 设置为粗体,这也会为您提供正确答案:
我想检测a的样式(bold ,heavy, black)字体。但是我只能检测字体是否为粗体。
BOOL isBold = (font.fontDescriptor.symbolicTraits & UIFontDescriptorTraitBold)!=0;
UIFontDescriptorSymbolicTraits
.
一种方法是检查字体名称是否包含'black'或'heavy'字符串,但这似乎不可靠。
有UIFontWeightTrait
,但只有UIFont systemFontOfSize: weight:
如果有可用的样式,我想创建带有样式的自定义字体。
Swift检测Heavy/Black字体样式的版本
let fontUsage = font.fontDescriptor.fontAttributes["NSCTFontUIUsageAttribute"] as! String
if fontUsage == "CTFontHeavyUsage"{
print("It is heavy")
}
else if fontUsage == "CTFontBlackUsage"{
print("it's black")
}
并创建具有属性的字体:
let fontDescriptor = UIFontDescriptor(fontAttributes: ["NSCTFontUIUsageAttribute" : "CTFontHeavyUsage"])
let font = UIFont(descriptor: fontDescriptor, size: 17)
检查它是 Heavy 还是 Black:
NSString *fontUsage = font.fontDescriptor.fontAttributes[@"NSCTFontUIUsageAttribute"];
if ([fontUsage isEqualToString:@"CTFontHeavyUsage"]) {
NSLog(@"It's Heavy");
}
else if ([fontUsage isEqualToString:@"CTFontBlackUsage"]) {
NSLog(@"It's Black");
}
其他用法选项列表很简单,把用法写成"CTFont......Usage"格式即可,我测试的列表是:
//CTFontUltraLightUsage,CTFontThinUsage,CTFontLightUsage,CTFontMediumUsage,CTFontDemiUsage
以及如何创建带有用法的字体,例如 heavy:
UIFontDescriptor *fontDescriptor = [[UIFontDescriptor alloc] initWithFontAttributes:@{@"NSCTFontUIUsageAttribute":@"CTFontHeavyUsage"}];
UIFont *font = [UIFont fontWithDescriptor:fontDescriptor size:17];
Swift3 检查版本:
if let fontUsage = font.fontDescriptor.fontAttributes["NSCTFontUIUsageAttribute"] as? String {
if fontUsage == "CTFontHeavyUsage" {
print("It's Heavy")
}
else if fontUsage == "CTFontBlackUsage" {
print("It's Black")
}
}
这会告诉您字体是否为粗体:
var isBold = label.font.fontDescriptor.symbolicTraits.contains(.traitBold)
这里有一些经验:即使设置了粗体字体,或者如果您手动将字体的 symbolicTraits 设置为粗体,这也会为您提供正确答案: