自己创建一个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);
}