Created
August 27, 2015 19:14
-
-
Save ajjames/fdceed48b37a4ee16fc6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension NSMutableAttributedString | |
| { | |
| func replace(target:String, with replacement:String, attributes:[String:NSObject]?) | |
| { | |
| var text = self.string | |
| var error:NSError? | |
| var regex = NSRegularExpression(pattern:target, options:.IgnoreMetacharacters, error:nil) | |
| var range = NSMakeRange(0, count(text)) | |
| regex?.enumerateMatchesInString(text, options:nil, range: range, usingBlock: { | |
| (result:NSTextCheckingResult!, flags:NSMatchingFlags, stop:UnsafeMutablePointer<ObjCBool>) -> Void in | |
| let subStringRange = result.rangeAtIndex(0) | |
| let attributedString = NSAttributedString(string:replacement, attributes:attributes) | |
| self.replaceCharactersInRange(subStringRange, withAttributedString:attributedString) | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment