eliot1019 / AttrTextView

A custom UITextView class supporting clickable hashtags and mentions
https://medium.com/@eliothan/clickable-hashtags-and-mentions-in-swift-3-0-c627c7d3dd9d#.g9svfm36d
MIT License
25 stars 5 forks source link

Doesn't detect all hashtags #3

Open StackHelp opened 5 years ago

StackHelp commented 5 years ago

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 ?

halilibrahimsengul commented 5 years ago

i had also same problem and see that words.filter filtering same words and i do like that and it's worked for me - Change this

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)
}