如何使用Swift創(chuàng)建帶屬性的字符串?含代碼

    如何使用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

    聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
    發(fā)表評論
    更多 網(wǎng)友評論0 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 中文字幕精品亚洲无线码一区应用 | 乱色熟女综合一区二区三区| 中文字幕精品一区二区日本| 国产综合一区二区在线观看| 久久精品一区二区免费看| 亚洲一区二区在线视频| 精品无码一区二区三区在线| 波多野结衣AV一区二区三区中文 | 波多野结衣一区二区三区aV高清| 综合久久一区二区三区 | 蜜桃传媒一区二区亚洲AV| 国产三级一区二区三区 | 亚洲乱码av中文一区二区| 国产精品成人免费一区二区| 精品国产日韩亚洲一区| 亚洲影视一区二区| 日本在线视频一区| 亚洲图片一区二区| 日本一区二区三区在线观看视频 | 国产高清视频一区三区| 国产精品视频一区二区猎奇| 国产一区二区精品久久岳√| 国产激情з∠视频一区二区 | 亚洲色大成网站www永久一区| 亚洲一区二区三区免费在线观看| 美女免费视频一区二区| 无码乱人伦一区二区亚洲| 日本一区二区三区不卡视频中文字幕 | 国产成人精品一区二三区熟女| 国产精品一区二区三区99| 亚洲国产欧美一区二区三区 | 成人免费视频一区二区三区| 亲子乱AV视频一区二区| 日本韩国一区二区三区| 亚洲电影一区二区| AV无码精品一区二区三区宅噜噜 | 国产亚洲无线码一区二区 | 国产高清在线精品一区小说| 麻豆亚洲av熟女国产一区二| 国产一区二区三区夜色| 国产在线观看一区二区三区|