iOS swift 带有attributeString的多行文本label

class AttributeStringGenerator {

    var attributeString: NSMutableAttributedString!

    var lineSpacing: CGFloat = 2

    init() {

        attributeString = NSMutableAttributedString()

    }

    

    func reset() {

        attributeString = NSMutableAttributedString()

    }

    

    /// 添加空行

    func addEmptyLine(height: CGFloat = 12) {

        let font = UIFont.systemFont(ofSize: height)

        let attr = NSAttributedString(string: "\n", attributes:  [NSAttributedString.Key.font:font])

        attributeString.append(attr)

    }

    

    /// 加一行文字

    ///

    /// - Parameters:

    ///   - string: 要加的文字

    ///   - alignment: 对齐方式

    ///   - color: 颜色

    ///   - font: 字体

    ///   - isLastLine: 是否最后一行,最后一行不加换行

    func addLine(string: String, alignment: NSTextAlignment, color: UIColor = UIColor.white, font: UIFont = UIFont.systemFont(ofSize: 12), isLastLine: Bool = false) {

        let para = NSMutableParagraphStyle()

        para.alignment = alignment

        

        para.lineSpacing = lineSpacing

        

        //如果是最后一行,不需要加 \n

        var s = string

        if !isLastLine {

            s += "\n"

        }

        

        let attr = NSAttributedString(string: s, attributes:

            [NSAttributedString.Key.paragraphStyle:para,

             NSAttributedString.Key.foregroundColor:color,

             NSAttributedString.Key.font:font])

        

        attributeString.append(attr)

    }

    

    

    /// 添加文字居中带图标的行

    ///

    /// - Parameters:

    ///   - icon: 图标

    ///   - string: 文字

    ///   - size: 图标大小

    ///   - color: 文字颜色

    ///   - font: 字体

    ///   - isLastLine: 是否最后一行

    func addLineWith(icon: UIImage,string: String, size: CGSize, color: UIColor = UIColor.white, font: UIFont = UIFont.systemFont(ofSize: 18), isLastLine: Bool = true) {

        let para = NSMutableParagraphStyle()

        para.alignment = .center

        para.lineSpacing = lineSpacing

        //如果是最后一行,不需要加 \n

        var s = string

        if !isLastLine {

            s += "\n"

        }

        

        let att = NSTextAttachment(data: nil, ofType: nil)

        att.image = icon

        att.bounds = CGRect(x: 0, y: -font.pointSize/10, width: size.width, height: size.height)

        let attr = NSMutableAttributedString(attributedString: NSAttributedString(attachment: att))

        

        attr.append(NSMutableAttributedString(string: " " + s, attributes:

            [NSAttributedString.Key.foregroundColor:color,

             NSAttributedString.Key.font:font]))

        attr.addAttribute(NSAttributedString.Key.paragraphStyle, value: para, range: NSMakeRange(0, attr.length))

        

        attributeString.append(attr)

    }

    

    

    

    

    /// 添加一行文字,分左中右三部分, 左边的左对齐, 中间的右对齐,可以设置通过某符号对齐,比如 ":", 右边的可以自行设置对齐方式

    ///

    /// - Parameters:

    ///   - leftString: 左边的文字

    ///   - leftColor: 左边文字的颜色

    ///   - leftFont: 左边文字字体

    ///   - centerString: 中间的文字

    ///   - centerPosition: 中间文字的位置

    ///   - centerColor: 中间文字颜色

    ///   - centerFont: 中间文字字体

    ///   - alignString: 中间文字对齐的符号

    ///   - rightString: 右边的文字

    ///   - rightPosition: 右边文字的位置

    ///   - rightColor: 右边文字颜色

    ///   - rightFont: 右边文字字体

    ///   - alignment: 右边文字对齐方式

    func addLine(leftString: String?, leftColor: UIColor = UIColor.white, leftFont: UIFont = UIFont.systemFont(ofSize: 12),

                 centerString: String? = nil,centerPosition:CGFloat = 0, centerColor: UIColor = UIColor.white, centerFont: UIFont = UIFont.systemFont(ofSize: 12), alignString: String = "",

                 rightString: String?,rightPosition:CGFloat = 0, rightColor: UIColor = UIColor.white, rightFont: UIFont = UIFont.systemFont(ofSize: 12), alignment: NSTextAlignment = .right,

                 isLastLine: Bool = false) {

        var string = ""

        var leftRange: NSRange

        var centerRange: NSRange

        var rightRange: NSRange

        

        if let left = leftString {

            string = string + left + "\t"

            //特殊字符.count算出的长度不对,需要改成 lengthOfBytes(using: String.Encoding.unicode)/2

            leftRange = NSMakeRange(0, string.lengthOfBytes(using: String.Encoding.unicode)/2)

        } else {

            string += "\t"

            leftRange = NSMakeRange(0, 1)

        }

        

        if let center = centerString {

            string = string + center + "\t"

            centerRange = NSMakeRange(leftRange.length, center.lengthOfBytes(using: String.Encoding.unicode)/2 + 1)

        } else {

            //string += "\t"

            centerRange = NSMakeRange(leftRange.length, 0)

        }

        

        if let right = rightString {

            if isLastLine {

                string = string + right

                rightRange = NSMakeRange(leftRange.length + centerRange.length, right.lengthOfBytes(using: String.Encoding.unicode)/2)

            } else {

                string = string + right + "\n"

                rightRange = NSMakeRange(leftRange.length + centerRange.length, right.lengthOfBytes(using: String.Encoding.unicode)/2 + 1)

            }

            

        } else {

            if isLastLine {

                string += "\n"

                rightRange = NSMakeRange(leftRange.length + centerRange.length, 1)

            } else {

                rightRange = NSMakeRange(leftRange.length + centerRange.length, 0)

            }

            

        }

        

        let para = NSMutableParagraphStyle()

        

        para.lineSpacing = lineSpacing

        let align = NSCharacterSet(charactersIn: alignString)

        

        let t1 = NSTextTab(textAlignment: .right, location: centerPosition, options: [NSTextTab.OptionKey.columnTerminators:align])

        let t2 = NSTextTab(textAlignment: alignment, location: rightPosition, options: [:])

        para.tabStops = [t1,t2]

        

        if centerString == nil {

            para.tabStops = [t2]

        }

        

        let attr = NSMutableAttributedString(string: string)

        attr.addAttribute(NSAttributedString.Key.paragraphStyle, value: para, range: NSMakeRange(0, string.count))

        

        attr.addAttributes([NSAttributedString.Key.foregroundColor:leftColor, NSAttributedString.Key.font:leftFont], range: leftRange)

        attr.addAttributes([NSAttributedString.Key.foregroundColor:centerColor, NSAttributedString.Key.font:centerFont], range: centerRange)

        attr.addAttributes([NSAttributedString.Key.foregroundColor:rightColor, NSAttributedString.Key.font:rightFont], range: rightRange)

        attributeString.append(attr)

    }

    

    func getHeight(width:CGFloat) -> CGFloat {

        let options : NSStringDrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading]

        let bounds = attributeString.boundingRect(with: CGSize(width: width, height: 99999), options: options, context: nil)

        return bounds.height

    }

}

最后将attributeString赋值给label

猜你喜欢

转载自www.cnblogs.com/duzhaoquan/p/10393975.html