1

我有 UITextView 用户在其中键入文本。显示键盘时,我在其上添加带有 UIlabel 的 inputView。我希望这个 UIlabel 保存文本的字符长度。这似乎很容易,但不幸的是,当用户更改文本时它不会更新这个单词计数器 UILabel ..

这就是我加载 inputView 的方式

_textView.inputView = [self inputAccessoryView];

在 inputAccessoryView 中,我只是将 UILabel 添加为子视图。显示键盘时,UILabel 也与 inputView 一起显示。我跟踪变化

- (void)textViewDidChange:(UITextView *)textView

不幸的是, UILabel 从未更新(重绘)。当我登录控制台它的值时,该值是正确的,所以它的更新,但 UIlabel 永远不会重绘并保持默认值。

谁能帮我这个?

4

2 回答 2

0

你是否

_textView.delegate = self;

?

于 2011-06-15T04:31:42.873 回答
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
}

注意:简单地更改标签的文本对我也不起作用,我不得不重新初始化它。

希望能帮助到你。

于 2016-12-04T00:53:30.343 回答