有了UILabel任何字体,我怎么能知道它是否已经是粗体?或者我怎样才能使它变得大胆?在CSS中,我有一个font-weight属性.我想有类似的东西.
到目前为止我发现的一切都是你必须设置正确的字体名称.但是,这是不可靠的.大胆的版本Cochin是Cochin-Bold,但大胆的版本ArialMT是不是ArialMT-Bold,但是Arial-BoldMT,因此它显然不足以追加-Bold.(自定义字体的粗体版本也可能有完全不同的名称).
我能做的是找到我给定字体系列的所有字体.
__block UIFont *font = myLabel.font;
[[UIFont fontNamesForFamilyName:font.familyName] enumerateObjectsUsingBlock:^(NSString *fontName, NSUInteger idx, BOOL *stop) {
if ([fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch].location != NSNotFound) {
font = [UIFont fontWithName:fontName size:font.pointSize];
*stop = YES;
}
}];
myLabel.font = font;
Run Code Online (Sandbox Code Playgroud)
但这并不可靠.我可以很容易地得到一个BoldItalic版本.我可以改进我的检查以避免这种情况,但这不是一个很好的解决方案.
也许CoreText可以在这里提供帮助?
Amy*_*all 10
也许CoreText可以在这里提供帮助?
CoreText使用自己的字体系统CTFont.如果你正在使用它,你可以做你想要的:
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)name, size, NULL);
CTFontRef boldFont = CTFontCreateCopyWithSymbolicTraits(font, 0.0, NULL, kCTFontBoldTrait, kCTFontBoldTrait);
Run Code Online (Sandbox Code Playgroud)
我想你可以得到派生的粗体字体的名称:
CFStringRef boldName = CTFontCopyPostScriptName(boldFont);
Run Code Online (Sandbox Code Playgroud)
...并使用它来创建一个新的UIFont:
UIFont *ret = [UIFont fontWithName:(NSString *)boldName size:size];
Run Code Online (Sandbox Code Playgroud)
我不知道这会有多快,但您可以在应用程序启动时执行此操作然后缓存名称.
| 归档时间: |
|
| 查看次数: |
2947 次 |
| 最近记录: |