2013年8月6日 星期二

ios UILabel 在同一段文字上 做不同顏色的調整

step1
創造出一個 Label

  eatLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 100, 30)];

  eatLabel.font = [UIFont systemFontOfSize:14];

step2.
創造出你要的文字 給予每ㄍㄜ

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"吃了  早餐"];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor 
orangeColor] range:NSMakeRange(0,2)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3,3)];

step3.

把字串接回 Label

    eatLabel.backgroundColor = [UIColor clearColor];
  eatLabel.attributedText = str;
        [self addSubview:eatLabel];

0 意見:

張貼留言