2011-06-04 83 views
1

我的UITextView其中用戶鍵入的文本。顯示鍵盤時,我在其上添加了帶有UIlabel的inputView。我想要這個UIlabel來保存文本的字符長度。這似乎很容易的事,但可惜的是,當用戶改變文本..如何更新inputView子視圖的UILabel?

我這是怎麼加載inputView

_textView.inputView = [self inputAccessoryView]; 

在inputAccessoryView我只需添加的UILabel作爲一個子視圖時不更新這個字計數器的UILabel。當鍵盤顯示時,UILabel也用inputView顯示。我跟蹤

- (void)textViewDidChange:(UITextView *)textView 

可惜的是該的UILabel從不更新(重繪)的變化。當我登錄到控制檯的值,該值是正確的,那麼它的更新,但是的UILabel是從來沒有重繪,並保持默認值。

任何人都可以幫助我嗎?

+0

你是如何試圖更新標籤,以及如何你肯定的是,即使用戶界面不顯示它的價值被更新? – 2011-06-04 16:03:23

+0

請顯示更多代碼。 – jaminguy 2011-06-04 16:45:25

回答

0

做你

_textView.delegate = self; 

0

我知道這是5年前,但它可能會幫助別人,誰喜歡我偶然發現了你的問題。

我用一個UIToolbar作爲我inputAccessoryView(在我的情況下,它有一個標籤,一個靈活的分離器和按鈕)。 在textFieldEditingChanged事件我重建工具欄的一部分,這樣

@IBAction func textFieldEditingChanged(_ sender: UITextField) { 

    //get a reference to the toolbar 
    let toolBar = sender.inputAccessoryView as! UIToolbar 

    //create a new label and set size + other properties 
    toolbarLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 40, height: 22)) 
    toolbarLabel.text = mySpecificCalculatedString(sender.text!) 
    toolbarLabel.font = defaultFont(17) 
    toolbarLabel.adjustsFontSizeToFitWidth = true 
    let width = toolbarLabel.textRect(forBounds: CGRect(x: 0, y: 0, width: maxWidthForLabel, height: 0), limitedToNumberOfLines: 1).size.width 
    toolbarLabel.frame = CGRect(x: 0, y: 0, width: width, height: 22) 
    toolbarLabel.textColor = .black 
    toolbarLabel.tag = 666 

    //rebuild the specific tolbar item 
    let customView = UIBarButtonItem(customView: toolbarLabel) 
    toolBar.items![0] = customView 
} 

注:簡單地改變標籤的文本對我也沒有工作,我不得不重新初始化。

希望它有幫助。