Open StackHelp opened 5 years ago
for word in words.filter({$0.hasPrefix(wordPrefix)}) { let range = textString!.range(of: word) attrString?.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range) attrString?.addAttribute(NSAttributedString.Key(rawValue: attrName), value: 1, range: range) attrString?.addAttribute(NSAttributedString.Key(rawValue: "Clickable"), value: 1, range: range) attrString?.addAttribute(NSAttributedString.Key.font, value: font, range: range) }
--- with this---
do {
let nsText = textString
let regex = try NSRegularExpression(pattern: "(@[A-Za-z0-9-._]*)", options: [])
for match in regex.matches(in: text, options: [], range: NSRange(location: 0, length: nsText!.length)) {
print(match.range)
print(nsText!.substring(with: match.range))
let range = match.range
attrString?.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
attrString?.addAttribute(NSAttributedString.Key(rawValue: attrName), value: 1, range: range)
attrString?.addAttribute(NSAttributedString.Key(rawValue: "Clickable"), value: 1, range: range)
attrString?.addAttribute(NSAttributedString.Key.font, value: font, range: range)
}
} catch {
//handle error
print(error)
}
I have this kind of string
"#beautiful", "#travelling", "#goodlife", "#beautiful", "#travelling", "#goodlife", "#beautiful", "#travelling", "#goodlife", "#beautiful", "#travelling", "#goodlife", "#beautiful", "#travelling", "#goodlife", "#beautiful", "#travelling", "#goodlife", "#beautiful", "#travelling", "#goodlife", "#beautiful", "#travelling", "#goodlife", "#beautiful", "#travelling", "#goodlife"
. This textview detects only first 3 hashtags and other doesn't detect at all. Any reason behind it ?