/// 获取字符出现的位置信息(支持多次位置获取)
/// - Parameter string: 原始文本
/// - Parameter inString: 需要查找的字符
private func rangeOfString(string:NSString,
andInString inString:String) -> [NSRange] {
var arrRange = [NSRange]()
var _fullText = string
var rang:NSRange = _fullText.range(of: inString)
while rang.location != NSNotFound {
var location:Int = 0
if arrRange.count > 0 {
if arrRange.last!.location + arrRange.last!.length < string.length {
location = arrRange.last!.location + arrRange.last!.length
}
}
_fullText = NSString.init(string: _fullText.substring(from: rang.location + rang.length))
if arrRange.count > 0 {
rang.location += location
}
arrRange.append(rang)
rang = _fullText.range(of: inString)
}
return arrRange
}
/// 批量设置富文本
/// - Parameter textFont: 默认文本
/// - Parameter textColor: 默认文本颜色
/// - Parameter changeTexts: 需要改变的文本
/// - Parameter changFonts: 需要改变的字体
/// - Parameter changeColors: 需要改变的颜色
/// - Parameter isLineThrough: 是否下划线
func setAttributeStringTexts(FullTextFont textFont:UIFont,
andFullTextColor textColor:UIColor,
withChangeTexts changeTexts:[String],
withChangeFonts changFonts:[UIFont],
withChangeColors changeColors:[UIColor],
isLineThrough:Bool = false) -> NSAttributedString{
let strFullText:String = self
var range:NSRange?
var dicAttr:[NSAttributedString.Key:Any]?
let attributeString = NSMutableAttributedString.init(string: strFullText)
//不需要改变的文本
range = NSString.init(string: strFullText).range(of: String.init(strFullText))
dicAttr = [
NSAttributedString.Key.font:textFont,
NSAttributedString.Key.foregroundColor:textColor,
]
attributeString.addAttributes(dicAttr!, range: range!)
//需要改变的文本
var index:Int = 0
for item in changeTexts {
//range = NSString.init(string: strFullText).range(of: item)
let ranges = self.rangeOfString(string: NSString.init(string: strFullText), andInString: item)
for range in ranges {
dicAttr = [
NSAttributedString.Key.font:changFonts.count > index ? changFonts[index] : changFonts.first as Any,
NSAttributedString.Key.foregroundColor:changeColors.count > index ? changeColors[index] : changeColors.first as Any,
]
if isLineThrough {
dicAttr?[NSAttributedString.Key.strikethroughStyle] = NSNumber.init(value: 1)
}
if range.location + range.length <= fullString.length {
attributeString.addAttributes(dicAttr!, range: range)
}
}
index += 1
}
return attributeString
}
网友评论