objective-c NSMutableAttributedString

时间:2022-09-21 07:38:59

本人开发的开发者技术变现资源聚集地,大家支持下,下面是网址

https://www.baiydu.com

NSMutableAttributedString 是一个很强悍的富文本处理字符串,可以方便的实现一个字符串中某个字符的样式处理。我把我下面代码实现的功能步骤说一下:首先拼接两个字符串,然后给前前半部分和后半部分字符串分别实现不同样式,最后在给后半部分的字符串增加删除线.... 如果不用到NSMutableAttributedString 实现起来不但麻烦不说,而且不能保证百分之百的准确: 下面先截图 然后上代码

objective-c  NSMutableAttributedStringobjective-c  NSMutableAttributedString(←就是它了)

NSDictionary *singleDictionary=[[NSDictionary alloc]initWithObjectsAndKeys:@"pic1",@"imagePath",@"雅致汉斯套餐",@"title",@"重庆only you婚礼策划",@"content",@"6666",@"currentPrice",@"8888",@"truePrice", nil];
//价格
UILabel *PriceLabel=[[UILabel alloc]initWithFrame:CGRectMake(DEVICE_Width*0.5+6,89-22,DEVICE_Width*0.5-6, 15)];
PriceLabel.textColor=COLOR(249, 135, 164);
PriceLabel.font=[UIFont systemFontOfSize:15]; PriceLabel.text= [NSString stringWithFormat:@"%@%@%@%@",@"¥",[singleDictionary objectForKey:@"currentPrice"],@" ¥",[singleDictionary objectForKey:@"truePrice"]];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString: PriceLabel.text];
[str addAttribute:NSForegroundColorAttributeName value:COLOR(249, 135, 164) range:NSMakeRange(0,str.length)]; //设置字体颜色
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:15] range:NSMakeRange(0, str.length)]; //设置字体大小
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:11]range:NSMakeRange(0, 1)];
NSString *getCutStr= [singleDictionary objectForKey:@"currentPrice"];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:11] range:NSMakeRange(getCutStr.length+2, str.length-(getCutStr.length+2))]; //设置字体大小 [str addAttribute:NSForegroundColorAttributeName value:COLOR(149,150,151) range:NSMakeRange(getCutStr.length+2, str.length-(getCutStr.length+2))]; //设置字体颜色 //删除线
int beginThroughIndex= [NSString stringWithFormat:@"%@", [singleDictionary objectForKey:@"currentPrice"]].length+3;
[str addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid|NSUnderlineStyleSingle) range:NSMakeRange(beginThroughIndex, str.length-beginThroughIndex)];
[str addAttribute:NSStrikethroughColorAttributeName value:COLOR(149,150,151) range:NSMakeRange(beginThroughIndex, str.length-beginThroughIndex)];

 

PriceLabel.attributedText = str;
PriceLabel.textAlignment=NSTextAlignmentLeft;
PriceLabel.numberOfLines = 0;
[cell addSubview:PriceLabel];

  

本人创业做的一款androidApp, 下载量已经有2000多万,各种当前热门的网络手机奖励红包全部集成,另外还有热门电影和淘宝高额优惠券!很适合各类型的用户。

 objective-c  NSMutableAttributedString