user2268539
user2268539

Reputation: 173

Colour of a substring in a string of text view

I have text view like:

    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];

Now what i want is text "textview" should be in some different colour. How can i do this? Thanks!!

Upvotes: 1

Views: 2381

Answers (3)

Nishant Tyagi
Nishant Tyagi

Reputation: 9913

You can use different color , different font and different size text in your control(UILabel, UITextField or UItextView) by using this class method

+ (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:@""];
}

Here you can pass your text , its range , font and color as parameters. Example :

[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]];

It will display text with different color and different font.

Note : In this method we have used UITextView . If you need UILabel or UITextField then you just need to change it in same way like:

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;

Hope it helps you.

Upvotes: 1

Vineet Singh
Vineet Singh

Reputation: 4019

First of all,You have to break your string into substrings. As you said in your comment,""this is a" is in white and "textview" is in red"... Am going to suggest you a way in that reference.

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

NSString *yourString = staticTxt.text;

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

Now you can set color of "str" substring.

Let me tell you one thing very clearly.It is way only to change substring color of static string.. If you are going to change the text of your textview dynamically... then this method is not a good choice...

Upvotes: 1

ColinE
ColinE

Reputation: 70122

You need to create an attributed text string, as described in this answer:

Underline text in a UITextView

Upvotes: 1

Related Questions