-2

I am trying to set text for a label. I want to set different font sizes and colors for different sentences. Say

#define Label @"This is a label Title. This is label subscript"

Here i need "This is a label Title" in one font. And "This is label subscript" in other font. Is it possible. I tried doing like

#define Label @"This is a label Title. <b>This is label subscript <b>" and few other things. But no change.

Thanks Jithen

Desdenova
  • 5,326
  • 8
  • 37
  • 45
Coder
  • 1,661
  • 4
  • 27
  • 50
  • Refer this: http://stackoverflow.com/questions/3482346/how-do-you-use-nsattributedstring – viral Apr 12 '13 at 12:08

2 Answers2

1

Strings does not have fonts. You need to set the font of the label itself, or use Attributed strings

Here is a simple example of how to use more than one attribute in an attributed string:

NSString *s1 = @"Hello ";
NSString *s2 = @"World";

NSDictionary *attr1 = [NSDictionary dictionaryWithObjectsAndKeys:
    [UIFont fontWithName:@"Arial" size:20] , NSFontAttributeName, nil];

NSDictionary *attr2 = [NSDictionary dictionaryWithObjectsAndKeys:
    [UIFont fontWithName:@"Georgia" size:40] , NSFontAttributeName, nil];

NSMutableAttributedString *as = [[NSMutableAttributedString alloc] 
    initWithString:[s1 stringByAppendingString:s2] attributes:attr1];

[as setAttributes:attr2 range:NSMakeRange(s1.length, s2.length)];

To put the text in a label you can use:

myLabel.attributedText = as;

I would advice against putting this kind of formatting in macros, as the code becomes messy and harder to maintain.

Hjalmar
  • 989
  • 8
  • 16
0

You can use this method.

+ (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString label:(UILabel*) label font:(NSArray*) fontName color:(NSArray*) colorName{
label.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:10.0].fontName, [UIFont fontWithName:[fontName objectAtIndex:i] size:10.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 expectedLabelSize = [text sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:10.0f] constrainedToSize:CGSizeMake(186,100) lineBreakMode:UILineBreakModeWordWrap];

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

label.lineBreakMode = UILineBreakModeWordWrap;

[label setText:@""];

}

Using this method you can display different size and different color text in same label.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61