How to make part of a line bold in iOS?

I want to make part of the text string bold.

For example: It must be bold. This is a normal line.

On Android, it's easy to achieve using spannable strings. What is its equivalent in iOS?

+6
source share
4 answers

Yes, this can be achieved using NSAttributedString :

 NSString *yourString = @"This is to be bold. This is normal string."; NSMutableAttributedString *yourAttributedString = [[NSMutableAttributedString alloc] initWithString:yourString]; NSString *boldString = @"This is to be bold"; NSRange boldRange = [yourString rangeOfString:boldString]; [yourAttributedString addAttribute: NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:boldRange]; [yourLabel setAttributedText: yourAttributedString]; 
+18
source
 NSString *text = @"Hello"; NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text]; [attributedText addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13.0] range:NSMakeRange(0, text.length-2)]; 
+1
source

You can achieve this using NSMutableAttributedString .

Refer Apple docs about NSMutableAttributedString .

I tried this answer to get bold and plain font in UILabel.

0
source

Quick version:

 @IBOutlet var theLabel: UILabel! @IBOutlet var theTextview: UITextView! let theString = "Please satisfy three of the following password conditions" as NSString let theAttributedString = NSMutableAttributedString(string: theString as String) let boldString = "three" let boldRange = theString.range(of: boldString) let font = UIFont.boldSystemFont(ofSize: 20) theAttributedString.addAttribute(NSFontAttributeName, value: font, range: boldRange) theLabel.attributedText = theAttributedString theTextview.attributedText = theAttributedString 
0
source

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


All Articles