字符串分类
-(NSMutableAttributedString *)keyWords:(NSString *)keyWords withKeyWordsColor:(UIColor *)color
{
NSMutableAttributedString *mutableAttributedStr = [[NSMutableAttributedString alloc] initWithString:self];
if (color == nil) {
color = [UIColor colorWithHexString:@"52c683"];
}
if (keyWords.length<=0) {
return mutableAttributedStr;
}
for (NSInteger j=0; j<=keyWords.length-1; j++) {
NSRange searchRange = NSMakeRange(0, [self length]);
NSRange range;
NSString *singleStr = [keyWords substringWithRange:NSMakeRange(j, 1)];
while
((range = [self rangeOfString:singleStr options:0 range:searchRange]).location != NSNotFound) {
//改变多次搜索时searchRange的位置
searchRange = NSMakeRange(NSMaxRange(range), [self length] - NSMaxRange(range));
//设置富文本
[mutableAttributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
}
}
return mutableAttributedStr;
}
swift 版本
extension String{
func keyWords_swift(_ keyWords: String, withKeyWordsColor color: UIColor = .red) -> NSMutableAttributedString? {
let mutableAttributedStr = NSMutableAttributedString.init(string: self)
if keyWords.isEmpty {
return mutableAttributedStr
}
let text = NSString.init(string: self)
var range = text.range(of: keyWords)
var searchRange = NSMakeRange(0, self.count);
while range.location != NSNotFound {
mutableAttributedStr.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
//改变多次搜索时searchRange的位置
searchRange = NSMakeRange(NSMaxRange(range), self.count - NSMaxRange(range));
range = text.range(of: keyWords, options: [], range: searchRange)
}
return mutableAttributedStr
}
func keyWords_swiftNormal(_ keyWords: String, withKeyWordsColor color: UIColor = .red) -> NSMutableAttributedString? {
let mutableAttributedStr = NSMutableAttributedString.init(string: self)
if keyWords.isEmpty {
return mutableAttributedStr
}
let text = NSString.init(string: self)
let range = text.range(of: keyWords)
if range.location != NSNotFound {
mutableAttributedStr.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
}
return mutableAttributedStr
}
}
func range(from nsRange: NSRange) -> Range<String.Index>? {
guard
let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location, limitedBy: utf16.endIndex),
let to16 = utf16.index(from16, offsetBy: nsRange.length, limitedBy: utf16.endIndex),
let from = String.Index(from16, within: self),
let to = String.Index(to16, within: self)
else { return nil }
return from ..< to
}
func nsRange(from range: Range<String.Index>) -> NSRange {
let from = range.lowerBound.samePosition(in: utf16)!
let to = range.upperBound.samePosition(in: utf16)!
return NSRange(location: utf16.distance(from: utf16.startIndex, to: from),
length: utf16.distance(from: from, to: to))
}
func getMoreKeyWordsAttributedString(members: (keywrod:String,color:UIColor)...) ->NSMutableAttributedString {
let mutableAttributedStr = NSMutableAttributedString.init(string: self)
if members.isEmpty {
return mutableAttributedStr
}
for i in members {
let keywrod = i.keywrod
let color = i.color
if keywrod.isEmpty {
continue
}
let text = NSString.init(string: self)
var range = text.range(of: keywrod)
var searchRange = NSMakeRange(0, self.count);
while range.location != NSNotFound {
mutableAttributedStr.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
//改变多次搜索时searchRange的位置
searchRange = NSMakeRange(NSMaxRange(range), self.count - NSMaxRange(range));
range = text.range(of: keywrod, options: [], range: searchRange)
}
}
return mutableAttributedStr
}
网友评论