1. 程式人生 > >iOS 在一個UILabel上顯示不同顏色

iOS 在一個UILabel上顯示不同顏色

self.addressLabel = [[UILabelalloc]initWithFrame:CGRectZero];

self.addressLabel.textColor = [UIColorblackColor];

self.addressLabel.font = [UIFontsystemFontOfSize:15];

        [self.contentViewaddSubview:self.addressLabel];

NSMutableAttributedString *noteStr = [[NSMutableAttributedStringalloc]initWithString

:[NSStringstringWithFormat:@"[預設地址]哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"]];

NSRange redRangeTwo = NSMakeRange([[noteStr string] rangeOfString:@"[預設地址]"].location, [[noteStr string] rangeOfString:@"[預設地址]"].length);

        [noteStr addAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor] range:redRangeTwo];

        [self.addressLabelsetAttributedText:noteStr];

        [self.addressLabelsizeToFit];