問題
設(shè)計師同學(xué)說有個標題的字體應(yīng)該加粗册舞,需要修改下盐捷。
解決
檢查后發(fā)現(xiàn)代碼中忽略了字體的粗細屬性。
UIFont *font = [UIFont systemFontOfSize:fontSize];
這個方法是不支持設(shè)置字體粗細的讶坯。實際上系統(tǒng)提供了設(shè)置字體的粗細的方法。
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize;
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize weight:(UIFontWeight)weight NS_AVAILABLE_IOS(8_2);
需要注意的是下面這個weight設(shè)置的方法只是在iOS8.2開始的版本生效岗屏。修改后設(shè)置字體的方法如下:
if(([[[UIDevice currentDevice] systemVersion] compare:@"8.2" options:NSNumericSearch] == NSOrderedAscending)) {
font = [UIFont systemFontOfSize:fontSize];
} else {
font = [UIFont systemFontOfSize:fontSize weight:textWeight];
}
擴展
- fontWeight是描述字體粗細程度的屬性辆琅,我們平時比較少注意到。另外iOS中定義了UIFontWeight的一些常量
UIKIT_EXTERN const UIFontWeight UIFontWeightUltraLight NS_AVAILABLE_IOS(8_2);
UIKIT_EXTERN const UIFontWeight UIFontWeightThin NS_AVAILABLE_IOS(8_2);
UIKIT_EXTERN const UIFontWeight UIFontWeightLight NS_AVAILABLE_IOS(8_2);
UIKIT_EXTERN const UIFontWeight UIFontWeightRegular NS_AVAILABLE_IOS(8_2);
UIKIT_EXTERN const UIFontWeight UIFontWeightMedium NS_AVAILABLE_IOS(8_2);
UIKIT_EXTERN const UIFontWeight UIFontWeightSemibold NS_AVAILABLE_IOS(8_2);
UIKIT_EXTERN const UIFontWeight UIFontWeightBold NS_AVAILABLE_IOS(8_2);
UIKIT_EXTERN const UIFontWeight UIFontWeightHeavy NS_AVAILABLE_IOS(8_2);
UIKIT_EXTERN const UIFontWeight UIFontWeightBlack NS_AVAILABLE_IOS(8_2);
- 另外在CSS中關(guān)于font-weight的描述是比較詳細的这刷,可以參考CSS font-weight 屬性和font-weight里面的說明婉烟。