2011-06-04 87 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 
} 

注:简单地改变标签的文本对我也没有工作,我不得不重新初始化。

希望它有帮助。