【问题标题】:I thought I wanted an infinite loop. What kind of loop do I want?我以为我想要一个无限循环。我想要什么样的循环?
【发布时间】:2024-01-06 01:56:01
【问题描述】:

我想突出显示 textview 中出现的每个单词。我认为我想要一个无限循环,因为我希望它始终扫描关键字的条目文本。我正在学习可能并非如此。我在寻找什么样的循环来实现连续扫描文本的目标?

while (range.location == textView.text.characters.count) {
    text.addAttribute(NSForegroundColorAttributeName, value:UIColor.redColor(), range: nsString.rangeOfString("red"))
    text.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, stringLength))
}

【问题讨论】:

    标签: ios objective-c swift swift2


    【解决方案1】:

    无限循环不是你想要的。 您希望在用户对文本进行更改时收到通知,然后根据需要循环遍历文本和颜色。

    看看 UITextViewDelegate 方法。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextViewDelegate_Protocol/

    具体来说

    - (void)textViewDidEndEditing:(UITextView *)textView
    

    当用户将响应者退出到 textview 时会发生这种情况

    或者更好的体验是在他们打字时为文本着色。

    UITextField 具有动作 UIControlEventEditingChanges,它会在每次文本编辑时触发。

    【讨论】:

      最近更新 更多