Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Range of substring in NSMutableAttributedString

I have AttributedString with emoji like this "🤣🤣🤣 @Mervin tester 🤣🤣🤣"

Now I need to find a range of Mervin in this attributed String.

let attributedString = NSMutableAttributedString(string: "🤣🤣🤣 @Mervin tester 🤣🤣🤣")

let range = // range for "Mervin" in above String. 

Thank you.

like image 777
Ujesh Avatar asked Jun 29 '17 08:06

Ujesh


1 Answers

This extension should help you.

extension NSAttributedString {
    func rangeOf(string: String) -> Range<String.Index>? {
        return self.string.range(of: string)
    }
}

Usage:

attributedString.rangeOf(string: "Mervin")
like image 99
Oleg Gordiichuk Avatar answered Dec 31 '22 23:12

Oleg Gordiichuk