ios - 用户界面 TextView /NSAttribute : Detect if word starts with a particular symbol

标签 ios swift uitextview nsattributedstring

我一直在寻找一种方法来更改 UITextView 中的文本当单词以 "@" 开头时或 "#" .我找到了 this code snippet下面在 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, length: attrStr.length)

        while (range.location != NSNotFound) {
            range = (attrStr.string as NSString).range(of: string, options: [], range: range)
            if (range.location != NSNotFound) {
                attrStr.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: NSRange(location: range.location, length: searchLength))
                attrStr.addAttribute(NSAttributedStringKey.font, value: UIFont(name: "Karla-Regular", size: 16.0)!, range: NSRange(location: range.location, length: searchLength))
                range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
                textView.attributedText = attrStr
            }
        }
    }
}

最佳答案

假设你在文中保留#@,你可以修改Larme评论中的答案:

let regex = try! NSRegularExpression(pattern: "(?:#|@)\\w+", options: [])

func textViewDidChange(_ textView: UITextView) {
    let attrStr = NSMutableAttributedString(attributedString: textView.attributedText ?? NSAttributedString())
    let plainStr = attrStr.string
    attrStr.addAttribute(.foregroundColor, value: UIColor.black, range: NSRange(0..<plainStr.utf16.count))

    let matches = regex.matches(in: plainStr, range: NSRange(0..<plainStr.utf16.count))

    for match in matches {
        let nsRange = match.range
        let matchStr = plainStr[Range(nsRange, in: plainStr)!]
        let color: UIColor
        if matchStr.hasPrefix("#") {
            color = .red
        } else {
            color = .blue
        }
        attrStr.addAttribute(.foregroundColor, value: color, range: nsRange)
    }

    textView.attributedText = attrStr
}

我只是更改了模式,适配了 Swift 4.1,修复了一些错误,删除了一些冗余代码并添加了一些更改颜色的代码。

关于ios - 用户界面 TextView /NSAttribute : Detect if word starts with a particular symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51231983/

相关文章:

arrays - 使用 swift 将初始化的数组包装到另一个数组中

ios - 将阴影添加到UITextView的框架中,还将阴影添加到文本中

IOS:在触摸 UITableView 时隐藏键盘

iOS App 阻止设备进入休眠模式

ios - 减缓 Push Segue 的转换

ios - NavigationBar 在 Show Detail segue 后不会消失

ios - 为什么 characterRange(at : . zero) == nil?

ios - Xcode - iPad 上的约束 - 横向与纵向

ios - 如何让图像覆盖整个UIView?

ios - 如何通过触摸单元格内的按钮快速编辑文本字段