自己創建一個NSString的類別:
創建一個回調block:typedef void(^AttributedBlock)(id data);
1.根據需求頁面需要修改一個label里面的文字顏色及大小:
/**
currentString 當前不需要改變的文字
chageString 當前需要改變的文字
endString 結尾字符串
fontSize 改變的字體大小
stringColor 改變的顏色
tag 1:改變顏色。2:改變大小 3.改變顏色和字體
*/
/**改變字體顏色大小*/
+(void)stringWithCurrentString:(NSString *)currentString
withChangeString:(NSString *)chageString
withColor:(UIColor *)stringColor
withTag:(NSInteger)tag
withFont:(NSInteger)fontSize
withEndString:(NSString *)endString
withBlock:(AttributedBlock)block;
+(void)stringWithCurrentString:(NSString *)currentString
withChangeString:(NSString *)chageString
withColor:(UIColor *)stringColor
withTag:(NSInteger)tag
withFont:(NSInteger)fontSize
withEndString:(NSString *)endString
withBlock:(AttributedBlock)block
{
NSString * textString = [NSString stringWithFormat:@"%@%@%@",currentString,chageString,endString];
NSInteger currentStringLength = currentString.length;
NSInteger chageStringLength = chageString.length;
NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc]initWithString:textString];
if (tag == 1)//改變顏色
{
[attributeString addAttribute:NSForegroundColorAttributeName value:stringColor range:NSMakeRange(currentStringLength, chageStringLength)];
}
else if (tag == 2)
{
[attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize] range:NSMakeRange(currentStringLength, chageStringLength)];
}
else if (tag == 3)
{
[attributeString addAttribute:NSForegroundColorAttributeName value:stringColor range:NSMakeRange(currentStringLength, chageStringLength)];
[attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize] range:NSMakeRange(currentStringLength, chageStringLength)];
}
block(attributeString);
}
2.文字前面加圖片:
+(void)stringWithImage:(NSString *)currentString
withBlock:(AttributedBlock)block;
+(void)stringWithImage:(NSString *)currentString
withBlock:(AttributedBlock)block
{
NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc]initWithString:currentString];
NSTextAttachment *attach = [[NSTextAttachment alloc] init];
attach.image = [UIImage imageNamed:@"需要添加的圖片"];
attach.bounds = CGRectMake(0, -3, 15, 15);
NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
[attributeString insertAttributedString:attachString atIndex:0];
block(attributeString);
}
3.顯示價格的時候需要顯示中劃線:
/**中劃線。價格*/
+(void)stringWithLine:(NSString *)currentString
withBlock:(AttributedBlock)block;
+(void)stringWithLine:(NSString *)currentString
withBlock:(AttributedBlock)block
{
NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc]initWithString:currentString];
NSInteger currentStringLength = currentString.length;
[attributeString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, currentStringLength)];
[attributeString addAttribute:NSStrikethroughColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, currentStringLength)];
block(attributeString);
}