显示标签中具有多个属性的字符串

时间:2015-03-12 07:17:30

标签: ios nsstring uilabel nsattributedstring

我需要显示一个每秒都会改变的字符串。

例如:

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

上述字符串需要显示多个文本属性,例如 -

1)文字的颜色 - '视图将以秒为单位刷新'将为白色 2)数字的颜色 - '10','09'......将为黄色。

如下面的参考资料所示:

enter image description here

如何只使用一个标签来实现这一目标?

非常感谢提前。

5 个答案:

答案 0 :(得分:2)

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;

答案 1 :(得分:0)

您可以使用这样的字符串属性(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];

然后要在NSString中包含变量,您可以使用:

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

答案 2 :(得分:0)

试试这个

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

答案 3 :(得分:0)

在storyboard中创建一个标签,设置约束,使其可以容纳可变宽度 将标签命名为myLabel;

这里是.m文件

    //
//  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

答案 4 :(得分:-1)

如果它在webview中,我们可以使用CSS,但它是UILabel所以你可以使用UILabel的以下链接: -

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

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

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

希望这会为你工作。