项目需求
根据产品需要,要求所有出现价格的地方,小数点后两位都要小两号字体,评论人昵称需要匿名处理。好吧又是仿淘宝😂😂
功能实现
方法很简单,只需要讲我写的代码复制到你的NString的Categorie里面就可以了。
#pragma mark富文本
- (NSMutableAttributedString *)stringwith:(UIColor *)color whith:(UIFont *)font with:(NSRange)range {
NSMutableAttributedString *attriutedStr = [[NSMutableAttributedString alloc]initWithString:self];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
if (color != nil) {
[dic addEntriesFromDictionary:@{NSForegroundColorAttributeName: color }];
}
if (font != nil) {
[dic addEntriesFromDictionary:@{NSFontAttributeName: font }];
}
[attriutedStr addAttributes:dic range:range];
return attriutedStr;
}
#pragma mark匿名
- (NSString *)isNiMingString {
if (self.length == 0 || [self isEqual:[NSNull null]] || [self isEqualToString:@""] || [self isEqualToString: @"(null)"]) {
return @"****";
}else if (self.length == 1) {
return @"****";
} else if (self.length == 2 ) {
return [[self substringToIndex:1] stringByAppendingString:@"****"];
} else if (self.length == 11) {
return [[[self substringToIndex:3] stringByAppendingString:@"****"] stringByAppendingString:[self substringFromIndex:7]];
} else {
return [[[self substringToIndex:1] stringByAppendingString:@"****"] stringByAppendingString:[self substringFromIndex:self.length - 1]];
}
}
实际调用
//价格
NSString *price1 = [NSString stringWithFormat:@"¥%.2f",self.dataModel.warePrice];
cell3.yuanjia1.attributedText = [price1 stringwith:nil whith:[UIFont systemFontOfSize:15] with:NSMakeRange(price1.length- 2, 2)];
//匿名
commentCell.userName.text = [userComment.customerName isNiMingString];
效果如下:
WX20171116-134241.png
怎么样?是不是很淘宝😛😛
网友评论