swift 富文本開發之 SwiftyAttributes

對於開發中使用富文本的情況是非常常見的一個場景,而且swift已經有一個很好的支持富文本的NSAttributedString。但是使用起來還是有點麻煩,不太方便。網上也有一個第三方的框架SwiftyAttributes,試用起來很好用。然後這個功能非常的棒,支持的平臺也比較多,如果我們只需要手機平臺的話,那麼我們用上它就有點不太值了。但是我們可以根據他的思路單獨封裝一個專用的手機平臺sdk。

這個源碼核心的實現相對比較簡單,但是裏面的思想確實很新穎,也只有swift能這麼實現了,那麼我們可以按照他的邏輯來實現。

首先我們需要定義一個AttributeNameKey枚舉類型:

enum AttributeNameKey {
    case font(UIFont)
    case textColor(UIColor)
    case paragraphStyle(NSParagraphStyle)
    
    
    var keyName: NSAttributedString.Key {
        
        let name: NSAttributedString.Key
        
        switch self {
            case .textColor:
                name = .foregroundColor
            case .paragraphStyle:
                name = .paragraphStyle
            case .font:
                name = .font
        }
        return name;
    }
    
    var value: Any {
        switch self {
            case .font(let font):
                return font;
            case .textColor(let color):
                return color;
            case .paragraphStyle(let style):
                return style;
        }
    }
}

這個主要完成了兩個功能,對應着NSAttributedString的鍵值。這裏也用到了swift獨有的值綁定特性。

接下來我們擴展NSAttributedString的使用。

 

extension NSAttributedString {
    
    
    func withAttributes(nameKey:[AttributeNameKey]) -> NSMutableAttributedString {
        let mutable = mutableCopy() as! NSMutableAttributedString;
        for item in nameKey {
            mutable.addAttributes([item.keyName:item.value], range: .init(location: 0, length: length));
        }
       
        return mutable;
    }
    func withAttribute(nameKey: AttributeNameKey) -> NSMutableAttributedString {
        withAttributes(nameKey: [nameKey])
    }
}

這個是給NSAttributedString添加了兩個屬性,這個是核心,我們也可以再添加具體的函數比如像下面的一樣:

func withFont(font: UIFont) -> NSMutableAttributedString {
    withAttribute(nameKey: .font(font))
}

爲了給字符串直接加上富文本屬性還需要給String也添加一個擴展:

 

extension String {
    var attributed: NSMutableAttributedString {
        NSMutableAttributedString(string: self)
    }
    
    func withAttribute(nameKey: AttributeNameKey) -> NSMutableAttributedString {
        attributed.withAttributes(nameKey: [nameKey]);
    }
    func withAttributes(nameKey:[AttributeNameKey]) -> NSMutableAttributedString {
        attributed.withAttributes(nameKey: nameKey)
    }
    
}

爲了能讓NSAttributedString實現像swift字符串一樣的加號操作,我們需要給NSAttributedString添加一個加號操作符。如下:

extension NSAttributedString {
    static func + (left: NSAttributedString,right: NSAttributedString) -> NSAttributedString {
        let compone = left as! NSMutableAttributedString;
        compone.append(right);
        return compone;
    }
}

這樣我們就可以開心的像字符串一樣的使用了。具體使用如下:

func testAttribute() {
    let text = "麪條".withAttribute(nameKey:
.font(UIFont.systemFont(ofSize:  15))).withAttribute(nameKey:
.textColor(UIColor.red)) + "(12元)".withAttribute(nameKey:
.font(UIFont.systemFont(ofSize: 12))).withAttribute(nameKey:
.textColor(UIColor.green));
}

如果細心的人會發現,屬性太少不夠用,這裏我先申明一下,這個只是一個思路,也是一個例子,我們可以添加NSAttributedString所有的屬性。當然最好用到那個添加那個,沒有必要全部添加上。

其實還有個小問題,如果要添加圖片還是有點麻煩。那麼我們也封裝一下圖片。如下:

extension UIImage {
    func attribute(bounds: CGRect) -> NSMutableAttributedString {
        let imageText = NSTextAttachment();
        imageText.image = self
        imageText.bounds = bounds;
        return NSMutableAttributedString(attachment: imageText)
    }
}

這樣我們就可以直接按照字符串的規則添加圖片了。非常方便。

好了對於這個功能就先到這裏了,成功的路上總不缺少努力的人,加油。。。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章