iPhone上的CoreText和删除线
我目前正在努力在许多UITableViewCell
显示删除线文本的需要。 用HTML编写的东西就像looke一样
<strike>€99 </ strike>保存50%=>现在€49
我不想仅仅为一行文本使用UIWebView
,特别是在小型UITableViewCell
使用它。 我知道有可重复使用的单元格,但是我想保留更有效的内存方式。
所以…我使用的是NSAttributedString
,在AliSoftware的UILabel
OHAttributedLabel
OHAttributedLabel的帮助下。 事实上,只有从iOS 4.0开始才是可用的,因为我们使用各种与4.0兼容的东西。
我可以设法创build属性string,它显示OHAttributedLabel
中的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)]; [归档标签setAttributedText:s];
但是在这里,三个NS*
常量是不确定的。 我已经导入CoreText.h
, Foundation/NSAttributedString.h
,无济于事。 我在网上看到了NSStrikethroughStyleAttributeName = @"NSStrikethroughStyleAttributeName"
, NSUnderlinePatternSolid = 0
和NSUnderlineStyleSingle = 1
,但是对这些值进行硬编码不会给任何东西。 有一件事,我得到的自动完成是相当于kCT...*
常量,但有kCTUnderlineStyleAttributeName
, kCTStrokeWidthAttributeName
,…但没有提到kCTStrikethrough
_anything。
我该怎么做才能显示那个* $ |#!@一条穿透文本?
在iOS 6中,您可以使用NSStrikethroughStyleAttributeName
[attributedString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:selectedRange];
虽然它看起来不合适,但numberWithInt值与NSUnderlineStyleSingle一样正确。
一个更简单的方法可能是两个标签,使用这个问题的答案 – UILabel中文本的像素宽度 – 删除其中一个标签中的文本。