Tuesday, December 15, 2015

How to set different text color in single label in iOS



myLabel.text = @"ABCDEEFGHIJKLMNO";

NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithAttributedStringmyLabel.attributedText];
    
    [text addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor]range:NSMakeRange(8flightDetailsStatus.text.length-8)];

    [myLabel setAttributedText: text];
    myLabel.font = [UIFont fontWithName:AppFONT size:10.0];

    myLabel.textAlignment = NSTextAlignmentRight;


//You have to pass the text, color of text and size of the text.
+ (NSMutableAttributedString *)SetLabelAttributes:(NSString *)input col:(UIColor *)col size:(Size)size {
    
    NSMutableAttributedString *labelAttributes = [[NSMutableAttributedString alloc]initWithString:input];
    
    UIFont *font=[UIFont fontWithName:AppFONT size:size];
    
    NSMutableParagraphStyle* style = [NSMutableParagraphStyle new];
    style.alignment = NSTextAlignmentRight;
    
    [labelAttributes addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, labelAttributes.length)];
    [labelAttributes addAttribute:NSParagraphStyleAttributeName value:stylerange:NSMakeRange(0, labelAttributes.length)];
    [labelAttributes addAttribute:NSForegroundColorAttributeName value:colrange:NSMakeRange(8, labelAttributes.length-8)];
    
    return labelAttributes;

}

//You can use this method like this
myLabel.attributedText = [Utility SetLabelAttributes: myLabel.text col:[UIColorgreenColorsize:10.0];

No comments:

Post a Comment