自定义返回NSMutableAttributedString

import UIKit

 

extension NSMutableAttributedString {

    

    class func addAttributeLine(text:String, param:[NSAttributedString.Key : Any],lineColor: UIColor = UIColor.black,lineStyle:NSAttributedString.Key = NSAttributedString.Key.underlineStyle,isCover:Bool = false) -> (NSMutableAttributedString,NSMutableAttributedString) {

        

        let array1:NSMutableDictionary = param as! NSMutableDictionary

        let array2:NSMutableDictionary = param as! NSMutableDictionary

        if param.keys.contains(NSAttributedString.Key.underlineStyle) {

            array1.removeObject(forKey: NSAttributedString.Key.underlineStyle)

            if isCover == true {

                array2.setValue(lineStyle, forKey: NSAttributedString.Key.underlineStyle.rawValue)

            }

        }else{

            array2.setValue(lineStyle, forKey: NSAttributedString.Key.underlineStyle.rawValue)

        }

        

        if param.keys.contains(NSAttributedString.Key.underlineColor) {

            array1.removeObject(forKey: NSAttributedString.Key.underlineColor)

            if isCover == true {

                array2.setValue(lineColor, forKey: NSAttributedString.Key.foregroundColor.rawValue)

            }

        }else{

            array2.setValue(lineColor, forKey: NSAttributedString.Key.foregroundColor.rawValue)

        }

        

        

        

        let attStr1 = NSMutableAttributedString.init(string: text, attributes: array1 as? [NSAttributedString.Key : Any])

        let attStr2 = NSMutableAttributedString.init(string: text, attributes: (array2 as? [NSAttributedString.Key : Any]))

        return (attStr1,attStr2)

    }

}

posted @ 2019-12-02 11:59  sundayswift  阅读(180)  评论(0编辑  收藏  举报