在TTTAttributedLabel中执行某些操作后,将strikeThrough文本恢复为纯文本

时间:2013-05-10 06:39:41

标签: ios ipad

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

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无效时,我希望它没有通过。有什么建议?

1 个答案:

答案 0 :(得分: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