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)
    }
}

这样我们就可以直接按照字符串的规则添加图片了。非常方便。

好了对于这个功能就先到这里了,成功的路上总不缺少努力的人,加油。。。。。

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