Quantcast
Viewing latest article 6
Browse Latest Browse All 11

How to find a range of a String in another String in swift

I tried to use NSMutableAttributedString to show a reach text in UILabel, but I've found it's hard to got a method to find out the range of the-string inside of a string. Here is what that I've done.hope this helped you!
let attrString = NSMutableAttributedString(string:desc)
        if let strRange =  desc.rangeOfString(stringInSearchBar){
            let distanceBegin = distance(desc.startIndex, strRange.startIndex)
            let distanceEnd = distance(desc.startIndex, strRange.endIndex)
            let range = NSMakeRange(distanceBegin, distanceEnd - distanceBegin)
            attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.markColorRed(), range:range)
        }
Image may be NSFW.
Clik here to view.


何清龙 2015-01-14 12:01 发表评论

Viewing latest article 6
Browse Latest Browse All 11

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>