NSStrikethroughStyleAttributeName,如何在iOS 10.3中敲出string?
我已经在iOS 10.3发布之前使用了这一行代码,并且工作正常。
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@",strMRP,strOffer]]; [attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:NSMakeRange(0, strMRP.length)]; [attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:NSMakeRange(strMRP.length, strOffer.length+1)]; [attributeString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleDouble] range:NSMakeRange(0,strMRP.length)];
但现在停止工作了,有没有其他方法可以做出罢工?
这是iOS 10.3中的错误 , NSStrikethroughStyleAttributeName
(任何NSUnderlineStyle
情况)在iOS SDK 10.3上不再工作。
如果有人发现有关这个更新的答案,请通知在这里,我会更新我的答案。
产品版本:10.3
创builddate:2017年3月14日
发源地:2017年3月14日
打开雷达链接: http : //www.openradar.appspot.com/31034683
雷达状态为当前打开状态
你可以看到替代样本也可能是有用的。
我在开发者论坛上find了一个解决方法,这对我很有用。 添加NSBaselineOffsetAttributeName
到string属性解决了这个问题:)
它工作正常
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@",strMRP,strOffer]]; [attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:NSMakeRange(0, strMRP.length)]; [attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:NSMakeRange(strMRP.length, strOffer.length+1)]; [attributeString addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleNone] range:NSMakeRange(0,strMRP.length)]; [attributeString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleDouble] range:NSMakeRange(0,strMRP.length)];
iOS 10.3以后,你需要添加NSBaselineOffsetAttributeName。
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@",strMRP,strOffer]]; [attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:NSMakeRange(0, strMRP.length)]; [attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:NSMakeRange(strMRP.length, strOffer.length+1)]; [attributeString addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleNone] range:NSMakeRange(0,strMRP.length)]; [attributeString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleDouble] range:NSMakeRange(0,strMRP.length)];
一旦添加NSBaselineOffsetAttributeName,那么它适用于单行,双行等。
如上所述,这是一个iOS 10.3的错误。
我们需要一个即时的解决方法,以防万一任何人在寻找提示:我们的标签通过NSMutableAttributedString
和NSMutableParagraphStyle
设置了属性。 使用没有/“空”段落样式(没有任何属性设置的实例)时,错误没有发生。
所以在这种情况下,省略段落样式和解决当时丢失的段落属性为我们解决了这个问题。
只需使用这个: –
NSMutableAttributedString * costPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@“‰%@”,strDetails]]; [costPrice addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0,costPrice.length)];
这是临时解决scheme。 希望它的作品
***你可以通过它function和享受!
func customString(currentprice:String,oldPrice:String) -> NSMutableAttributedString{ // 1 let NewString = currentprice + " " + oldPrice let string = NewString as NSString let attributedString = NSMutableAttributedString(string: string as String) // 2 let firstAttributes = [NSForegroundColorAttributeName: UIColor(red: 238/255, green: 140/255, blue: 84/255, alpha: 1),NSBaselineOffsetAttributeName:1] let secondAttributes = [NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSStrikethroughStyleAttributeName: 1] // 3 attributedString.addAttributes(firstAttributes, range: string.rangeOfString(currentprice)) attributedString.addAttributes(secondAttributes, range: string.rangeOfString(oldPrice)) return attributedString }
并使用像:
YourUILabel.attributedText = customString("300", oldPrice: "400")