字符串 消除首位空格,文字颜色,和分段改写,
(1)系统去首尾空格方法,使用NSString中的str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]方法只是去掉左右两边的空格;
(2)替换的方法,使用NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@" " withString:@""];可以去掉空格,注意此时生成的strUrl是autorelease属性的,不要妄想对strUrl进行release操作。
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:
[NSString stringWithFormat:@" 当前消费 %.2f 元,免费配送",total]];
_tokentotal = total;
[str addAttribute:NSForegroundColorAttributeName value:BLACK range:NSMakeRange(0,6)];
[str addAttribute:NSForegroundColorAttributeName value:[UtilcolorWithHexString:@"#8ace2a"] range:NSMakeRange(6,str.length - 13)];
[str addAttribute:NSForegroundColorAttributeName value:BLACKrange:NSMakeRange(str.length - 6,6)];
((UILabel*)[self.view viewWithTag:3000]).attributedText = str;
2, 字符串的替换
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];

浙公网安备 33010602011771号