如何在TTTAttributedLabel中设置范围的背景颜色

时间:2014-05-27 14:43:18

标签: tttattributedlabel

无法弄清楚如何设置范围的背景颜色以使其看起来像荧光笔

下面的黄色不会显示为背景颜色

NSArray *keys = [[NSArray alloc] initWithObjects:(id)kCTForegroundColorAttributeName,(id)kCTUnderlineStyleAttributeName, (id) NSBackgroundColorAttributeName, nil];
NSArray *objects = [[NSArray alloc] initWithObjects:[UIColor redColor],[NSNumber numberWithInt:kCTUnderlineStyleNone], [UIColor yellowColor], nil];
NSDictionary *linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];

self.madLibLabel.linkAttributes = linkAttributes;


[self.madLibLabel addLinkToURL:[NSURL URLWithString:@"what://"] withRange:[self.madLibLabel.text rangeOfString:@"WHAT"]];

1 个答案:

答案 0 :(得分:1)

您应该使用kTTTBackgroundFillColorAttributeName密钥而不是NSBackgroundColorAttributeName

以下是代码段:

NSArray *keys = [[NSArray alloc] initWithObjects:(id)kCTForegroundColorAttributeName,(id)kCTUnderlineStyleAttributeName, (id) kTTTBackgroundFillColorAttributeName, nil];
NSArray *objects = [[NSArray alloc] initWithObjects:[UIColor redColor],[NSNumber numberWithInt:kCTUnderlineStyleNone], [UIColor yellowColor], nil];
NSDictionary *linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];

self.madLibLabel.linkAttributes = linkAttributes;


[self.madLibLabel addLinkToURL:[NSURL URLWithString:@"what://"] withRange:[self.madLibLabel.text rangeOfString:@"WHAT"]];