2014-10-30 68 views
2

將文本以編程方式添加到UILabel。隨着更多文本的添加,文本將包裝並增加標籤的高度。添加文本後UILabel反彈

的問題是,當文本換行在一行的末尾,整個標籤會跳起來1線的高度和動畫本身回落到它的正確位置。最終的結果很好,但你如何擺脫奇怪的跳躍動畫。同樣,每當UILabel的高度因另一行換行而增加時,就會發生這種情況。

示例代碼:

var eventCount = 0; 
func someEvent(sender:AnyObject){ 
    eventCount += 1; 
    if(eventCount == 1){ 
     lbl.text = "this" 
    }else if(eventCount == 2){ 
     lbl.text = "this is some" 
    }else if(eventCount == 3){ 
     lbl.text = "this is some sample text" 
    }else if(eventCount == 4){ 
     // this is where text wraps to line 2 
     // the label jumps up 20px or so and 
     // animates back down to it's original position 
     lbl.text = "this is some sample text that causes the label to wrap" 
    } 
} 

自動版式約束

  0 
      | 
    0 - UILabel - 0 

標籤屬性

lines = 0 
+0

郵政編碼請 – Jatin 2014-10-30 20:57:08

+0

增加示例代碼;謝謝參觀! – bgolson 2014-10-30 21:13:59

+0

當你添加文本時你是否嘗試過'[self.view layoutIfNeeded]'? – 2014-10-30 21:25:28

回答

3

我已經設法再現與下面的代碼的問題:

class ViewController: UIViewController { 

    @IBOutlet weak var label: UILabel! 

    @IBAction func buttonPushed(sender: AnyObject) { 
     UIView.animateWithDuration(0.5) { 
      self.someEvent(self) 
      self.view.layoutIfNeeded() 
     } 
    } 

    func someEvent(sender:AnyObject){ 
     self.label.text! += " test" 
    } 
} 

所以,我相信你的someEvent()的動畫塊中調用。

UIView.performWithoutAnimation解決了這個問題。

var eventCount = 0; 
func someEvent(sender:AnyObject){ 
    UIView.performWithoutAnimation { 
     self.eventCount += 1; 
     if(self.eventCount == 1){ 
      self.lbl.text = "this" 
     }else if(self.eventCount == 2){ 
      self.lbl.text = "this is some" 
     }else if(self.eventCount == 3){ 
      self.lbl.text = "this is some sample text" 
     }else if(self.eventCount == 4){ 
      self.lbl.text = "this is some sample text that causes the label to wrap" 
     } 
     self.view.layoutIfNeeded() 
    } 
} 
+0

+1非常感謝您抽出寶貴的時間來重現這一點。奇怪的是,臭蟲清除本身了,所以它可能是一個間歇性問題。現在它在我的複雜屏幕和簡單測試用例上都能正常工作。不涉及動畫塊,但如果沒有其他洞察力提供給此bug,我會給你賞金。再次感謝! – bgolson 2014-11-05 23:33:23