應用中有一個滾動條,根據滾動條的值顯示下面的字符串:
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]];