当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS如何将字符串中特定后的字变成红色

iOS如何将字符串中特定后的字变成红色

2019年07月24日  | 移动技术网移动技术  | 我要评论

一,效果图。

二,代码。

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如何将字符串中特定后的字变成红色,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网