我正在尝试使用删除线创建一个属性字符串。我可以设置其他属性,如前景色和字体大小,但当我尝试通过部分文本设置删除线时,文本的那一部分会消失。什么可能导致它的想法?
以下是代码。谢谢你的期待!
// ...
//Price
NSLog(@"RESULT NUMBER %d", cell.result.resultId);
priceString = (priceString == nil) ? @"$150.00\n$100.00" : priceString;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:priceString];
NSRange linebreak = [priceString rangeOfString:@"\n"];
if (linebreak.location != NSNotFound) {
[attributedString beginEditing];
// RegPrice
NSRange firstLine = NSMakeRange(0, linebreak.location);
// [attributedString addAttribute:NSFontAttributeName
// value:[UIFont boldSystemFontOfSize:11]
// range:firstLine];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1]
range:firstLine];
@try {
[attributedString addAttribute:NSStrikethroughStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:firstLine];
} @catch (NSException *e) {
NSLog(@"ATTRIBUTE EXCEPTION: %@", e);
}
// Sale Price
[attributedString addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:14]
range:NSMakeRange(linebreak.location + 1, priceString.length - (linebreak.location + 1))];
[attributedString endEditing];
}
cell.lblPrice.attributedText = attributedString;
return cell;
}
答案 0 :(得分:1)
我遇到了同样的字符串消失问题,直到我意识到NSStrikethroughStyleAttributeName
的值应该是NSNumber。
因此上面的代码应如下所示:
目标-C
[attributedString addAttribute:NSStrikethroughStyleAttributeName
value: [NSNumber numberWithInt: NSUnderlineStyleSingle]
range:firstLine];
在Swift 4中,使用类型[NSAttributedStringKey: Any]
的字典传递属性,并且删除线样式属性如下所示:
[NSAttributedStringKey.strikethroughStyle: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue)]
答案 1 :(得分:0)
对我来说boldSystemFontOfSize:返回一个HelveticaNeueInterface-MediumP4字体的字体对象。你确定这个字体有一个删除线?并非所有字体都可以。也许尝试使用不同的字体,你知道它有一个突破。
答案 2 :(得分:0)
我遇到的问题是删除线文字仅在iOS 7.0中的UILabel中消失。它在iOS 6.1和7.1中运行良好。
iOS 7.0中似乎存在一个错误,如果标签对于文本来说太高,则删除文本会消失一定量。当我将标签的框架设置为足够小的高度或在设置attributedText后调用sizeToFit时,它可以工作。