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" }
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.
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.
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
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With