3

我目前正在努力解决在许多UITableViewCells 中显示删除线文本的需要。用 HTML 编写的东西看起来像

<strike>99 欧元</strike> 节省 50% => 现在 49 欧元

我不想UIWebView仅将 a 用于单行文本,尤其是在 small UITableViewCells 中使用它。我知道有可重复使用的单元格,但我想尽可能以更节省内存的方式保存。

所以...我在AliSoftware 的-replacementNSAttributedString的帮助下使用 s 。它仅从 iOS 4.0 开始可用这一事实没有问题,因为我们使用各种仅与 4.0 兼容的东西。UILabelOHAttributedLabel

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

基本上我是这样的:

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

NSMutableAttributedString *s = [NSMutableAttributedString attributesStringWithString:price];

[s addAttribute:NSStrikethroughStyleAttributeName 值:[NSNumber numberWithInteger:NSUnderlinePatternSolid | NSUnderlineStyleSingle] 范围:NSRangeFromString(price)];
[attributedLabel setAttributedText:s];

但是在这里,这三个NS*常量是未定义的。我已经导入CoreText.h, Foundation/NSAttributedString.h,但无济于事。我在网上的某个地方看到过 NSStrikethroughStyleAttributeName = @"NSStrikethroughStyleAttributeName",NSUnderlinePatternSolid = 0NSUnderlineStyleSingle = 1, 但是硬编码这些值并没有给出任何东西。我通过自动完成得到的一件事是等效的kCT...*常量,但是有kCTUnderlineStyleAttributeName, kCTStrokeWidthAttributeName, ... 但没有提到kCTStrikethrough_anything。

我应该怎么做才能显示 *$|#!@ 一条删除线?

4

2 回答 2

7

在 iOS 6 中,您可以使用 NSStrikethroughStyleAttributeName

[attributedString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:selectedRange];

虽然它可能看起来不合适,但 numberWithInt 值作为 NSUnderlineStyleSingle 是正确的。

于 2012-12-05T22:29:54.343 回答
2

一个更简单的方法可能是两个标签,使用这个问题的答案 - UILabel 中文本的像素宽度- 删除其中一个标签中的文本。

于 2011-10-09T12:14:22.767 回答