UILabel文本的两种颜色

我想为UILabel's文本设置两种颜色。 我尝试TTTRegexAttributedLabel ,但它是抛出未知types的错误。

我也试过下面的代码。 但是它在settext中崩溃了。

  NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Hello. That is a test attributed string."]; [str addAttribute: @"Hello" value:[UIColor yellowColor] range:NSMakeRange(3,5)]; [str addAttribute:@"That" value:[UIColor greenColor] range:NSMakeRange(10,7)]; [str addAttribute:@"Hello" value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(20, 10)]; [syncStatusLabel setText:(NSString *)str]; 

有没有其他的方式来设置单个UILabel文本的多种颜色?

你可以设置文字颜色与图案像波纹pipe的图像..

  [yourLable setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImageName"]]]; 

并设置不同的颜色与此波纹pipe代码..请检查教程与详细队友..

 NSString *test = @"Hello. That is a test attributed string."; CFStringRef string = (CFStringRef) test; CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); CFAttributedStringReplaceString (attrString,CFRangeMake(0, 0), string); /* Note: we could have created CFAttributedStringRef which is non mutable, then we would have to give all its attributes right when we create it. We can change them if we use mutable form of CFAttributeString. */ //Lets choose the colors we want to have in our string CGColorRef _orange=[UIColor orangeColor].CGColor; CGColorRef _green=[UIColor greenColor].CGColor; CGColorRef _red=[UIColor redColor].CGColor; CGColorRef _blue=[UIColor blueColor].CGColor; //Lets have our string with first 20 letters as orange //next 20 letters as green //next 20 as red //last remaining as blue CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 20),kCTForegroundColorAttributeName, _orange); CFAttributedStringSetAttribute(attrString, CFRangeMake(20, 20),kCTForegroundColorAttributeName, _green); CFAttributedStringSetAttribute(attrString, CFRangeMake(40, 20),kCTForegroundColorAttributeName, _red); CFAttributedStringSetAttribute(attrString, CFRangeMake(60, _stringLength-61),kCTForegroundColorAttributeName, _blue); 

欲了解更多信息,请参阅本教程

coretext教程换IOS-部分

我希望这可以帮助你…

NSAttributedString必须使用UILabelattributedText NSAttributedString attributedText进行设置。 例如[syncStatusLabel setAttributedText:str]在你的情况。 祝你好运!

用swift试试这个(用下面的扩展名执行代码)

 extension NSMutableAttributedString { func setColorForText(textToFind: String, withColor color: UIColor) { let range: NSRange = self.mutableString.range(of: textToFind, options: .caseInsensitive) self.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range) } } 

尝试使用UILabel进行扩展:

 self.view.backgroundColor = UIColor.blue.withAlphaComponent(0.5) let label = UILabel() label.frame = CGRect(x: 40, y: 100, width: 280, height: 200) let stringValue = "Hello. That is a test attributed string." // or direct assign single string value like "firstsecondthird" label.textColor = UIColor.lightGray label.numberOfLines = 0 let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue) attributedString.setColorForText(textToFind: "Hello", withColor: UIColor.yellow) attributedString.setColorForText(textToFind: "That", withColor: UIColor.green) label.font = UIFont.systemFont(ofSize: 26) label.attributedText = attributedString self.view.addSubview(label) 

结果如下:

在这里输入图像说明