2012-10-30 59 views
72

擊中我想創建一個UILabel中文字是這樣的的UILabel與文本通過

enter image description here

我怎樣才能做到這一點?當文字很小時,該行也應該很小。

+1

可能的重複:http://stackoverflow.com/questions/10550732/font-with-strike-through-it – mopsled

+0

如果你只需要iOS 6的支持,那麼你可以使用'NSAttributedString'和'UILabel屬性文本'屬性。 – rmaddy

回答

130

SWIFT CODE

let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text") 
    attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length)) 

則:

yourLabel.attributedText = attributeString 

爲了使串罷工的某些部分,然後提供一系列

let somePartStringRange = (yourStringHere as NSString).range(of: "Text") 
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: somePartStringRange) 

目標C

的iOS 6.0>UILabel支持NSAttributedString

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

夫特

let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your String here") 
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length)) 

定義

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)aRange 

Parameters List:

:一個字符串,指定屬性名稱。屬性鍵可以由另一個框架提供,也可以是您定義的定製鍵。有關在何處查找系統提供的屬性鍵的信息,請參閱NSAttributedString類參考中的概述部分。

value:與名稱關聯的屬性值。

aRange:指定屬性/值對應用的字符範圍。

然後

yourLabel.attributedText = attributeString; 

對於​​3210你需要3-rd party component做到這一點。 其中之一是TTTAttributedLabel,另一個是OHAttributedLabel

+0

對於iOS 5.1.1較小版本我如何使用3方歸因標籤來顯示屬性文本:? – Dev

+0

查看編輯答案 –

+0

你可以建議一個好的Toutorial?你提供的鏈接有點難以理解.. :( – Dev

7

可以使用NSMutableAttributedString做到在IOS 6。

NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:@"$198"]; 
[attString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0,[attString length])]; 
yourLabel.attributedText = attString; 
28

我喜歡NSAttributedString而不是NSMutableAttributedString對於這個簡單的例子:

NSAttributedString * title = 
    [[NSAttributedString alloc] initWithString:@"$198" 
            attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)}]; 
[label setAttributedText:title]; 

常量同時指定一個屬性串的NSUnderlineStyleAttributeNameNSStrikethroughStyleAttributeName屬性:

typedef enum : NSInteger { 
    NSUnderlineStyleNone = 0x00, 
    NSUnderlineStyleSingle = 0x01, 
    NSUnderlineStyleThick = 0x02, 
    NSUnderlineStyleDouble = 0x09, 
    NSUnderlinePatternSolid = 0x0000, 
    NSUnderlinePatternDot = 0x0100, 
    NSUnderlinePatternDash = 0x0200, 
    NSUnderlinePatternDashDot = 0x0300, 
    NSUnderlinePatternDashDotDot = 0x0400, 
    NSUnderlineByWord = 0x8000 
} NSUnderlineStyle; 
19

SWIFT CODE

let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text") 
    attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length)) 

然後:

yourLabel.attributedText = attributeString 

由於Prince answer)以下代碼

+0

上帝,我討厭迅捷的語法。但謝謝你的答覆。 –

0

使用

NSString* strPrice = @"£399.95"; 

NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:strPrice]; 

[finalString addAttribute: NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleSingle] range: NSMakeRange(0, [titleString length])]; 
self.lblOldPrice.attributedText = finalString; 
20

在夫特,使用用於單刪除線樣式枚舉:

let attrString = NSAttributedString(string: "Label Text", attributes: [NSStrikethroughStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue]) 
label.attributedText = attrString 

附加刪除線樣式(記住訪問使用.rawValue枚舉):

  • NSUnderlineStyle.StyleSingle

    • NSUnderlineStyle.StyleNone
    • NSUnderlineStyle.StyleThick
    • NSUnderlineStyle.StyleDouble

    刪除線圖案(與樣式進行或編輯):

    • NSUnderlineStyle.PatternDot
    • NSUnderlineStyle.PatternDash
    • NSUnderlineStyle.PatternDashDot
    • NSUnderlineStyle.PatternDashDotDot

    指定刪除線應當僅跨越字(而不是空格)被應用:

    • NSUnderlineStyle.ByWord
  • +1

    向上投票使用正確的常量而不是數字 –

    1

    對於任何人只要看到如何做到這一點的實現代碼如下細胞(SWIFT),你必須設置.attributeText這樣的:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
        let cell = tableView.dequeueReusableCellWithIdentifier("TheCell")! 
    
        let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: message) 
        attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length)) 
    
        cell.textLabel?.attributedText = attributeString 
    
        return cell 
        } 
    

    如果你想刪除的刪除線做否則它會堅持!:

    cell.textLabel?.attributedText = nil 
    
    0

    對於那些誰面臨的問題與多行文本罷工

    let attributedString = NSMutableAttributedString(string: item.name!) 
        //necessary if UILabel text is multilines 
        attributedString.addAttribute(NSBaselineOffsetAttributeName, value: 0, range: NSMakeRange(0, attributedString.length)) 
        attributedString.addAttribute(NSStrikethroughStyleAttributeName, value: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue), range: NSMakeRange(0, attributedString.length)) 
        attributedString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.darkGray, range: NSMakeRange(0, attributedString.length)) 
    
        cell.lblName.attributedText = attributedString 
    
    2

    SWIFT 4

    let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text Goes Here") 
        attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 0, range: NSMakeRange(0, attributeString.length)) 
        self.lbl_productPrice.attributedText = attributeString 
    
    1

    在斯威夫特的iOS重拳出擊的UILabel文本。請試試這個它的工作對我來說

    let attributedString = NSMutableAttributedString(string:"12345") 
             attributedString.addAttribute(NSAttributedStringKey.baselineOffset, value: 0, range: NSMakeRange(0, attributedString.length)) 
             attributedString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSNumber(value: NSUnderlineStyle.styleThick.rawValue), range: NSMakeRange(0, attributedString.length)) 
             attributedString.addAttribute(NSAttributedStringKey.strikethroughColor, value: UIColor.gray, range: NSMakeRange(0, attributedString.length)) 
    
    yourLabel.attributedText = attributedString 
    

    你可以改變你的 「strikethroughStyle」 像styleSingle,styleThick,styleDouble enter image description here

    0

    創建字符串擴展,並添加以下方法

    static func makeSlashText(_ text:String) -> NSAttributedString { 
    
    
    let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: text) 
         attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length)) 
    
    return attributeString 
    
    } 
    

    然後用於這樣的標籤

    yourLabel.attributedText = String.makeSlashText("Hello World!")