代码笔记iOS-将字符串中特定后的字变成红色

Posted 菜鸟and小白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码笔记iOS-将字符串中特定后的字变成红色相关的知识,希望对你有一定的参考价值。

一,效果图。

二,代码。

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-将字符串中特定后的字变成红色的主要内容,如果未能解决你的问题,请参考以下文章

第十周课程总结

初学C++有些字符我怎么输出的黑字而不是蓝色的字或者红色的比如int ‘main 我怎么输入的是黑字而不是红字

micropython中怎么将gb2312编码的字节流变成中文

这点html代码怎么让字体变成红色?

day6,笔记内容

jqGrid 将行的字变成超连接