I want to highlight one word of a sentence which is defined in xxx.strings file, such as highlight "Allow" in sentence "Please click Allow".
Asked
Active
Viewed 589 times
0
-
Yes. You can use AttributedString – Sahil Manchanda Jul 19 '18 at 04:06
-
1A `.strings` file is for localization and is a poor choice to store a list of words. You better use plain text and split by line, or plist file – Code Different Jul 19 '18 at 04:09
-
You can also use HTML in your strings to highlight text. Then you read it into a NSAttributedString using the init?(html:.... initialiser. – drekka Jul 19 '18 at 04:30
-
1Possible duplicate of [UITextView highlight all matches using swift](https://stackoverflow.com/questions/25525171/uitextview-highlight-all-matches-using-swift) – Venk Jul 19 '18 at 05:12
-
Possible duplicate of [Making text bold using attributed string in swift](https://stackoverflow.com/questions/28496093/making-text-bold-using-attributed-string-in-swift) – Sharad Chauhan Jul 19 '18 at 05:57
2 Answers
1
yes you can use NSAttributed String to apply different attributes to your string like
underline
let attributedString = NSMutableAttributedString(string: textString)
let range = (self.text! as NSString).range(of: "your string you want to highlight")
attributedString.addAttributes([NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue,NSAttributedStringKey.underlineColor : .white], range: range)
yourlabel.attributedText = attributedString
change string color(forground color)
let attributedString = NSMutableAttributedString(string: textString)
let range = (self.text! as NSString).range(of: "your string you want to highlight")
attributedString.addAttributes([NSAttributedStringKey.foregroundColor : UIColor.red], range: range)
yourlabel.attributedText = attributedString
change string color(background color)
let attributedString = NSMutableAttributedString(string: textString)
let range = (self.text! as NSString).range(of: "your string you want to highlight")
attributedString.addAttributes([NSAttributedStringKey.backgroundColor : UIColor.red], range: range)
yourlabel.attributedText = attributedString
change font
let attributedString = NSMutableAttributedString(string: textString)
let range = (self.text! as NSString).range(of: "your string you want to highlight")
attributedString.addAttributes([NSAttributedStringKey.font : fontyouwant], range: range)
yourlabel.attributedText = attributedString

Devil Decoder
- 966
- 1
- 10
- 26
0
Hello please refer below method:-
func underline() {
if let textString = self.text {
let attributedString = NSMutableAttributedString(string: textString)
let range = (self.text! as NSString).range(of: "your highlighted string")
attributedString.addAttributes([NSAttributedStringKey.underlineColor : .white], range: range)
attributedText = attributedString
}
}
hope it will help you, Thank you.

V D Purohit
- 1,179
- 1
- 10
- 23