问题
I have used this line of code before release of iOS 10.3 ,and worked fine.
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)];
But now it is stopped working ,is there any alternate way to do the strike out ?
回答1:
it is the bug in iOS 10.3 , NSStrikethroughStyleAttributeName
(any NSUnderlineStyle
cases) is not working any more on iOS SDK 10.3.
if anyone found the updated answer related to this , please inform here, I will update my answer.
Product Version: 10.3
Created: 14-Mar-2017
Originated: 14-Mar-2017
Open Radar Link: http://www.openradar.appspot.com/31034683
Radar status is Currently Open state
you can see the alternate sample also here may be it useful.
回答2:
I found one workaround on developer forum, which works for me. Adding of NSBaselineOffsetAttributeName
to string attributes fixed this problem :)
回答3:
It work fine with
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)];
回答4:
iOS 10.3 onward you need to add 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)];
Once you add NSBaselineOffsetAttributeName then it works for single line, double line ect.
回答5:
As mentioned above, this is an iOS 10.3 bug.
We needed an immediate workaround, so just in case anyone is looking for hints:
Our Label had attributes set through both NSMutableAttributedString
as well as NSMutableParagraphStyle
. The bug did not occur when using no / an "empty" paragraph style (instance without any properties set).
So in this scenario, omitting the paragraph style and working around the then missing paragraph properties resolved the issue for us.
回答6:
Just Use this :-
NSMutableAttributedString *costPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"₹ %@",strDetails]]; [costPrice addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleSingle] range:NSMakeRange(0,costPrice.length)];
This is temp solution . Hope it works
回答7:
***You can pass it to function & Enjoy !!!
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
}
and use like:
YourUILabel.attributedText = customString("300", oldPrice: "400")
来源:https://stackoverflow.com/questions/43070335/nsstrikethroughstyleattributename-how-to-strike-out-the-string-in-ios-10-3