[英]Change color and size portion of NSString with NSMutableAttributedString
Browsed through a few NSAttributedString
examples but can't seem to get this working. 浏览了一些NSAttributedString
示例,但似乎无法使其正常工作。 I am trying to change the size and color of part of a NSMutableAttributedString
. 我试图改变NSMutableAttributedString
的一部分的大小和颜色 。
I've tried a few variations of this: 我尝试了一些这样的变化:
NSMutableAttributedString *hintText = [[NSMutableAttributedString alloc] initWithString:@"This is red and huge and this is not"];
//Black and large
[hintText setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Futura-Medium" size:20.0f], NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(0, 11)];
//Rest of text -- just futura
[hintText setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Futura-Medium" size:16.0f]} range:NSMakeRange(12, ((hintText.length -1) - 12))];
This just changes the size of the text, not the color. 这只会改变文本的大小,而不是颜色。 Can someone point me in the right direction? 有人能指出我正确的方向吗?
EDIT: I use this like so: myUILabel.attributedText = hintText
; 编辑:我这样使用: myUILabel.attributedText = hintText
;
When I'm trying to do something like this, I find it to be easier to build individual pieces (each of which is an NSAttributedString
) and then "glue" them together with something like the following: 当我尝试做这样的事情时,我发现构建单个部分(每个部分都是NSAttributedString
)更容易,然后将它们“粘合”在一起,如下所示:
NSAttributedString *string1 = // Get the red and large string here
NSAttributedString *string2 = // Get the just futura string here
NSMutableAttributedString *hintText = [[NSMutableAttributedString alloc] init];
[hintText appendAttributedString:string1];
[hintText appendAttributedString:string2];
I find this makes it much easier to follow the logical flow and I've never found this way to have performance limitations that needed optimization. 我发现这使得遵循逻辑流更加容易,我从未发现这种方式存在需要优化的性能限制。
FWIW, I got the following code to work as I believe the OP desires: FWIW,我得到以下代码,因为我相信OP的愿望:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSMutableAttributedString *hintText = [[NSMutableAttributedString alloc] initWithString:@"This is red and huge and this is not"]; //Red and large [hintText setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Futura-Medium" size:20.0f], NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(0, 20)]; //Rest of text -- just futura [hintText setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Futura-Medium" size:16.0f]} range:NSMakeRange(20, hintText.length - 20)]; [self.button setAttributedTitle:hintText forState:UIControlStateNormal]; }
Note that he was specifying [UIColor blackColor]
and I updated that to [UIColor redColor]
. 请注意,他正在指定[UIColor blackColor]
并将其更新为[UIColor redColor]
。 Also, I updated the range calculations to include all of the characters in "This is red and huge". 此外,我更新了范围计算,以包括“这是红色和巨大”中的所有字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.