iPhone上的CoreText和删除线

Cyr*_*lle 3 iphone nsattributedstring strikethrough core-text ios

我目前正在努力解决在许多人中显示删除线文本的需要UITableViewCell.用HTML编写的东西就像是一样

<strike>€99</strike> save 50% => now €49

我不想只使用UIWebView一行文本,特别是它用于小UITableViewCells.我知道有可重复使用的单元格,但是我希望保持内存效率更高的方式.

所以...我使用的是NSAttributedStringS,与AliSoftware的的帮助UILabel-更换OHAttributedLabel.事实上它只能从iOS 4.0开始使用是没有问题的,因为我们使用的只是4.0兼容的各种东西.

我可以设法创建属性字符串,它显示文本OHAttributedLabel,好的,这很酷.但我无法实现的是设置"删除线"或"删除线"属性.

基本上我是这样的:

NSString *price = [NSString stringWithFormat:@"%01.2f €", product.price];
NSString *rate = [NSString stringWithFormat:@" -%01.0f%%", product.reductionRate];

NSMutableAttributedString *s = [NSMutableAttributedString attributedStringWithString:price];

[s addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlinePatternSolid | NSUnderlineStyleSingle] range:NSRangeFromString(price)];
[attributedLabel setAttributedText:s];

但是在这里,三个NS*常数是未定义的.我进口了CoreText.h,Foundation/NSAttributedString.h但无济于事.我在什么地方看到,在网络上 NSStrikethroughStyleAttributeName = @"NSStrikethroughStyleAttributeName",并且NSUnderlinePatternSolid = 0NSUnderlineStyleSingle = 1,但硬编码这些值不放弃任何东西.有一件事我与自动完成得到的都是等价kCT...*的常量,但也有kCTUnderlineStyleAttributeName,kCTStrokeWidthAttributeName,...但没有提及kCTStrikethrough_anything.

我该怎么做才能显示*$ |#!@一段删除文字?

Den*_*Vog 7

使用iOS 6,您可以使用NSStrikethroughStyleAttributeName

[attributedString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:selectedRange];
Run Code Online (Sandbox Code Playgroud)

虽然它可能看起来不合适,但numberWithInt值与NSUnderlineStyleSingle一样正确.

  • 尽管如此,如果使用Core Text来渲染属性字符串,它也不会呈现删除线,即使在iOS7.1中也是如此 (2认同)