代码之家  ›  专栏  ›  技术社区  ›  Sanjay Mohnani

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

  •  0
  • Sanjay Mohnani  · 技术社区  · 10 年前

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

    例如:

    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 回复  |  直到 10 年前
        1
  •  2
  •   Azat Adam Richardson    10 年前
    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;
    
        2
  •  0
  •   djoosi    10 年前

    可以使用以下字符串属性( 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
    
        3
  •  0
  •   Jaffer Sheriff    10 年前

    试试这个

    - (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];
        }
    }
    
        4
  •  0
  •   Saket Kumar    10 年前

    在情节提要中创建标签设置约束,使其能够适应可变宽度 将标签命名为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
    
        5
  •  -1
  •   Er.Shreyansh Shah Rajaram    10 年前

    如果它是在webview中,我们可以使用CSS,但它是UILabel,因此您可以使用以下UILabels链接:-

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

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

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

    希望这对你有用。