如何使用Swift創建帶屬性的字符串?含代碼

如何使用Swift創建帶屬性的字符串?

本文將解釋如何在Swift語言中創建帶屬性的字符串。愛掏網 - it200.com在Swift中,要對字符串應用不同的屬性,需要進行哪些步驟?

在Swift中,我們使用NSAttributedString類來創建帶屬性的字符串。愛掏網 - it200.com

在Swift中,NSAttributedString是一個用于創建和管理帶屬性的字符串的類。愛掏網 - it200.com帶屬性的字符串是一個在字符串的部分文本上應用了額外屬性(如文字顏色、字體和樣式)的字符串。愛掏網 - it200.com

本文將展示帶屬性的字符串的不同用例。愛掏網 - 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
    }
}

解釋

在上面的代碼中,我們設置了一個名為TestController的視圖控制器,用于為UILabel類顯示不同的屬性文本。愛掏網 - it200.com

輸出

如何給整個字符串應用顏色?

在這個例子中,您將看到如何給整個字符串應用顏色。愛掏網 - it200.com以下是使用不同屬性在Swift中創建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
}

輸出

如何對整個字符串應用不同的顏色和字體樣式?

在這個示例中,您將看到如何對字符串應用不同的顏色和字體樣式的示例。愛掏網 - it200.com以下是在Swift中創建帶有不同屬性的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
}

輸出

如何對整個字符串應用行間距?

在許多情況下,您必須對字符串應用一些行間距以正確顯示多行文本。愛掏網 - it200.comSwift提供了NSMutableParagraphStyle類來添加行之間的間距。愛掏網 - it200.com以下是如何在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
}

輸出

結論

在真實的iOS應用中,屬性字符串是一個非常有用和常用的功能。愛掏網 - it200.com你可以對字符串應用不同的樣式。愛掏網 - it200.com同時,你還可以對子字符串應用不同的樣式。愛掏網 - it200.com

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 老司机午夜性大片免费| 久久久受www免费人成| 91香蕉视频污| 污污污污污污www网站免费| 小兔子好大从衣服里跳出来| 国产中文字幕在线观看| 久久久久久久97| 草莓app下载2019年| 无码精品尤物一区二区三区| 国产一区二区三区日韩精品| 久久久无码精品亚洲日韩蜜桃| 饥渴难耐16p| 日本边添边摸边做边爱的网站| 国产在线精品国自产拍影院同性 | 娜露温泉无删减视频在线看| 农民工嫖妓50岁老熟女| а√在线地址最新版| 看久久久久久a级毛片| 女欢女爱第一季| 人人妻人人澡人人爽人人精品 | 色猫咪av在线网址| 成年人免费黄色| 免费a级毛片出奶水| a级毛片免费观看网站| 毛片基地在线观看| 国产精品久久久久鬼色| 乱人伦精品视频在线观看| 高清国语自产拍免费视频国产| 日本护士xxx| 又粗又大又猛又爽免费视频| v一区无码内射国产| 欧美激情一区二区三区中文字幕| 国产精品…在线观看| 久久伊人中文字幕| 精品无码久久久久久久久| 天天爱天天做天天爽夜夜揉| 亚洲欧洲日产国码一级毛片| 国产精品亚洲四区在线观看 | 精品一区二区三区色花堂| 在线观看亚洲免费视频| 亚洲一区无码中文字幕|