Skip to content

Instantly share code, notes, and snippets.

@Nirma
Created September 7, 2015 09:06
Show Gist options
  • Save Nirma/34f285fabdb265861f69 to your computer and use it in GitHub Desktop.
Save Nirma/34f285fabdb265861f69 to your computer and use it in GitHub Desktop.
Simple highlighting with special characters
func highlighted(originalString: String, delimiter:[String]) -> NSMutableAttributedString? {
if delimiter.count != 2 {
return nil
}
let highlightedAttributes = [NSForegroundColorAttributeName: UIColor.redColor()]
let searchString = originalString.componentsSeparatedByString(delimiter[0])
var attributedResult = NSMutableAttributedString()
for subString in searchString {
let subComponents = subString.componentsSeparatedByString(delimiter[1])
if subComponents.count > 1 {
let coloredKeywordString = NSMutableAttributedString(string: subComponents[0], attributes: highlightedAttributes)
attributedResult.appendAttributedString(coloredKeywordString)
attributedResult.appendAttributedString(NSAttributedString(string: subComponents[1]))
} else {
attributedResult.appendAttributedString(NSAttributedString(string: subComponents[0]))
}
}
return attributedResult
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment