Ios swift: Can I change the font style of a specific word in a string?

I am extracting a row from the contents of the DB. Using the method, I extract the longest word from this string.

Now I would like to print the entire line on a text label, but I would like to highlight the longest word in a different color and text styles within the line.

How can i do this? Do I need to cut the string into pieces - set the formatting - and put it all together before passing it to the label?

Or is there another (better) way?

+6
source share
3 answers

If you already know the longest word, you should get the range of that word in a string. For this, I prefer the NSString rangeOfString: method.

Then you create an NSMutableAttributedString from a string with your default attributes. Finally, you add selection attributes to the range that you defined earlier.

 let longString = "Lorem ipsum dolor. VeryLongWord ipsum foobar" let longestWord = "VeryLongWord" let longestWordRange = (longString as NSString).rangeOfString(longestWord) let attributedString = NSMutableAttributedString(string: longString, attributes: [NSFontAttributeName : UIFont.systemFontOfSize(20)]) attributedString.setAttributes([NSFontAttributeName : UIFont.boldSystemFontOfSize(20), NSForegroundColorAttributeName : UIColor.redColor()], range: longestWordRange) label.attributedText = attributedString 

What looks on my site:

enter image description here

+22
source

You want to see Attributed Strings and NSRange. You can use them together to create different styles for ranges in a row. Here is a snippet:

 myMutableString = NSMutableAttributedString(string: myString, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!]) //Add more attributes here: myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Chalkduster", size: 24.0), range: NSRange(location: 7,length: 5)) myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "AmericanTypewriter-Bold", size: 18.0)!, range: NSRange(location:2,length:4)) myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:4)) myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Georgia", size: 36.0)!, range: NSRange(location: 0, length: 1)) myMutableString.addAttribute(NSStrokeColorAttributeName, value: UIColor.blueColor(), range: NSRange(location: 0, length: 1)) myMutableString.addAttribute(NSStrokeWidthAttributeName, value: 2, range: NSRange(location: 0, length: 1)) myMutableString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location: 0, length: myString.length)) myLabel.backgroundColor = UIColor.grayColor() //Apply to the label myLabel.attributedText = myMutableString 
+2
source

NSMutableAttributedString.

You create an NSMutableAttributedString and apply the effects you want to use with addAttributes:range .

Then assign it to the attributedText property of your UILabel .

0
source

Source: https://habr.com/ru/post/984043/


All Articles