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

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 に答える
0

あなたでしたか

_textView.delegate = self;

?

于 2011-06-15T04:31:42.873 に答える