一段文字中的某一段內(nèi)容需要將其變得更加突顯出來略板,為了復(fù)雜多余的操作流程航攒,便通過如下的方法將內(nèi)容進行調(diào)整烫映,更加簡便方便分预。
/**
改變 label 文字中某段文字的顏色和大小
label 傳入的文本內(nèi)容(注:傳入前要有文字)
oneIndex 從首位文字開始
endIndex 至末位文字結(jié)束
color 字體顏色
size 字體字號
*/
+ (void)YHLabelAttributedString:(UILabel *)label firstText:(NSString *)oneIndex toEndText:(NSString *)endIndex textColor:(UIColor *)color textSize:(CGFloat)size;
+ (void)YHLabelAttributedString:(UILabel *)label firstText:(NSString *)oneIndex toEndText:(NSString *)endIndex textColor:(UIColor *)color textSize:(CGFloat)size {
// 創(chuàng)建 Attributed
NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:label.text];
// 需要改變的首位文字位置
NSUInteger firstLoc = [[noteStr string] rangeOfString:oneIndex].location;
// 需要改變的末位文字位置
// NSUInteger endLoc = [[noteStr string] rangeOfString:endIndex].location + 1;
NSUInteger endLoc = [[noteStr string] rangeOfString:endIndex].location + [[noteStr string] rangeOfString:endIndex].length;
// 需要改變的區(qū)間
NSRange range = NSMakeRange(firstLoc, endLoc - firstLoc);
// 改變顏色
[noteStr addAttribute:NSForegroundColorAttributeName value:color range:range];
// 改變字體大小及類型
[noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:size] range:range];
// 為 label 添加 Attributed
[label setAttributedText:noteStr];
}
以上便是此次分享的全部內(nèi)容兢交,希望能對大家有所幫助!