一,效果圖。
二,代碼。
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如何將字符串中特定后的字變成紅色,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選