Created
September 7, 2015 09:06
-
-
Save Nirma/34f285fabdb265861f69 to your computer and use it in GitHub Desktop.
Simple highlighting with special characters
This file contains 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
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