0

If I create a label like this:

var label = UILabel(...)
label.text = first_name + ", " + age
self.view.addSubview(label)

How can I change the age of the label to bold, but keep the first_name normal weight?

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • You need to use NSAttributedString which is a special string that lets you makes pieces of it look different (i.e. bolding)...then set the label.attributedText instead – DBoyer Aug 01 '15 at 22:30

1 Answers1

7

You can use an attributed string. Here, the bold size is 15, but you can also change that point size if you want.

var age = "13"

var att = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15)]

var boldAge = NSMutableAttributedString(string:age, attributes:att)

// Assigning to a UILabel
yourLabel.attributedText = boldAge

so now you would want to assign your text like this:

label.attributedText = first_name + ", " + boldAge
Cole
  • 2,641
  • 1
  • 16
  • 34
  • I think the code has been slightly changed. Here is the updated version: ``` var normalText = "Normal Text " var boldText = " Bold Text" var attributedStringNormal = NSMutableAttributedString(string:normalText) var attrs = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 15)] var boldString = NSMutableAttributedString(string: boldText, attributes:attrs) attributedStringNew.append(boldString) ``` – Ahmed Ashraf Butt Aug 25 '22 at 00:37