2014-03-25 56 views
12

我使用下面的代碼通過文字的UILabel文字刪除與顏色

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:(text ? text : @"")]; 
[attributeString addAttribute:NSStrikethroughStyleAttributeName 
         value:@2 
         range:NSMakeRange(0, [attributeString length])]; 

label.attributedText = attributeString; 

文字罷工有白色,因此刪除線也是白色的。我想將其改爲紅色。我該怎麼做?

回答

15

按照NSAttributedString UIKit Additions Reference,你可以設置

NSStrikethroughColorAttributeName 

屬性到您選擇的UIColor

+0

謝謝。這做了[attributeString addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,[attributeString length])]; –

11

添加 「NSStrikethroughColorAttributeName」 屬性字符串

[attributedString addAttributes:@{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle) 
           , NSStrikethroughColorAttributeName: [UIColor redColor] 
           , NSBackgroundColorAttributeName: [UIColor yellowColor]} 

文檔是here

3

在斯威夫特:

attributeString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, attributeString.length)) 
+0

請縮小代碼塊(使用四個空格或選擇代碼並單擊編輯器工具欄上的「{}」按鈕)。 – BM5k

6

SWIFT 3.X

要用顏色刪除文本標籤,需要2個屬性,一個用於NSStrikethroughStyleAttributeName,一個用於NSStrikethroughColorAttributeName。我覺得它更可讀。

let attributedString = NSMutableAttributedString(string: "0,91 €") 
    attributedString.addAttribute(NSStrikethroughStyleAttributeName, value: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue), range: NSMakeRange(0, attributedString.length)) 
    attributedString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.red, range: NSMakeRange(0, attributedString.length))