4

我想为UILabel's文本设置两种颜色。我试过TTTRegexAttributedLabel了,但它抛出了未知类型错误。

我也尝试了以下代码。但它在 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 文本设置多种颜色?

4

3 回答 3

3

您可以使用以下图案图像设置文本颜色..

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

并用下面的代码设置不同的颜色..请与详细的伙伴一起查看教程..

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-tutorial-for-ios-part

我希望这可以帮助你...

于 2012-11-27T07:27:42.783 回答
1

NSAttributedString必须使用UILabel'attributedText属性设置。例如[syncStatusLabel setAttributedText:str]在你的情况下。祝你好运!

于 2012-11-27T07:26:41.457 回答
0

快速尝试一下(使用以下扩展名执行代码)

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)

这是结果:

在此处输入图像描述

于 2017-10-23T10:25:13.223 回答