前语:AttributedString是Apple推出的能够完成单个字符或字符规模带相应特点的字符串。特点供给了一些文本特性,能够让文本展示的样式愈加丰富。在日常开发过程中,我通常用于同一个Label中包括不同的字体巨细或字体色彩的样式编写中。

运用举例

需求:需求设置一个红底白字的Label

attributedLabel = UILabel()
let contentStr = NSString(string: "AttributedString")
let attStr = NSMutableAttributedString(string: contentStr as String)
attStr.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 20, weight: .medium), range: NSRange(location: 0, length: contentStr.length))
attStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.white, range: NSRange(location: 0, length: contentStr.length))
attStr.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.red, range: NSRange(location: 0, length: contentStr.length))
attributedLabel.attributedText = attStr

样式展示:

「Swift」AttributedString常见运用办法

文本特点介绍

从上方代码能够看出,文本的特点是经过设置文本字体,文本色彩,文本背景色彩所完成的。所以下面来一一列举一些常用的文本特点及展示作用。

  • 设置文本字体巨细和粗细
attStr.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 20, weight: .medium), range: NSRange(location: 0, length: contentStr.length))

作用:

「Swift」AttributedString常见运用办法

  • 设置文本色彩
attStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: NSRange(location: 0, length: contentStr.length))

作用:

「Swift」AttributedString常见运用办法

  • 设置背景色彩
attStr.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.red, range: NSRange(location: 0, length: contentStr.length))

作用:

「Swift」AttributedString常见运用办法

  • 设置下划线
attStr.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: contentStr.length))

作用:

「Swift」AttributedString常见运用办法

  • 设置下划线色彩

默认下划线色彩与文本色彩相同

attStr.addAttribute(NSAttributedString.Key.underlineColor, value: UIColor.green, range: NSRange(location: 0, length: contentStr.length))

作用:

「Swift」AttributedString常见运用办法

  • 拼接文本

先设置好相关文本特点,然后将其相互连接

let contentStr1 = NSString(string: "Attributed")
let attStr1 = NSMutableAttributedString(string: contentStr1 as String)
attStr1.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 20, weight: .medium), range: NSRange(location: 0, length: contentStr1.length))
attStr1.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: NSRange(location: 0, length: contentStr1.length))
let contentStr2 = NSString(string: "String")
let attStr2 = NSMutableAttributedString(string: contentStr2 as String)
attStr2.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12, weight: .medium), range: NSRange(location: 0, length: contentStr2.length))
attStr2.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.green, range: NSRange(location: 0, length: contentStr2.length))
attStr1.append(attStr2)
attributedLabel.attributedText = attStr1

作用:

「Swift」AttributedString常见运用办法

Attributes创立及运用

attributes能够一次性创立多个特点,attributes是一个字典

需求:创立一个红底白字的Label

let contentStr = NSString(string: "AttributedString")
let attStr = NSMutableAttributedString(string: contentStr as String)
attStr.addAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20, weight: .regular),
                      NSAttributedString.Key.foregroundColor : UIColor.white,
                      NSAttributedString.Key.backgroundColor : UIColor.red], range: NSRange(location: 0, length: contentStr.length))
attributedLabel.attributedText = attStr

作用:

「Swift」AttributedString常见运用办法

常用特点办法整合

我将常用的一些文本特点进行整合了一个类

import Foundation
import UIKit
public struct ZUAttributedString {
    public enum Font:String {
        case thin = "PingFangSC-Thin"
        case light = "PingFangSC-Light"
        case medium = "PingFangSC-Medium"
        case regular = "PingFangSC-Regular"
    }
    public enum Line{
        case none
        case midLine
        case underLine
    }
    public static func attributeString(content:String,
                                font: UIFont,
                                alignment:NSTextAlignment? = NSTextAlignment.center,
                                textColor:UIColor?,
                                backgroundColor: UIColor? = nil,
                                line:Line = .none,
                                lineSpacing:CGFloat = 0) -> NSMutableAttributedString
    {
        let contentStr = NSString(string: content)
        let attStr = NSMutableAttributedString(string: contentStr as String)
        //set color
        if let textColor = textColor {
            attStr.addAttribute(NSAttributedString.Key.foregroundColor, value: textColor, range: NSRange(location: 0, length: contentStr.length))
        }
        if let backgroundColor = backgroundColor {
            attStr.addAttribute(NSAttributedString.Key.backgroundColor, value: backgroundColor, range: NSRange(location: 0, length: contentStr.length))
        }
        let style = NSMutableParagraphStyle()
        if let align = alignment {
            style.alignment = align
        } else {
            style.alignment = NSTextAlignment.center
        }
        if lineSpacing > 0{
            style.lineSpacing = lineSpacing
        }
        attStr.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: NSRange(location: 0, length: contentStr.length))
        attStr.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: 0, length: contentStr.length))
        switch line{
        case .none:
            break
        case .midLine:
            attStr.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attStr.length))
            break
        case .underLine:
            attStr.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attStr.length))
            break
        }
        return attStr
    }
    public static func attributeString(content:String,
                                font:ZUAttributedString.Font,
                                size:CGFloat,
                                alignment:NSTextAlignment? = NSTextAlignment.center,
                                textColor:UIColor?,
                                backgroundColor:UIColor? = nil,
                                line:Line = .none,
                                maximumLineHeight:CGFloat) -> NSMutableAttributedString
    {
        let attStr = NSMutableAttributedString(string: content)
        //set color
        if let textColor = textColor {
            attStr.addAttribute(NSAttributedString.Key.foregroundColor, value: textColor, range: NSRange(location: 0, length: content.count))
        }
        if let backgroundColor = backgroundColor {
            attStr.addAttribute(NSAttributedString.Key.backgroundColor, value: backgroundColor, range: NSRange(location: 0, length: content.count))
        }
        let style = NSMutableParagraphStyle()
        if let align = alignment {
            style.alignment = align
        } else {
            style.alignment = NSTextAlignment.center
        }
        attStr.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: NSRange(location: 0, length: content.count))
        //set font
        switch font {
        case .thin:
            let font = UIFont(name: ZUAttributedString.Font.thin.rawValue, size: size) ?? UIFont.systemFont(ofSize: size)
            attStr.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: 0, length: content.count))
        case .light:
            let font = UIFont(name: ZUAttributedString.Font.light.rawValue, size: size) ?? UIFont.systemFont(ofSize: size)
            attStr.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: 0, length: content.count))
            break
        case .medium:
            let font = UIFont(name: ZUAttributedString.Font.medium.rawValue, size: size) ?? UIFont.systemFont(ofSize: size)
            attStr.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: 0, length: content.count))
            break
        case .regular:
            let font = UIFont(name: ZUAttributedString.Font.regular.rawValue, size: size) ?? UIFont.systemFont(ofSize: size)
            attStr.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: 0, length: content.count))
            break
        }
        switch line{
        case .none:
            break
        case .midLine:
            attStr.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attStr.length))
            break
        case .underLine:
            attStr.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attStr.length))
            break
        }
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.maximumLineHeight = maximumLineHeight
        // Line spacing attribute
        attStr.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attStr.length))
        return attStr
    }
}

办法运用:

let attStr1 = ZUAttributedString.attributeString(content: "Attributed", font: UIFont.systemFont(ofSize: 20, weight: .regular), textColor: UIColor.green, backgroundColor: nil)
let attStr2 = ZUAttributedString.attributeString(content: "String", font: UIFont.systemFont(ofSize: 20, weight: .regular), textColor: UIColor.blue, backgroundColor: nil)
attStr1.append(attStr2)

作用:

「Swift」AttributedString常见运用办法

所以经过办法整合的方式,能够愈加便利快捷的运用AttributedString,而且也能够更好的完成咱们方针的文本样式

参阅文章

iOS swift 带有attributeString的多行文本label

Swift生成特点文本AttributedString

如果该文章对你有所协助,能够点赞、收藏而且重视一下! 后续会持续更新更多技术内容