程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> cocoa touch-NSAttributedString修改字符串顏色

cocoa touch-NSAttributedString修改字符串顏色

編輯:編程綜合問答
NSAttributedString修改字符串顏色

應用中有一個滾動條,根據滾動條的值顯示下面的字符串:
Very Bad, Bad, Okay, Good, Very Good

滾動條的實現代碼如下:

  - (IBAction) sliderValueChanged:(UISlider *)sender {
   scanLabel.text = [NSString stringWithFormat:@" %.f", [sender value]];
    NSArray *texts=[NSArray arrayWithObjects:@"Very Bad", @"Bad", @"Okay", @"Good", @"Very Good", @"Very Good", nil];
    NSInteger sliderValue=[sender value]; //make the slider value in given range integer one.
    self.scanLabel.text=[texts objectAtIndex:sliderValue];
}

其中Very Bad紅色,Bad橙色,Okay黃色,Good和Very Good綠色。不知道怎麼設置attributedstring實現?謝謝

最佳回答:


我還沒編譯:

NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:self.text.text];
NSRange range=[self.text.text rangeOfString:[texts objectAtIndex:sliderValue]];

NSArray *colors=[NSArray arrayWithObjects:[UIColor redColor],[UIColor redColor],[UIColor yellowColor],[UIColor greenColor],nil];

[string addAttribute:NSForegroundColorAttributeName value:[colors objectAtIndex:sliderValue] range:range];           

[self.scanLabel setAttributedText:[texts objectAtIndex:sliderValue]];
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved