簡體   English   中英

NSStrikethroughStyleAttributeName,如何在iOS 10.3中刪除字符串?

[英]NSStrikethroughStyleAttributeName , How to strike out the string in iOS 10.3?

在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

創建於:2017年3月14日

發起於:2017年3月14日

打開雷達鏈接: http : //www.openradar.appspot.com/31034683

雷達狀態為當前打開狀態

您也可以在這里看到替代示例可能很有用。

我在開發人員論壇上找到了一種解決方法,該方法對我有用。 NSBaselineOffsetAttributeName添加到字符串屬性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)];

從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錯誤。

我們需要一個立即的解決方法,以防萬一有人在尋找提示:我們的Label通過NSMutableAttributedStringNSMutableParagraphStyle設置了屬性。 使用no /“空”段落樣式(沒有設置任何屬性的實例)時,不會發生該錯誤。

因此,在這種情況下,省略段落樣式並解決隨后缺少的段落屬性對我們來說解決了這個問題。

只需使用此:-

NSMutableAttributedString * costPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@“₹%@”,strDetails]]; [costPrice addAttribute:NSBaselineOffsetAttributeName值:[NSNumber numberWithInteger:NSUnderlineStyleSingle]范圍:NSMakeRange(0,costPrice.length)];

這是臨時解決方案。 希望能奏效

***您可以將其傳遞給功能並享受!!!

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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM