一,效果图。
二,代码。
viewcontroller.m
- (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view, typically from a nib. uilabel *testlabel=[[uilabel alloc]initwithframe:cgrectmake(10, 100, 100, 50)]; testlabel.text=@"1234567890"; testlabel.backgroundcolor=[uicolor orangecolor]; [self.view addsubview:testlabel]; [self redxingwithlabel:testlabel atindex:2]; } //将字符串特定的1个字变成红色 - (void)redxingwithlabel:(uilabel *)templabel atindex:(nsinteger)tempindex { nsmutableattributedstring * tempstring = [[nsmutableattributedstring alloc] initwithstring: templabel.text]; [tempstring addattribute:nsforegroundcolorattributename value:[uicolor redcolor] range:nsmakerange(tempindex, 1)]; templabel.attributedtext = tempstring; }
以上所述是小编给大家介绍的ios如何将字符串中特定后的字变成红色,希望对大家有所帮助
如对本文有疑问, 点击进行留言回复!!
iOS14Beta3续航怎么样 iOS14Beta3续航能力介绍
iOS14Beta3稳定性怎么样 iOS14Beta3升级建议介绍
uniapp编写H5界面放入IOS全面屏原生导航栏盖住H5界面
游戏APP解决方案,8月1日起下架无版号ios手游的出路在哪?
网友评论