文本视图字符串中子字符串的颜色

时间:2013-04-25 11:05:25

标签: ios objective-c xcode nsstring uitextview

我有文字视图,如:

    UITextview staticTxt = [[UITextView alloc]initWithFrame:CGRectMake(10, 80, 300, 400)];
        staticTxt.backgroundColor = [UIColor clearColor];
        staticTxt.text = @"this is a textview";
        staticTxt.textColor = [UIColor whiteColor];
[self.view addSubview:staticTxt];

现在我想要的是文字“textview”应该是一些不同的颜色。我怎样才能做到这一点? 谢谢!

3 个答案:

答案 0 :(得分:1)

您需要创建一个属性文本字符串,如以下答案中所述:

Underline text in a UITextView

答案 1 :(得分:1)

首先,您必须将字符串分解为子字符串。正如你在评论中所说的那样,“”这是一个“是白色的”,而textview“是红色的”......我会在你的参考资料中建议你。

staticTxt.text = @"this is a textview";

NSString *yourString = staticTxt.text;

NSString *str = [[yourString componentsSeparatedByString:@"a "] lastObject];

现在您可以设置“str”子串的颜色。

让我非常清楚地告诉你一件事。它只是改变静态字符串的子串颜色。如果要动态更改textview的文本...那么这个方法不是一个好的选择..

答案 2 :(得分:1)

您可以使用此类方法在控件(UILabel,UITextField或UItextView)中使用不同颜色,不同字体和不同大小的文本

+ (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString inputView:(UITextView*) inputView font:(NSArray*) fontName color:(NSArray*) colorName
{
    inputView.layer.sublayers = nil;

    NSMutableAttributedString *mutableAttributedString = [[ NSMutableAttributedString alloc]initWithString:text];

    for (int i =0 ; i<[rangeString count]; i++)
    {
        CTFontRef  ctFont = CTFontCreateWithName((__bridge CFStringRef) [UIFont fontWithName:[fontName objectAtIndex:i] size:14.0].fontName, [UIFont fontWithName:[fontName objectAtIndex:i] size:14.0].pointSize, NULL);

        NSRange whiteRange = [text rangeOfString:[rangeString objectAtIndex:i]];

        if (whiteRange.location != NSNotFound)
        {
            [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[colorName objectAtIndex:i] range:whiteRange];
            [mutableAttributedString addAttribute:(NSString*)kCTFontAttributeName
                                            value:( __bridge id)ctFont
                                            range:whiteRange];
        }
    }

    CGSize expectedinputViewSize = [text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0f] constrainedToSize:CGSizeMake(186,100) lineBreakMode:UILineBreakModeWordWrap];

    CATextLayer   *textLayer = [[CATextLayer alloc]init];
    textLayer.frame =CGRectMake(0,4, inputView.frame.size.width,expectedinputViewSize.height+4);
    textLayer.contentsScale = [[UIScreen mainScreen] scale];
    textLayer.string=mutableAttributedString;
    textLayer.opacity = 1.0;
    textLayer.alignmentMode = kCAAlignmentLeft;
    [inputView.layer addSublayer:textLayer];
    [textLayer setWrapped:TRUE];

    [inputView setText:@""];
}

您可以在此处传递文本,范围,字体和颜色作为参数。 示例:

[CommonFunctions setMultiColorAndFontText:@"This is a textview" rangeString:[NSArray arrayWithObjects:@"This is a",@"textview", nil] textfield:txtEmailAddress font:[NSArray arrayWithObjects:@"Arial",@"Helvetica",nil] color:[NSArray  arrayWithObjects:(UIColor *)[UIColor whiteColor].CGColor,(UIColor *)[UIColor redColor].CGColor,nil]];

它将显示不同颜色和不同字体的文本。

注意:在这个方法中我们使用了UITextView。如果您需要UILabel或UITextField,那么您只需要以相同的方式更改它:

<强>的UILabel

+ (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString inputView:(UILabel*) label font:(NSArray*) fontName color:(NSArray*) colorName;

<强>的UITextField

+ (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString inputView:(UITextField*) label font:(NSArray*) fontName color:(NSArray*) colorName;

<强>的UITextView

+ (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString inputView:(UITextView*) label font:(NSArray*) fontName color:(NSArray*) colorName;

希望它对你有所帮助。