是否可以将 2 个 NSAttributedString
放在一个标签中?
例如:
NSString *a = [car valueForKey:@"name"];
NSString *b = [car valueForKey:@"version"];
NSAttributedString *title;
title = [[NSAttributedString alloc] initWithString:a attributes:@{ NSFontAttributeName : [UIFont fontWithName:@"Noteworthy-Bold" size:36], NSUnderlineStyleAttributeName : @1 , NSStrokeColorAttributeName : [UIColor blackColor]}]; //1
NSAttributedString *title2;
title2 = [[NSAttributedString alloc] initWithString:b attributes:@{ NSFontAttributeName : [UIFont fontWithName:@"Noteworthy-Bold" size:36], NSUnderlineStyleAttributeName : @0 , NSStrokeColorAttributeName : [UIColor blackColor]}]; //2
cell.textLabel.attributedText = //what should I write here?
最佳答案
您可以使用 -appendAttributedString:(NSString *) 方法将一个属性字符串附加到另一个。由于您可以使两个属性字符串都可变,因此您还可以包含分隔符(分号、逗号)以区分同一个标签中的两个不同字符串。
下面是一些示例代码:
NSMutableAttributedString *string1 = [[NSMutableAttributedString alloc] initWithString:@"Hello"];
NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc] initWithString:@"Hello 2"];
NSMutableAttributedString *semiStr = [[NSMutableAttributedString alloc] initWithString:@" ; "];
[string1 appendAttributedString:semiStr]; //separate string 1 and 2 by semi-colon
[string1 appendAttributedString:string2];
textLabel.text = string1; //which string2 is now appended to
显然,在您的属性字符串中,您会有属性(就像您在问题中所说的那样)。 您也可以始终信任 Apple's documentation on the NSMutableAttributedString class找到适合下次使用的方法。
关于ios - 将 2 个 NSAttributedStrings 放在一个标签中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17774039/