如何从颜色数组中为文本指定文本颜色?

时间:2014-07-27 07:01:30

标签: ios objective-c

我正在尝试制作五种颜色中的UILabel

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *randomColor = [colorArray objectAtIndex: arc4random() % [colorArray count]];
    colorArray = [[NSArray alloc] init];
    [UIColor redColor], [UIColor blueColor], [UIColor greenColor], [UIColor yellowColor];
    colorLabel.textColor = randomColor;
}

如何将数组中的对象设置为标签textColor的UIColor?对不起,如果已经回答了这个问题,我就无法找出正确的方法来对其进行查找。

1 个答案:

答案 0 :(得分:1)

试试这个......

#define kNumColors    4

- (UIColor *) randomColor
{
  NSInteger colorIndex = arc4random() % kNumColors;
  UIColor *color;

  switch (colorIndex) {
    case 0:
      color = [UIColor blueColor];
      break;
    case 1:
      color = [UIColor redColor];
      break;
    case 2:
      color = [UIColor yellowColor];
      break;
    case 3:
      color = [UIColor greenColor];
      break;
  }
  return color;
}

指定随机颜色......

colorLabel.textColor = [self randomColor];