
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Make Attributed String Using Swift
This article will explain how you can make an attributed string in the Swift language. What are the steps to apply different attributes to a string in Swift?
In Swift, we use the NSAttributedString class to make an attributed string.
In Swift, NSAttributedString is a class used to create and manage attributed strings. An attributed string is a string that has additional attributes, such as text color, font, and style, applied to parts of the string.
This article will show different use cases for an attributed string.
Basic Setup
import UIKit class TestController: UIViewController { private let attributedLabel = UILabel() override func viewDidLoad() { super.viewDidLoad() initialSetup() } private func initialSetup() { // basic setup view.backgroundColor = .white navigationItem.title = "NSAttributedString" // attributedLabel customization attributedLabel.numberOfLines = 0 attributedLabel.backgroundColor = UIColor(white: 0, alpha: 0.1) view.addSubview(attributedLabel) attributedLabel.translatesAutoresizingMaskIntoConstraints = false attributedLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30).isActive = true attributedLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true attributedLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 30).isActive = true attributedLabel.heightAnchor.constraint(equalToConstant: 300).isActive = true } }
Explanation
In the above code, we set up a view controller named TestController to display different attributed text for a UILabel class.
Output

How to apply color to a full string?
In this example, you will see how you can apply color to a full string. Here is an example of how you can create an NSAttributedString object with different attributes in Swift ?
private func example1() { let string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book." let attributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.red, let attributedString = NSAttributedString(string: string, attributes: attributes) attributedLabel.attributedText = attributedString }
Output

How to apply different colors and font styles to a full string?
In this example, you will see an example of how to apply different colors and font styles to a string. Here is an example of how you can create an NSAttributedString object with different attributes in Swift ?
private func example2() { // first part let attributedString = NSMutableAttributedString(string: "This is the first line of black color. We're not applying any attribute to this part of string.", attributes: [.foregroundColor: UIColor.black, font: UIFont.systemFont(ofSize: 17)]) // appending new lines attributedString.append(NSAttributedString(string: "\n\n")) // second part attributedString.append(NSAttributedString(string: "This part will be in Red color and bold style in the string.", attributes: [.foregroundColor: UIColor.red, .font: UIFont.systemFont(ofSize: 17, weight: .bold)])) // appending new lines attributedString.append(NSAttributedString(string: "\n\n")) // third part attributedString.append(NSAttributedString(string: "This part will be in Brown color and underline style in the string.", attributes: [.foregroundColor: UIColor.brown, .font: UIFont.systemFont(ofSize: 17), .underlineStyle: 1])) attributedLabel.attributedText = attributedString }
Output

How to apply line spacing to a full string?
In many cases, you must apply some line spacing to a string to properly display multiline text. Swift provides the NSMutableParagraphStyle class to add spacing between lines. Here is an example of how you can apply a paragraph style to a string in Swift ?
private func example3() { let string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book." let paragraph = NSMutableParagraphStyle() paragraph.lineSpacing = 7 let attributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.red, .font: UIFont.systemFont(ofSize: 17), .paragraphStyle: paragraph] let attributedString = NSAttributedString(string: string, attributes: attributes) attributedLabel.attributedText = attributedString }
Output

Conclusion
In real iOS applications, attributed strings are a very useful and commonly used feature. You can apply different styles to a string. Also, you can apply various styles to substrings.