问题描述
我目前正在努力在许多UITableViewCell
中显示删除线文本.用HTML编写的内容看起来像
I'm currently struggling with the need to display strikethrough text in many UITableViewCell
s. Something that written in HTML would looke like
<strike>€99</strike> save 50% => now €49
我不想只将UIWebView
用于一行文本,特别是在小的UITableViewCell
中使用它.我知道有可重用的单元格以及所有单元格,但是我想让事情尽可能以更节省内存的方式进行.
I don't want to use a UIWebView
just for a single line of text, especially that it's used in small UITableViewCell
s. I know there are reusable cells and all, but I'd like to keep things the more memory-efficient way possible.
所以...在AliSoftware的UILabel
-替换OHAttributedLabel
的帮助下,我正在使用NSAttributedString
.仅从iOS 4.0开始才可用的事实没有问题,因为我们仅使用与4.0兼容的所有内容.
So... I'm using NSAttributedString
s, with the help of AliSoftware's UILabel
-replacement OHAttributedLabel
. The fact that it's only available starting with iOS 4.0 is no problem, as we use all kinds of stuff only 4.0-compatible.
我可以设法创建属性字符串,它在OHAttributedLabel
中显示文本,确定,很酷.但是我无法实现的是设置"strikeout"或"strikethrough"属性.
I can manage to create the attributed string, it displays text in the OHAttributedLabel
, OK, that's cool. But what I can't achieve is setting the "strikeout", or "strikethrough" attribute.
基本上我是这样的:
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 = 0
和NSUnderlineStyleSingle = 1
,但是对这些值进行硬编码不会产生任何结果.我用自动补全得到的一件事是等效的kCT...*
常量,但是有kCTUnderlineStyleAttributeName
,kCTStrokeWidthAttributeName
,...,但是没有提到kCTStrikethrough
_anything.
But here, the three NS*
constants are undefined. I've imported CoreText.h
, Foundation/NSAttributedString.h
, to no avail. I've seen somewhere on the web thatNSStrikethroughStyleAttributeName = @"NSStrikethroughStyleAttributeName"
, and that NSUnderlinePatternSolid = 0
and NSUnderlineStyleSingle = 1
, but hard-coding these values don't give anything.One thing I got with auto-completion are the equivalent kCT...*
constants, but there are kCTUnderlineStyleAttributeName
, kCTStrokeWidthAttributeName
, ... but no mention of kCTStrikethrough
_anything.
我应该怎么显示* $ |#!@删除线文本?
What should I do to display that *$|#!@ piece of strike-through text ?
推荐答案
在iOS 6中,您可以使用NSStrikethroughStyleAttributeName
With iOS 6 you can use NSStrikethroughStyleAttributeName
[attributedString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:selectedRange];
虽然看起来不合适,但numberWithInt值正确为NSUnderlineStyleSingle.
While it may seem out of place, the numberWithInt value is correct as NSUnderlineStyleSingle.
这篇关于iPhone上的CoreText和删除线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!