宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
//  AttributedStringbuilder.swift
//  qrcode
//
//  Created by Chen, Harry (CHE-MLP) on 2017/11/15.
//  Copyright © 2017年 Chen, Harry (CHE-MLP). All rights reserved.
//
 
import UIKit
 
class AttributedStringbuilder: NSObject {
    
    var mutableAttributedString: NSMutableAttributedString = NSMutableAttributedString()
    
    class func build() -> AttributedStringbuilder {
        return AttributedStringbuilder()
    }
    
    @discardableResult
    func add(string: String,withFont: UIFont,withColor: UIColor,lineSpace: CGFloat) -> AttributedStringbuilder {
        let style = NSMutableParagraphStyle()
        style.lineSpacing = lineSpace
        mutableAttributedString.append(NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: withColor,NSAttributedString.Key.font: withFont, NSAttributedString.Key.paragraphStyle: style]))
        return self
    }
    
    @discardableResult
    func add(string: String,withFont: UIFont,withColor: UIColor,backColor: UIColor) -> AttributedStringbuilder {
        let style = NSMutableParagraphStyle()
        mutableAttributedString.append(NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: withColor,NSAttributedString.Key.font: withFont,NSAttributedString.Key.backgroundColor: backColor, NSAttributedString.Key.paragraphStyle: style]))
        return self
    }
    
    @discardableResult
    func add(string: String,withFont: UIFont,withColor: UIColor,indent: CGFloat) -> AttributedStringbuilder {
        let style = NSMutableParagraphStyle()
        style.firstLineHeadIndent = indent
        mutableAttributedString.append(NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: withColor,NSAttributedString.Key.font: withFont, NSAttributedString.Key.paragraphStyle: style]))
        return self
    }
    
    @discardableResult
    func add(string: String,withFont: UIFont,withColor: UIColor,indent: CGFloat,lineSpace: CGFloat) -> AttributedStringbuilder {
        let style = NSMutableParagraphStyle()
        style.firstLineHeadIndent = indent
        style.lineSpacing = lineSpace
        mutableAttributedString.append(NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: withColor,NSAttributedString.Key.font: withFont, NSAttributedString.Key.paragraphStyle: style]))
        return self
    }
    
    @discardableResult
    func underLine() -> AttributedStringbuilder {
        let range1 = NSRange(location: 0, length: mutableAttributedString.string.count)
        let number = NSNumber(value:NSUnderlineStyle.single.rawValue)
        mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: number, range: range1)
        return self
    }
    
    @discardableResult
    func add(string: String,withFont: UIFont,withColor: UIColor) -> AttributedStringbuilder {
        mutableAttributedString.append(NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: withColor,NSAttributedString.Key.font: withFont]))
        return self
    }
    
    @discardableResult
    func add(string: String,withSize: CGFloat,withColor: UIColor) -> AttributedStringbuilder {
        mutableAttributedString.append(NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: withColor,NSAttributedString.Key.font: UIFont.systemFont(ofSize: withSize)]))
        return self
    }
    
    @discardableResult
    func attach(attachment: NSTextAttachment) -> AttributedStringbuilder {
        mutableAttributedString.append(NSAttributedString(attachment: attachment))
        return self
    }
    
    @discardableResult
    func attach(image: UIImage) -> AttributedStringbuilder {
        let attachment = NSTextAttachment()
        attachment.image = image
        mutableAttributedString.append(NSAttributedString(attachment: attachment))
        return self
    }
    
}
 
extension NSMutableAttributedString{
    func heightOfAttributedString(_ width:Double) -> CGFloat {
        let height = self.boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil).height
        return ceil(height)
    }
}