Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make part of a UILabel bold in Swift

I have a UILabel I've made programmatically as:

var label = UILabel() 

I've then declared some styling for the label, including a font, such as:

label.frame = CGRect(x: 20, y: myHeaderView.frame.height / 2, width: 300, height: 30) label.font = UIFont(name: "Typo GeoSlab Regular Demo", size: 15) label.textColor = UIColor(hue: 0/360, saturation: 0/100, brightness: 91/100, alpha: 1) 

The first part of the label will always read : "Filter:" then followed by another part of the string, for example, "Most popular"

I would like the word filter to be in bold, so the whole thing would look like:

Filter: Most popular

I want to simplest way of creating this effect. I've been searching the internet for how to achieve this and there are so many ways, some which just look like pages of code. And most of it seems to be in Objective-C. I would like it in Swift please :)

I don't know if i'm on the right lines, but is this what NSRange can help achieve? Thanks in advance

Update

I use a series of if statements to change my label variable. Such as:

if indexArray == 1 {      label.text = "Filter: Film name"  } else if indexArray == 2 {      label.text = "Filter: Most popular"  } else if indexArray == 3 {      label.text = "Filter: Star rating"  } 
like image 596
Nick89 Avatar asked Apr 07 '16 20:04

Nick89


People also ask

How do I make my text bold on UILabel?

Write the following text in the label “Bold Regular”Double Click on Bold to select it, and then right click on it to see more options. Select font > Bold from that option. It should do the task.

How do I change the UILabel text in Swift?

To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.


2 Answers

You will want to use attributedString which allows you to style parts of a string etc. This can be done like this by having two styles, one normal, one bold, and then attaching them together:

let boldText = "Filter:" let attrs = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 15)] let attributedString = NSMutableAttributedString(string:boldText, attributes:attrs)  let normalText = "Hi am normal" let normalString = NSMutableAttributedString(string:normalText)  attributedString.append(normalString) 

When you want to assign it to a label:

label.attributedText = attributedString 
like image 159
Joe Benton Avatar answered Oct 26 '22 18:10

Joe Benton


You can use NSMutableAttributedString and NSAttributedString to create customized string. The function below makes given boldString bold in given string.

Swift 3

func attributedText(withString string: String, boldString: String, font: UIFont) -> NSAttributedString {     let attributedString = NSMutableAttributedString(string: string,                                                      attributes: [NSFontAttributeName: font])     let boldFontAttribute: [String: Any] = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: font.pointSize)]     let range = (string as NSString).range(of: boldString)     attributedString.addAttributes(boldFontAttribute, range: range)     return attributedString } 

Example usage

authorLabel.attributedText = attributedText(withString: String(format: "Author : %@", user.name), boldString: "Author", font: authorLabel.font) 

Swift 4

func attributedText(withString string: String, boldString: String, font: UIFont) -> NSAttributedString {     let attributedString = NSMutableAttributedString(string: string,                                                      attributes: [NSAttributedStringKey.font: font])     let boldFontAttribute: [NSAttributedStringKey: Any] = [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: font.pointSize)]     let range = (string as NSString).range(of: boldString)     attributedString.addAttributes(boldFontAttribute, range: range)     return attributedString } 

Swift 4.2 and 5

func attributedText(withString string: String, boldString: String, font: UIFont) -> NSAttributedString {     let attributedString = NSMutableAttributedString(string: string,                                                  attributes: [NSAttributedString.Key.font: font])     let boldFontAttribute: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: font.pointSize)]     let range = (string as NSString).range(of: boldString)     attributedString.addAttributes(boldFontAttribute, range: range)     return attributedString } 
like image 32
abdullahselek Avatar answered Oct 26 '22 18:10

abdullahselek