0

我正在使用此代码更改标签的颜色并将文本设置为删除线:

sliderlabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(10, 260, 310, 30)];

sliderlabel.font = [UIFont fontWithName:@"Optima-Bold" size:14];
[sliderlabel setTag:112];
sliderlabel.lineBreakMode = UILineBreakModeWordWrap;
[sliderlabel setBackgroundColor:[UIColor clearColor]];

NSString *sliderlabeltext = [NSString stringWithFormat:@"Change To: In-Progress (%d %%)",(int)slider.value];
[sliderlabel setText:sliderlabeltext afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
    NSRange boldRange = [[mutableAttributedString string] rangeOfString:[NSString stringWithFormat:@"In-Progress (%d %%)",(int)slider.value] options:NSCaseInsensitiveSearch];
    NSRange strikeRange = [[mutableAttributedString string] rangeOfString:sliderlabeltext options:NSCaseInsensitiveSearch];
    UIFont *boldSystemFont = [UIFont fontWithName:@"Optima-Bold" size:14];
    CTFontRef font = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
    if (font) {
        [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor colorWithRed:8/255.0 green:156/255.0 blue:94/255.0 alpha:1.0].CGColor range:boldRange];//34-139-34
        [mutableAttributedString addAttribute:kTTTStrikeOutAttributeName value:[NSNumber numberWithBool:YES] range:strikeRange];
        CFRelease(font);
    }

    return mutableAttributedString;
}];
[self.view addSubview:sliderlabel];
[sliderlabel release];

现在我希望它在执行某些操作(例如单击按钮)时没有删除线,传入[NSNumber numberWithBool:NO]不起作用addAttribute:value:range。有什么建议么?

4

1 回答 1

0

试试这个添加。

@implementation TTTAttributedLabel (Additions)

- (void)setStrikeThroughOn:(BOOL)isStrikeThrough {
    NSString* text = self.text;
    [self setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^        NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
    NSRange strikeRange = [[mutableAttributedString string] rangeOfString:text options:NSCaseInsensitiveSearch];
    [mutableAttributedString addAttribute:kTTTStrikeOutAttributeName value:[NSNumber numberWithBool:isStrikeThrough] range:strikeRange];
    return mutableAttributedString;
}];

// must trigger redraw
[self setNeedsDisplay];
}

@end
于 2013-05-22T01:39:37.727 回答