iwi*_*ill 38 iphone font-size uifont ios
如何获取UIFont实例的字体大小?
或者,如果有人可以为UIFont实现此方法?
- (UIFont *)boldFont;
Run Code Online (Sandbox Code Playgroud)
Emi*_*aez 38
这是一个老答案,它不再是最好的解决方案,请参阅接受的答案.
-(UIFont *)boldFont{
//First get the name of the font (unnecessary, but used for clarity)
NSString *fontName = self.fontName;
//Some fonts having -Regular on their names. so we have to remove that before append -Bold / -BoldMT
fontName = [[fontName componentsSeparatedByString:@"-"] firstObject];
//Then append "-Bold" to it.
NSString *boldFontName = [fontName stringByAppendingString:@"-Bold"];
//Then see if it returns a valid font
UIFont *boldFont = [UIFont fontWithName:boldFontName size:self.pointSize];
//If it's valid, return it
if(boldFont) return boldFont;
//Seems like in some cases, you have to append "-BoldMT"
boldFontName = [fontName stringByAppendingString:@"-BoldMT"];
boldFont = [UIFont fontWithName:boldFontName size:self.pointSize];
//Here you can check if it was successful, if it wasn't, then you can throw an exception or something.
return boldFont;
}
Run Code Online (Sandbox Code Playgroud)
Jha*_*iya 25
要从UIFont实例访问字体大小,请使用
@property(nonatomic, readonly) CGFloat pointSize
Run Code Online (Sandbox Code Playgroud)
有更多属性可以告诉字体大小(对于上限,小等...)
capHeight,xHeight,pointSize
Run Code Online (Sandbox Code Playgroud)
使用下面的代码访问粗体字体
UIFont* myboldFont = [UIFont boldSystemFontOfSize:11];
Run Code Online (Sandbox Code Playgroud)
Raf*_*iak 15
UIFontDescriptor是非常强大的类,可用于获得您想要的字体的粗体版本.它应该是完全安全的和未来的证明,因为它只需要使用公共API并且不依赖于任何字符串修改.
这是Swift 3中的代码:
extension UIFont {
func bold() -> UIFont? {
let fontDescriptorSymbolicTraits: UIFontDescriptorSymbolicTraits = [fontDescriptor.symbolicTraits, .traitBold]
let bondFontDescriptor = fontDescriptor.withSymbolicTraits(fontDescriptorSymbolicTraits)
return bondFontDescriptor.flatMap { UIFont(descriptor: $0, size: pointSize) }
}
}
Run Code Online (Sandbox Code Playgroud)
这是objective-c中的代码:
@implementation UIFont (RABoldFont)
- (UIFont *)ra_boldFont
{
UIFontDescriptor *fontDescriptor = [self fontDescriptor];
UIFontDescriptorSymbolicTraits traits = fontDescriptor.symbolicTraits;
traits = traits | UIFontDescriptorTraitBold;
UIFontDescriptor *boldFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:traits];
return [UIFont fontWithDescriptor:boldFontDescriptor size:self.pointSize];
}
@end
Run Code Online (Sandbox Code Playgroud)
这是查找字体大小的最简单方法
UIFont *anyFont;
//initialization and other stuff here
Run Code Online (Sandbox Code Playgroud)
现在在某些时候你想找到它的大小
CGFloat fontSize= [anyFont pointSize];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37781 次 |
| 最近记录: |