一,效果图。

二,代码。
viewcontroller.m
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
- (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如何将字符串中特定后的字变成红色,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.cnblogs.com/yang-guang-girl/archive/2017/07/03/7109591.html








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。