回答
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。
可以使用NSMutableAttributedString做到在IOS 6。
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:@"$198"];
[attString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0,[attString length])];
yourLabel.attributedText = attString;
我喜歡NSAttributedString
而不是NSMutableAttributedString
對於這個簡單的例子:
NSAttributedString * title =
[[NSAttributedString alloc] initWithString:@"$198"
attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)}];
[label setAttributedText:title];
常量同時指定一個屬性串的NSUnderlineStyleAttributeName
和NSStrikethroughStyleAttributeName
屬性:
typedef enum : NSInteger {
NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick = 0x02,
NSUnderlineStyleDouble = 0x09,
NSUnderlinePatternSolid = 0x0000,
NSUnderlinePatternDot = 0x0100,
NSUnderlinePatternDash = 0x0200,
NSUnderlinePatternDashDot = 0x0300,
NSUnderlinePatternDashDotDot = 0x0400,
NSUnderlineByWord = 0x8000
} NSUnderlineStyle;
SWIFT CODE
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
然後:
yourLabel.attributedText = attributeString
由於Prince answer)以下代碼
上帝,我討厭迅捷的語法。但謝謝你的答覆。 –
使用
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;
在夫特,使用用於單刪除線樣式枚舉:
let attrString = NSAttributedString(string: "Label Text", attributes: [NSStrikethroughStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue])
label.attributedText = attrString
種
附加刪除線樣式(記住訪問使用.rawValue枚舉):
- NSUnderlineStyle.StyleNone
- NSUnderlineStyle.StyleThick
- NSUnderlineStyle.StyleDouble
刪除線圖案(與樣式進行或編輯):
- NSUnderlineStyle.PatternDot
- NSUnderlineStyle.PatternDash
- NSUnderlineStyle.PatternDashDot
- NSUnderlineStyle.PatternDashDotDot
指定刪除線應當僅跨越字(而不是空格)被應用:
- NSUnderlineStyle.ByWord
向上投票使用正確的常量而不是數字 –
對於任何人只要看到如何做到這一點的實現代碼如下細胞(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
對於那些誰面臨的問題與多行文本罷工
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
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
在斯威夫特的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
創建字符串擴展,並添加以下方法
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!")
- 1. UILabel與更多...文本
- 2. 的UILabel文本接到通知
- 3. 的UILabel文本字符串與
- 4. 的UILabel doubble文本
- 5. 文本中的UILabel
- 6. UILabel在UILabel文本的末尾
- 7. UILabel文本爲html文本
- 8. 縮放UILabel文本與父視圖
- 9. 如何通過NSUserdefualts UILabel?
- 10. UILabel適合文本
- 11. UILabel文本概述
- 12. 展開UILabel文本
- 13. UILabel文本移動
- 14. 多色UILabel文本
- 15. UILabel文本問題
- 16. UILabel被截斷的文本
- 17. 設置UILabel的文本
- 18. 覆蓋UILabel中的'文本'
- 19. 坐落在文本的UILabel
- 20. UILabel的文本截斷
- 21. 使UILabel的文本加粗
- 22. 下劃線的UILabel文本
- 23. 每行上的UILabel文本
- 24. UILabel中的中心文本
- 25. UILabel上的文本填充
- 26. UILabel文本內的圖像
- 27. 通過文本
- 28. 迅速IOS改過來的UILabel文本值和超過
- 29. 如何通過jQuery與文本對齊?
- 30. 字體不通過的UILabel擴展
可能的重複:http://stackoverflow.com/questions/10550732/font-with-strike-through-it – mopsled
如果你只需要iOS 6的支持,那麼你可以使用'NSAttributedString'和'UILabel屬性文本'屬性。 – rmaddy