0
我試圖編寫一個UIFont
擴展名來增加字體大小,當單詞顯示在5.5英寸的iPhone。聽到的是我的代碼:fontSize總是17當從故事板加載
public override class func initialize() {
struct Static {
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token) {
let originalSelector = Selector("systemFontOfSize:")
let swizzledSelector = Selector("systemFontOfFlexibleSize:")
let originalMethod = class_getClassMethod(self, originalSelector)
let swizzledMethod = class_getClassMethod(self, swizzledSelector)
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
class func systemFontOfFlexibleSize(fontSize: CGFloat)->UIFont {
print(fontSize)
if UIScreen.mainScreen().bounds.height >= 736.0 {
return self.systemFontOfFlexibleSize(fontSize + 2)
} else {
return self.systemFontOfFlexibleSize(fontSize)
}
}
這個工作很好,當我使用的方法systemFontOfSize
代碼。當我使用xib或storyboard創建標籤時,systemFontOfFlexibleSize
仍然被調用,但無論我在故事板中設置了什麼,fontSize始終爲17。 有人可以解釋這個或任何其他優雅的解決方案嗎?
感謝您的幫助,提前致謝。
我困惑的問題是,我不能得到正確的標籤(在故事板)fontSize的,當故事板負載。 – ZeroChow
@ZeroChow所以你永遠不會改變代碼中的字體大小? – ClockWise
是的。當storyboard加載時調用'systemFontOfFlexibleSize',但是我得到一個錯誤的fontSize – ZeroChow