Frage

iOS7 addAttribute:(NSString *)NSForegroundColorAttributeName value: can change label.text color but in iOS6 will crash

iOS6 addAttribute:(NSString *)kCTForegroundColorAttributeName value: can not change label.text.color

How can I do?

- (void)userNoPayCreateFreeLabel
{
    UILabel *freeLabel = [[UILabel alloc] initWithFrame:CGRectMake(
                                                        0,
                                                        NAVIGATIONBAR_HEIGHT,
                                                        self.view.bounds.size.width,
                                                        28)];
    NSString *text = @"购买课程将享有高质量答疑,你可免费体验3次";
    freeLabel.attributedText = [self changeLableTextColorStr:text];
    freeLabel.backgroundColor = [CommUtls colorWithHexString:@"#fdfbf1"];
    freeLabel.font = [UIFont systemFontOfSize:13];
    freeLabel.textAlignment = 1;
    self.freeLabel = freeLabel;
    [self.view addSubview:freeLabel];
}

- (NSMutableAttributedString *)changeLableTextColorStr:(NSString *)text
{
    NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc] initWithString:text];
    NSRange range = NSMakeRange([aStr length] - 2, 1);
    if (IsIOS7) {
        [aStr addAttribute:(NSString *)NSForegroundColorAttributeName value:
                                                (id)[UIColor redColor].CGColor range:range];
    }
    else {
        [aStr addAttribute:(NSString *)kCTForegroundColorAttributeName value:
                                                (id)[UIColor redColor].CGColor range:range];
    }
    return aStr;
}
War es hilfreich?

Lösung

Try this,

[aStr addAttribute:NSStrokeColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, aStr.length)];
[aStr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, aStr.length)];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top