在标签中显示具有多个属性的字符串
Displaying a string with multiple attributes in a label
我需要显示一个每秒都会变化的字符串。
例如:
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) 文本的颜色 - 'View will refresh in _ sec' 将为白色
2) 数字的颜色 - '10' , '09' ... 将为黄色。
如下参考所示:
如何只使用一个标签实现这一目标?
非常感谢。
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;
如果它在 webview 中,我们可以使用 CSS 但它是 UILabel,因此您可以使用以下 UILabel 链接:-
1) https://github.com/AliSoftware/OHAttributedLabel/
2) https://github.com/mattt/TTTAttributedLabel/
3) https://github.com/joaoffcosta/UILabel-FormattedText
希望这对你有用。
您可以像这样使用字符串属性 (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
试试这个
- (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];
}
}
在情节提要中创建标签设置约束,使其可以适应可变宽度
将标签命名为 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
我需要显示一个每秒都会变化的字符串。
例如:
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) 文本的颜色 - 'View will refresh in _ sec' 将为白色
2) 数字的颜色 - '10' , '09' ... 将为黄色。
如下参考所示:
如何只使用一个标签实现这一目标?
非常感谢。
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;
如果它在 webview 中,我们可以使用 CSS 但它是 UILabel,因此您可以使用以下 UILabel 链接:-
1) https://github.com/AliSoftware/OHAttributedLabel/
2) https://github.com/mattt/TTTAttributedLabel/
3) https://github.com/joaoffcosta/UILabel-FormattedText
希望这对你有用。
您可以像这样使用字符串属性 (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
试试这个
- (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];
}
}
在情节提要中创建标签设置约束,使其可以适应可变宽度 将标签命名为 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