如何使用Swift創(chuàng)建帶屬性的字符串?
本文將解釋如何在Swift語言中創(chuàng)建帶屬性的字符串。愛掏網(wǎng) - it200.com在Swift中,要對字符串應(yīng)用不同的屬性,需要進(jìn)行哪些步驟?
在Swift中,我們使用NSAttributedString類來創(chuàng)建帶屬性的字符串。愛掏網(wǎng) - it200.com
在Swift中,NSAttributedString是一個(gè)用于創(chuàng)建和管理帶屬性的字符串的類。愛掏網(wǎng) - it200.com帶屬性的字符串是一個(gè)在字符串的部分文本上應(yīng)用了額外屬性(如文字顏色、字體和樣式)的字符串。愛掏網(wǎng) - it200.com
本文將展示帶屬性的字符串的不同用例。愛掏網(wǎng) - it200.com
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
}
}
解釋
在上面的代碼中,我們設(shè)置了一個(gè)名為TestController的視圖控制器,用于為UILabel類顯示不同的屬性文本。愛掏網(wǎng) - it200.com
輸出
如何給整個(gè)字符串應(yīng)用顏色?
在這個(gè)例子中,您將看到如何給整個(gè)字符串應(yīng)用顏色。愛掏網(wǎng) - it200.com以下是使用不同屬性在Swift中創(chuàng)建NSAttributedString對象的示例 –
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
}
輸出
如何對整個(gè)字符串應(yīng)用不同的顏色和字體樣式?
在這個(gè)示例中,您將看到如何對字符串應(yīng)用不同的顏色和字體樣式的示例。愛掏網(wǎng) - it200.com以下是在Swift中創(chuàng)建帶有不同屬性的NSAttributedString對象的示例-
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
}
輸出
如何對整個(gè)字符串應(yīng)用行間距?
在許多情況下,您必須對字符串應(yīng)用一些行間距以正確顯示多行文本。愛掏網(wǎng) - it200.comSwift提供了NSMutableParagraphStyle類來添加行之間的間距。愛掏網(wǎng) - it200.com以下是如何在Swift中將段落樣式應(yīng)用于字符串的示例?
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
}
輸出
結(jié)論
在真實(shí)的iOS應(yīng)用中,屬性字符串是一個(gè)非常有用和常用的功能。愛掏網(wǎng) - it200.com你可以對字符串應(yīng)用不同的樣式。愛掏網(wǎng) - it200.com同時(shí),你還可以對子字符串應(yīng)用不同的樣式。愛掏網(wǎng) - it200.com