目标C中的Strike-through字体

wal*_*wal 19 iphone fonts objective-c uitableview

如何在目标C中使用删除字体 ??? 更具体地说,在UITableViewCell中

cell.textLabel.text = name;
cell.detailTextLabel.text = quantity ;
cell.XXX = ??
Run Code Online (Sandbox Code Playgroud)

Mar*_*rov 50

这是Marin,"iOS6 by Tutorials"中属于字符串章节的作者.

从iOS6开始,实际上支持一堆不同的文本属性,包括删除线.

这是一个简短的示例,您可以将其用于单元格文本标签:

NSDictionary* attributes = @{
  NSStrikethroughStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]
};

NSAttributedString* attrText = [[NSAttributedString alloc] initWithString:@"My Text" attributes:attributes];
cell.textLabel.attributedText = attrText;
Run Code Online (Sandbox Code Playgroud)

就这样.祝好运!


Bra*_*nar 9

编辑:从iOS 6开始,此答案已过期.请参阅下面的最新答案

对于删除线或下划线字体没有原生支持.您必须自己在标签的视图上绘制线条.

这很愚蠢,因为IB的字体检查器具有设置删除和下划线的选项,但如果您尝试设置它们,则会立即忽略这些选项.

  • 现在可以使用iOS 6.请参阅Ican的答案. (2认同)

Zub*_*air 5

1 - 获取需要删除的文本大小

CGSize expectedLabelSize = [string sizeWithFont:cell.titleLabel.font constrainedToSize:cell.titleLabel.frame.size lineBreakMode:UILineBreakModeClip];
Run Code Online (Sandbox Code Playgroud)

2 - 创建一条线并将其添加到文本中

UIView *viewUnderline = [[UIView alloc] init];
viewUnderline.frame = CGRectMake(20, 12, expectedLabelSize.width, 1);
viewUnderline.backgroundColor = [UIColor grayColor];
[cell addSubview:viewUnderline];
[viewUnderline release]; 
Run Code Online (Sandbox Code Playgroud)