Sanjay Mohnani
Sanjay Mohnani

Reputation: 5967

Displaying a string with multiple attributes in a label

I need to display a string which will change every second.

For example :

View will refresh in 10 sec
View will refresh in 09 sec
View will refresh in 08 sec
View will refresh in 07 sec
..
View will refresh in 0 sec

The above string needs to be displayed with multiple properties of text, like-

1) The color of text - 'View will refresh in _ sec' will be White
2) The color of number - '10' , '09' ... will be Yellow.

As displayed in reference below :

enter image description here

How can I achieve this using only one label?

Many thanks in advance.

Upvotes: 0

Views: 3134

Answers (5)

Saket Kumar
Saket Kumar

Reputation: 1197

create a label in storyboard set the constraint such that it can accommodate variable width name the label as myLabel ;

here is .m file

    //
//  ViewController.m
//  TimerLogic
//
//

#import "ViewController.h"

@interface ViewController ()
{
    NSInteger currentTime ;
    NSTimer *myTimer ;
}

@end

@implementation ViewController
@synthesize myLabel ;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    currentTime = 10 ;
    [self.view addSubview:myLabel];
   myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(createLabel:) userInfo:nil repeats:YES] ;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)createLabel:(NSTimer *)theTimer
{
    if(currentTime == 10)
    {
    myLabel.backgroundColor = [UIColor redColor] ;
    myLabel.text = @"Thats me" ;
        myLabel.textColor = [UIColor yellowColor];
        myLabel.translatesAutoresizingMaskIntoConstraints = NO ;
//        NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V: | -offsetTop-[label]" options:0 metrics:@{@"offsetTop":@100 } views:NSDictionaryOfVariableBindings(myLabel)];
    }
    if(currentTime == 9)
    {
        myLabel.backgroundColor = [UIColor greenColor] ;
        myLabel.text = @"Thats me again 1" ;
         myLabel.textColor = [UIColor whiteColor];
    }
    if (currentTime == 8)
    {
        myLabel.backgroundColor = [UIColor greenColor] ;
        myLabel.text = @"Thats me again 2" ;
         myLabel.textColor = [UIColor blackColor];
    }

    // like that for all values
    if(currentTime == 7)
    {

        myLabel.backgroundColor = [UIColor purpleColor] ;
        myLabel.text = @"Thats me again 3" ;
         [myTimer invalidate ] ;
         myLabel.textColor = [UIColor yellowColor];
    }
       currentTime-- ;
}

@end

Upvotes: 0

Jaffer Sheriff
Jaffer Sheriff

Reputation: 1564

Try this

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSTimer *timer =  [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLabelText:) userInfo:nil repeats:YES];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] init];
    [str appendAttributedString:[[NSAttributedString alloc] initWithString:@"Will change in " attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:17], NSForegroundColorAttributeName : [UIColor whiteColor] }]];
    [str appendAttributedString:[[NSAttributedString alloc] initWithString:@"10 Sec" attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:19], NSForegroundColorAttributeName : [UIColor yellowColor] }]];
    myLabel.attributedText = str;
}

- (NSString *)extractNumberFromText:(NSString *)text
{
    NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    return [[text componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];
}
-(void)updateLabelText:(NSTimer *) timerLocal
{
    int labelCount =  [[self extractNumberFromText:myLabel.text]intValue];
    if (labelCount!=0)
    {
        labelCount--;

        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] init];
        [str appendAttributedString:[[NSAttributedString alloc] initWithString:@"Will change in " attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:17], NSForegroundColorAttributeName : [UIColor whiteColor] }]];
        [str appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%i Sec",labelCount] attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:19], NSForegroundColorAttributeName : [UIColor yellowColor] }]];
        myLabel.attributedText = str;
    }
    else
    {
        [timerLocal invalidate];
    }
}

Upvotes: 0

djoosi
djoosi

Reputation: 236

You can use string attributes like this (DOC):

NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:yourString];
[attr addAttribute: NSForegroundColorAttributeName value: [UIColor blackColor] range: NSMakeRange(0, 4)]; // color for char 0 to 4
[attr addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(4, 8)]; // color for char 4 to 8
[myLabel setAttributedText: attr];

Then to include variables in your NSString, you can use :

[NSString stringWithFormat:@"Hello %@ !", myVariable]; // %@ will be replace by myVariable

Upvotes: 0

Er.Shreyansh Shah
Er.Shreyansh Shah

Reputation: 1482

If it is in webview than we can use CSS but it's UILabel so u can use below links for UILabels:-

1) https://github.com/AliSoftware/OHAttributedLabel/

2) https://github.com/mattt/TTTAttributedLabel/

3) https://github.com/joaoffcosta/UILabel-FormattedText

Hope this'll work for u.

Upvotes: -1

Azat
Azat

Reputation: 6795

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] init];
[str appendAttributedString:[[NSAttributedString alloc] initWithString:@"Will change in " attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:17], NSForegroundColorAttributeName : [UIColor whiteColor] }]];
[str appendAttributedString:[[NSAttributedString alloc] initWithString:@"10 sec" attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:19], NSForegroundColorAttributeName : [UIColor yellowColor] }]];
myLabel.attributedText = str;

Upvotes: 2

Related Questions