iOS最近有一个需求需要在用户购买之前去遮挡客户的电话、手机号和邮箱等内容;自己在苦思冥想之际,写了一个小的功能,为了便于总结,特发布与简书之上,供各位码友使用;
1)、固定的* 遮挡
NSString*company=[string stringByReplacingOccurrencesOfString:[string substringWithRange:NSMakeRange(2,3)]withString:@"***"];
2)、不固定的* 遮挡
-(NSString *)replaceStringWithAsterisk:(NSInteger)startLocation length:(NSInteger)length withString:(NSString*)originalString {
NSString * replaceStr= originalString;
int len =length - startLocation;
for (NSInteger i = 0; i < len; i++) {
NSRange range = NSMakeRange(i, 1);
replaceStr = [replaceStr stringByReplacingCharactersInRange:range withString:@"*"];
startLocation ++;
}
return replaceStr;
}