小编Jas*_*son的帖子

UITextView / NSAttribute:检测单词是否以特定符号开头

我一直在寻找一种方法来更改UITextView单词中以"@"或开头的文本"#"。我在下面的StackOverflow上找到了此代码段,如果您键入"Hello"或,则可以完美运行"World"

我如何调整此代码,以便它可以检测单词是否以“ @”或“#”开头,​​后跟空格之前的任意数量的字符,并应用相同的样式?

UITextView如果用户以"@"或开头“单词”,结果将导致文本颜色发生变化"#"。即:

快速的棕色#fox跳过了@lazydog

func textViewDidChange(_ textView: UITextView) {
    let defaultAttributes = mediaDescription.attributedText.attributes(at: 0, effectiveRange: nil)
    let attrStr = NSMutableAttributedString(string: textView.text, attributes: defaultAttributes)
    let inputLength = attrStr.string.count
    let searchString : NSArray = NSArray.init(objects: "hello", "world")
    for i in 0...searchString.count-1
    {
        let string : String = searchString.object(at: i) as! String
        let searchLength = string.count
        var range = NSRange(location: 0, …
Run Code Online (Sandbox Code Playgroud)

uitextview nsattributedstring ios swift

1
推荐指数
1
解决办法
288
查看次数

标签 统计

ios ×1

nsattributedstring ×1

swift ×1

uitextview ×1