我有一個UIView,UILabel我想允許用戶動態更改字體大小。下面的代碼幾乎可以作業,但問題是在將 newUIFont分配給標簽后,它不會立即顯示在 UI 中。 相反,需要一些其他 UI 活動才能使其顯示。
我正在尋找的是一種在手勢結束后立即顯示字體更改的技術。
從本教程中展示的技術開始,我在UIScrollView. 在關聯中UIViewController,我實作了handlePinch(UIpinchGestureRecognizer)。
在該函式中,我僅在手勢開始和結束時才采取行動(不嘗試影片)。如果用戶指示縮放,我將字體增加 4 個點。如果用戶表示縮小,我將字體減少 4 磅。這一切都有效,除了螢屏在執行另一個 UI 功能之前不會反映更改。我只在模擬器上測驗過這個,但我認為它在真實設備上是一樣的。
我已經在傳遞給函式的視圖以及視圖上嘗試了setNeedsDisplay和setNeedsLayout其他一些奇怪的事情。UILabelhandlePinch()
@IBAction func handlePinch(_ gesture: UIPinchGestureRecognizer) {
guard let gestureView = gesture.view else {
return;
}
gestureView.transform = gestureView.transform.scaledBy(x: gesture.scale, y: gesture.scale)
if (gesture.state == UIPinchGestureRecognizer.State.began) {
pinchD = gestureView.transform.d
print("pinch started ", pinchD)
}
if (gesture.state == UIPinchGestureRecognizer.State.ended) {
print("pinch ended ", gestureView.transform.d)
print("compare:", pinchD, gestureView.transform.d)
let larger = gestureView.transform.d > pinchD
let fontSize = multiLineLabel.font.pointSize
if (larger) {
if (fontSize < 28) {
let calculatedFont = UIFont(name: multiLineLabel.font.fontName, size: fontSize 4)
print("bigger fontSize", fontSize 4);
multiLineLabel.font = calculatedFont
}
} else {
if (fontSize > 12) {
let calculatedFont = UIFont(name: multiLineLabel.font.fontName, size: fontSize - 4)
print("smaller fontSize", fontSize - 4);
multiLineLabel.font = calculatedFont
}
}
}
}
將計算字體應用到應用程式中后需要做什么UILabel才能使其立即顯示在應用程式中?
附加說明:
handlePinch() runs as expected; every time a pinch gesture ends, I see bigger fontSize... or smaller fontSize... logged.
After the logging, the simulator screen font size remains the same until I do one of several things. One thing that causes the font size to change is if I swipe to the next item. Before the animation starts, the font increases. The font changes also if I hit the back button (the font change shows briefly before the other view appears).
uj5u.com熱心網友回復:
事實證明,我的假設是不正確的:
我只在模擬器上測驗過這個,但我認為它在真實設備上是一樣的。
我相信那是不正確的。
我正在一個非常非常舊的 iMac 上進行測驗,模擬器陷入了困境,更新螢屏需要 15 或 30 秒!
所以我得出結論,代碼畢竟作業正常。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/433744.html
標籤:swift xcode uilabel refresh uifont
