iOS如何將字符串中特定后的字變成紅色
更新時間:2017年07月03日 10:23:41 作者:弦外雨
這篇文章主要介紹了iOS將字符串中特定后的字變成紅色的實(shí)例代碼,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
一,效果圖。

二,代碼。
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如何將字符串中特定后的字變成紅色,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
iOS實(shí)現(xiàn)類似微信和支付寶的密碼輸入框(UIKeyInput協(xié)議)
這篇文章主要介紹了iOS實(shí)現(xiàn)類似微信和支付寶的密碼輸入框,通過UIKeyInput協(xié)議為響應(yīng)者提供簡單的鍵盤輸入的功,再通過CoreGraphics繪制出密碼輸入框,感興趣的小伙伴們可以參考一下2016-08-08
iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件
這篇文章主要介紹了iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11
簡介iOS開發(fā)中應(yīng)用SQLite的模糊查詢和常用函數(shù)
這篇文章主要介紹了iOS開發(fā)中應(yīng)用SQLite的模糊查詢和常用函數(shù),SQLite是一個可作嵌入式的數(shù)據(jù)庫非常適合小型應(yīng)用使用,需要的朋友可以參考下2015-12-12

