如何使用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 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 无码乱码av天堂一区二区| 国产精品视频免费一区二区| 久久精品国产第一区二区三区| 日韩精品无码一区二区中文字幕| 无码精品人妻一区二区三区人妻斩| 无码aⅴ精品一区二区三区| 亚洲综合无码一区二区三区| 国产一区二区视频在线观看| 精品一区二区三区水蜜桃| 天堂成人一区二区三区| 亚洲AV无码一区二区三区在线| 精品一区二区久久久久久久网站| 一区二区免费电影| 国产精品被窝福利一区| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 亚洲日韩AV一区二区三区四区| 国产精品伦一区二区三级视频| 国产精品被窝福利一区 | 99久久人妻精品免费一区 | 亚洲AV成人精品日韩一区18p| 男人的天堂av亚洲一区2区| 国产精品高清视亚洲一区二区| 亚洲线精品一区二区三区| 免费一区二区无码东京热| 中文字幕一区二区免费| 成人精品视频一区二区三区| 自慰无码一区二区三区| 熟女少妇精品一区二区| 亚洲一区二区三区AV无码| 国产在线aaa片一区二区99| 又硬又粗又大一区二区三区视频 | 亚洲精品伦理熟女国产一区二区| 久久久久人妻精品一区二区三区| 成人h动漫精品一区二区无码| 无码视频一区二区三区| 亚洲国产一区二区视频网站| 精品无码人妻一区二区三区不卡 | 国产欧美色一区二区三区| 日韩精品免费一区二区三区| 福利一区福利二区| 午夜福利一区二区三区高清视频|