回答
UIFontDescriptor
是非常強大的類,可用於獲取大膽一些點你想要的字體版本。它應該是完全安全的和未來的證明,因爲它只需要使用公共API並且不依賴於任何字符串修改。
這裏是斯威夫特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) }
}
}
這裏是在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
這是iOS 7以來的最佳答案。 – iwill
使用NSString+UIKit方法來測量具有特定字體的字符串的大小。
iPhone將相同名稱的普通和粗體字體視爲完全獨立的字體,因此沒有簡單的方法在它們之間進行轉換。例如,ArialMT和Arial-BoldMT被OS視爲完全不同的字體。
編輯:我可能誤解了你的問題。也許你正在尋找UIFont的pointSize屬性?
要從UIFont實例訪問的字號使用
@property(nonatomic, readonly) CGFloat pointSize
還有更多的屬性來告訴字體大小(帽,小等)
capHeight,xHeight,pointSize
下面使用訪問粗體字體
UIFont* myboldFont = [UIFont boldSystemFontOfSize:11];
'boldFont:'方法完全沒用;它只是'boldSystemFontOfSize:'的包裝器。似乎很明顯,@iwill正在問如何將現有的UIFont轉換爲其大膽的UIFont。 – titaniumdecoy
這是一箇舊的答案,它不再是最好的解決方案請參閱接受的答案。
-(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;
}
最後:if(boldFont)return boldFont;迴歸自我; – iwill
並且:NSString * fontName = self.fontName;應該是NSString * fontName = self.familyName; //那麼這將是完美的,謝謝! – iwill
附加'-BoldMT'不適用於ArialMT等字體。其大膽的等價物是「Arial-BoldMT」,而不是「ArialMT-BoldMT」。 – titaniumdecoy
你可以用核心文本檢查粗體。使用kCTFontBoldTrait
。
這是最簡單的方式找到字體大小
UIFont *anyFont;
//initialization and other stuff here
現在你想找到它的大小
CGFloat fontSize= [anyFont pointSize];
extension UIFont {
func boldFont() -> UIFont? {
let fontDescriptor = self.fontDescriptor
let fontDescriptorSymbolicTraits: UIFontDescriptorSymbolicTraits = [ fontDescriptor.symbolicTraits, .traitBold ]
guard let boldFontDesc = fontDescriptor.withSymbolicTraits(fontDescriptorSymbolicTraits) else {
return nil
}
return UIFont(descriptor: boldFontDesc, size: pointSize)
}
}
- 1. 最大字體大小UIFont
- 2. UIFont:字體大小不同
- 3. 如何創建一個大膽但不粗體的UIFont?
- 4. 如何從常規UIFont創建粗體UIFont?
- 5. 如何粗體和更改圖表的字體大小標題
- 6. 如何從CMFCPropertyFontProperty獲取字體大小
- 7. 獲取字體大小
- 8. Java獲取字體大小
- 9. 如何獲取UILabel的字體大小和字體名稱?
- 10. 如何在RichTextBox中保留文本格式,如粗體,斜體,字體顏色,字體大小等?
- 11. 從文本中獲取字體和字體大小?
- 12. 如何在JTextArea實例中爲所選文本設置粗體字體樣式
- 13. 字體大小加粗PhantomJs screenshot
- 14. 如何在Android中獲取文本視圖的字體大小
- 15. 在dompdf中粗體或更大文字
- 16. UIFont:我無法獲取字體的大小。爲什麼我的字體等於零?
- 17. 如何設置內容的字體樣式表,包括字體顏色,字體大小和粗體字?
- 18. 控制字體粗細而不更改字體大小
- 19. 如何以像素爲單位獲取終端大小或字體大小?
- 20. 用CGContextShowGlyphsAtPoint繪製文本 - 如何獲取字體大小?
- 21. 字體大小100%,例外
- 22. 更改NSMutableAttributedString的字體大小而不刪除粗體格式。
- 23. 無法獲得UIFont的黑板SE粗體
- 24. SVG - 獲取TextElement的字體大小
- 25. 獲取div的計算字體大小
- 26. safari css粗體字體太粗體
- 27. 谷歌字體與字體重量:粗體不具有相同的大小
- 28. Blackberry:LabelField文本的粗體字體
- 29. Ghostscript粗體字體
- 30. 如何支持中文的斜體字體和粗體字體
你斯威夫特3正在尋找['pointSize'](http://developer.apple。 com/library/ios/documentation/uikit/reference/UIFont_Class/Reference/Reference.html#// apple_ref/occ/instp/UIFont/pointSize)。 –
收件人:@Deepak,pointSize是否與fontSize相同? – iwill
@iill:是的,'pointSize'將等於您提供給UIFont構造函數的'size:'參數的值。如果您閱讀UIFont的文檔,您會看到fontSize/fontOfSize方法以點爲單位獲取或返回值。 – titaniumdecoy