iOS attributesText为5.1

时间:2013-07-25 13:14:18

标签: ios uicolor nsattributedstring nsmutableattributedstring

我如何在iOS 5.1中制作这样的作品 -

 NSString *statusStrColored = [NSString stringWithFormat:@"%@ (%@)", statusStr, paymentStatusStr];
            NSLog(@"stoka = %@", statusStrColored);
            //NSArray *components = [statusStrColored componentsSeparatedByString:@" "];
            NSRange greenRange = [statusStrColored rangeOfString:statusStr];
            NSRange redRange = [statusStrColored rangeOfString:paymentStatusStr];
            NSMutableAttributedString *attrString1 = [[NSMutableAttributedString alloc] initWithString:statusStrColored];

            [attrString1 beginEditing];
            [attrString1 addAttribute: (NSString*)kCTForegroundColorAttributeName
                               value:(id)[[UIColor greenColor] CGColor]
                               range:greenRange];

            [attrString1 addAttribute: (NSString*)kCTForegroundColorAttributeName
                               value:(id)[[UIColor redColor] CGColor]
                               range:redRange];

            [attrString1 endEditing];
            cell.textLabel.attributedText = attrString1;

在iOS 6中它的确定,但在5.1中...这不可用((

我的代码错误 -

 cell.textLabel.attributedText = attrString1;

它可能在iOS 5.1中有模拟吗?

2 个答案:

答案 0 :(得分:0)

使用DTCoreText查看向后兼容的属性字符串和HTMLish处理。

答案 1 :(得分:0)

我将文本分成两个标签,并执行此操作)) -

cell.statusInfo.text = [NSString stringWithFormat:@"%@", statusStr];
UIColor *statusColor = (bronInfo.status == csConfirmed ) ? [UIColor greenColor] : [UIColor redColor];
cell.statusInfo.textColor = statusColor;

cell.paymentInfo.text = [NSString stringWithFormat:@"(%@)", paymentStatusStr];
UIColor *payColor = (bronInfo.paymentStatus == psPaid ) ? [UIColor greenColor] : [UIColor redColor];
cell.paymentInfo.textColor = payColor;

Сode减少了!