Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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(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