Swift 常用富文本分类

写了一个常用的swift富文本分类,有指定字符串变颜色,字体,等待.效果图如下:

demo github下载地址

Swift 常用富文本分类_第1张图片
可变颜色.png
Swift 常用富文本分类_第2张图片
行间距.png
  • 第一个指定字符串颜色变化
    如果只用一个 rangeOf只能搜索到第一个字符串的范围,没法搜索多个重复的字符串范围集合,所以要遍历.

    //: 选中字体颜色变红
    /// - Parameters:
    /// - text: 所有字符串
    /// - selectedText: 需要变颜色的字符串
    /// - allColor: 字符串本来颜色
    /// - selectedColor: 选中字符串颜色
    /// - fone: 字符串字体大小
    /// - Returns: 返回一个NSMutableAttributedString

static func attributenStringColor(text:String,
                             selectedText: String,
                             allColor: UIColor,
                             selectedColor: UIColor,
                             fone: CGFloat)->NSMutableAttributedString{
    var rangeArray: [NSRange] = [NSRange]()
    var lastLength: Int = 0
    let attStr = NSMutableAttributedString.init(string: text, attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: fone),NSForegroundColorAttributeName:allColor])
    var text = text as NSString
    while text.contains(selectedText) {
        let rang: NSRange =  text.range(of: selectedText)
        let rang2 = NSMakeRange(rang.location + lastLength, rang.length)
        rangeArray.append(rang2)
        lastLength += (rang.length + rang.location )
        text = text.substring(from: rang.length + rang.location) as NSString
    }
    for range1 in rangeArray {
        attStr.setAttributes([NSForegroundColorAttributeName:selectedColor], range: range1)
    }
    return attStr  }
  • 第二个是指定两个字符中间的字符串
    这个就是各种字符串范围操作,比较繁琐而已.用我的你直接用就行.

//MARK: 指定两个字符中间文字变色
///
/// - Parameters:
/// - text: 所有字符串
/// - allColor: 基础颜色
/// - selectedColor: 特殊颜色
/// - firstStr: 其实字符串
/// - lastStr: 结束字符串
/// - fone: 基础字体
/// - Returns: NSMutableAttributedString

static func attributenStringMutColor(text:String,
                             allColor: UIColor,
                             selectedColor: UIColor,
                             firstStr : String,
                             lastStr : String,
                             fone: CGFloat)->NSMutableAttributedString{
    var rangeArray: [NSRange] = [NSRange]()
    var lastLength: Int = 0
    let attStr = NSMutableAttributedString.init(string: text, attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: fone),NSForegroundColorAttributeName:allColor])
    var text = text as NSString
    while text.contains(firstStr) {
        let rang1: NSRange =  text.range(of: firstStr)
        let range2: NSRange =  text.range(of: lastStr)
        let orangeRange = NSMakeRange(rang1.location + 1, range2.location - (rang1.location + 1))
        let range = NSMakeRange(rang1.location + 1 + lastLength , range2.location - (rang1.location + 1) )
        rangeArray.append(range)
        lastLength += (orangeRange.length + orangeRange.location + 1)
        text = text.substring(from: orangeRange.length + orangeRange.location + 1) as NSString
    }
    for range1 in rangeArray{  attStr.setAttributes([NSForegroundColorAttributeName:UIColor.red], range: range1)
    }
    return attStr}
  • 第三个改变行间距比较简单

//MARK: 行间距
///
/// - Parameters:
/// - text: 整体字符串
/// - lineSpace: 行间距
/// - fone: 字图
/// - Returns: NSMutableAttributedString

static func lineSpace(text: String,lineSpace: CGFloat,fone: CGFloat)->NSMutableAttributedString{
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpace
let attStr = NSMutableAttributedString.init(string: text, attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: fone)])
attStr.addAttributes([NSParagraphStyleAttributeName : paragraphStyle], range: NSMakeRange(0, text.characters.count))
return attStr }

  • 第四个改变字体大小和第一个差不多,想看代码的话点击后面下载demo github下载地址

你可能感兴趣的:(Swift 常用富文本分类)