Deprecated text style UILabel swift3 [duplicate] - ios
I want to create a UILabel in which the text is like this
How can I do this? When the text is small, the line should also be small.
SWIFT 5 UPDATE CODE
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSRange(location: 0, length: attributeString.length))
then:
yourLabel.attributedText = attributeString
To make some part of string to strike then provide range
let somePartStringRange = (yourStringHere as NSString).range(of: "Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: somePartStringRange)
Objective-C
In iOS 6.0 > UILabel supports NSAttributedString
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:#"Your String here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:#2
range:NSMakeRange(0, [attributeString length])];
Swift
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your String here")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
Definition :
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)aRange
Parameters List:
name : A string specifying the attribute name. Attribute keys can be supplied by another framework or can be custom ones you define. For information about where to find the system-supplied attribute keys, see the overview section in NSAttributedString Class Reference.
value : The attribute value associated with name.
aRange : The range of characters to which the specified attribute/value pair applies.
Then
yourLabel.attributedText = attributeString;
For lesser than iOS 6.0 versions you need 3-rd party component to do this.
One of them is TTTAttributedLabel, another is OHAttributedLabel.
In Swift, using the single strikethrough line style:
let attributedText = NSAttributedString(
string: "Label Text",
attributes: [.strikethroughStyle: NSUnderlineStyle.single.rawValue]
)
label.attributedText = attributedText
Additional strikethrough styles (Remember to use the .rawValue):
.none
.single
.thick
.double
Strikethrough patterns (to be OR-ed with the style):
.patternDot
.patternDash
.patternDashDot
.patternDashDotDot
Specify that the strikethrough should only be applied across words (not spaces):
.byWord
I prefer NSAttributedString rather than NSMutableAttributedString for this simple case:
NSAttributedString * title =
[[NSAttributedString alloc] initWithString:#"$198"
attributes:#{NSStrikethroughStyleAttributeName:#(NSUnderlineStyleSingle)}];
[label setAttributedText:title];
Constants for specifying both the NSUnderlineStyleAttributeName and NSStrikethroughStyleAttributeName attributes of an attributed string:
typedef enum : NSInteger {
NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick = 0x02,
NSUnderlineStyleDouble = 0x09,
NSUnderlinePatternSolid = 0x0000,
NSUnderlinePatternDot = 0x0100,
NSUnderlinePatternDash = 0x0200,
NSUnderlinePatternDashDot = 0x0300,
NSUnderlinePatternDashDotDot = 0x0400,
NSUnderlineByWord = 0x8000
} NSUnderlineStyle;
Strikethrough in Swift 5.0
let attributeString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle,
value: NSUnderlineStyle.single.rawValue,
range: NSMakeRange(0, attributeString.length))
self.yourLabel.attributedText = attributeString
It worked for me like a charm.
Use it as extension
extension String {
func strikeThrough() -> NSAttributedString {
let attributeString = NSMutableAttributedString(string: self)
attributeString.addAttribute(
NSAttributedString.Key.strikethroughStyle,
value: NSUnderlineStyle.single.rawValue,
range:NSMakeRange(0,attributeString.length))
return attributeString
}
}
Call like this
myLabel.attributedText = "my string".strikeThrough()
UILabel extension for strikethrough Enable/Disable.
extension UILabel {
func strikeThrough(_ isStrikeThrough:Bool) {
if isStrikeThrough {
if let lblText = self.text {
let attributeString = NSMutableAttributedString(string: lblText)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0,attributeString.length))
self.attributedText = attributeString
}
} else {
if let attributedStringText = self.attributedText {
let txt = attributedStringText.string
self.attributedText = nil
self.text = txt
return
}
}
}
}
Use it like this :
yourLabel.strikeThrough(btn.isSelected) // true OR false
SWIFT CODE
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
then:
yourLabel.attributedText = attributeString
Thanks to Prince answer ;)
SWIFT 4
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text Goes Here")
attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, attributeString.length))
self.lbl_productPrice.attributedText = attributeString
Other method is to used String Extension
Extension
extension String{
func strikeThrough()->NSAttributedString{
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: self)
attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, attributeString.length))
return attributeString
}
}
Calling the function : Used it like so
testUILabel.attributedText = "Your Text Goes Here!".strikeThrough()
Credit to #Yahya - update Dec 2017
Credit to #kuzdu - update Aug 2018
Strike out UILabel text in Swift iOS. PLease try this it's working for me
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
You can change your "strikethroughStyle" like styleSingle, styleThick,styleDouble
You can do it in IOS 6 using NSMutableAttributedString.
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:#"$198"];
[attString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0,[attString length])];
yourLabel.attributedText = attString;
Swift 5
extension String {
/// Apply strike font on text
func strikeThrough() -> NSAttributedString {
let attributeString = NSMutableAttributedString(string: self)
attributeString.addAttribute(
NSAttributedString.Key.strikethroughStyle,
value: 1,
range: NSRange(location: 0, length: attributeString.length))
return attributeString
}
}
Example:
someLabel.attributedText = someText.strikeThrough()
For anyone looking on how to do this in a tableview cell (Swift) you have to set the .attributeText like this:
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
}
If you want to remove the strikethrough do this otherwise it will stick around!:
cell.textLabel?.attributedText = nil
I might be late to the party.
Anyway, I am aware about the NSMutableAttributedString but recently I achieved the same functionality with slightly different approach.
I added the UIView with height = 1.
Matched the leading and trailing constraints of the UIView with the label's leading and trailing constraints
Aligned the UIView at centre of the Label
After following all the above steps my Label, UIView and its constraints were looking like below image.
Swift5 UILabel Extension. Removing strikethrough sometimes doesn't work. Use this code in that case.
extension UILabel {
func strikeThrough(_ isStrikeThrough: Bool = true) {
guard let text = self.text else {
return
}
if isStrikeThrough {
let attributeString = NSMutableAttributedString(string: text)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0,attributeString.length))
self.attributedText = attributeString
} else {
let attributeString = NSMutableAttributedString(string: text)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: [], range: NSMakeRange(0,attributeString.length))
self.attributedText = attributeString
}
}
}
Use below code
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;
Swift 4.2
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: product.price)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attributeString.length))
lblPrice.attributedText = attributeString
Swift 5 - short version
let attributedText = NSAttributedString(
string: "Label Text",
attributes: [.strikethroughStyle: NSUnderlineStyle.single.rawValue]
)
yourLabel.attributedText = attributedText
Change the text property to attributed and select the text and right click to get the font property. Click on the strikethrough.
On iOS 10.3 has an issue on rendering strikethrough line fixes by Adding a NSBaselineOffsetAttributeName, as explained here, to the attributed string brings back the strikethrough line. Overriding drawText:in: can be slow especially on Collection View or Table View Cells.
One sol - Add view to render a line.
Second sol -
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
attributeString.addAttribute(NSAttributedString.Key.baselineOffset, value: 2, range: NSMakeRange(0, attributeString.length))```
Swift 4 and 5
extension NSAttributedString {
/// Returns a new instance of NSAttributedString with same contents and attributes with strike through added.
/// - Parameter style: value for style you wish to assign to the text.
/// - Returns: a new instance of NSAttributedString with given strike through.
func withStrikeThrough(_ style: Int = 1) -> NSAttributedString {
let attributedString = NSMutableAttributedString(attributedString: self)
attributedString.addAttribute(.strikethroughStyle,
value: style,
range: NSRange(location: 0, length: string.count))
return NSAttributedString(attributedString: attributedString)
}
}
Example
let example = NSAttributedString(string: "440").withStrikeThrough(1)
myLabel.attributedText = example
Results
For those who face issue with multi line text strike
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
Create String extension and add below method
static func makeSlashText(_ text:String) -> NSAttributedString {
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: text)
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
return attributeString
}
then use for your label like this
yourLabel.attributedText = String.makeSlashText("Hello World!")
This is the one you can use in Swift 4 because NSStrikethroughStyleAttributeName has been changed to NSAttributedStringKey.strikethroughStyle
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
self.lbl.attributedText = attributeString
Related
Is it possible to tell the label that the text should be labeled?
I need to add all the text that comes to the label a strikethrough. In html this is done with <s> </s> tags. And how to do it in the swift? Photo
You can add this code: let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text") attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length)) self.label.attributedText = attributeString Output: Hope this helps. Edit according to your code As per your code you want to show that in the Tableview label So you can use it like, add this lines in cellForRowAt let strText = schedule[indexPath.row].discipline let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: strText) // Get here the text you want to show in your label attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length)) cell.dispos.attributedText = attributeString And it will work! Edit Not working in real Device Possibly you have not ticked the Target Membership of any of the files which you are using. Make sure it is ticked , if not then tick it and it will work perfectly.
If you want it as default for a label, create a subclass of the UILabel. Let's call the subclass MyLabel, so in MyLabel.swift class MyLabel: UILabel { override internal var text: String? { didSet { if let text = self.text { let attribString = NSMutableAttributedString(string: text) attribString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 2, range: NSRange(location: 0, length: attribString.length)) self.attributedText = attribString } } } } Then in your StoryBoard file or Xib File, assign the class name: This will make sure that all text in the label are strikethrough by default
private func getStrikeThroughTextFor(_ text:String) -> NSMutableAttributedString { let attributeString = NSMutableAttributedString(string: text) let attributes : [NSAttributedStringKey: Any] = [.baselineOffset:0, .strikethroughStyle:2] attributeString.addAttributes(attributes, range: NSMakeRange(0, attributeString.length)) return attributeString }
Multiline attributed string with strikethrough
I have a label with: label.numberOfLines = 0 And I'm trying to make text of this label strikethrough with: let index: NSMutableAttributedString = NSMutableAttributedString(string: label.text!) index.addAttributes([NSStrikethroughStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue, NSStrikethroughColorAttributeName: UIColor.red], range: NSMakeRange(0, index.length)) label.textColor = UIColor.red label.attributedText = index Is it true that attributed string is not working with multilines or with labels with numberOfLines set to 0? And if so, how to make multiline text strikethrough?
Works fine with multiline if you add NSBaselineOffsetAttributeName before: let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: (object?.title)!) attributeString.addAttribute(NSBaselineOffsetAttributeName, value: 0, range: NSMakeRange(0, attributeString.length)) attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
Your code should be like, let index: NSMutableAttributedString = NSMutableAttributedString(string: lbl.text!) index.addAttributes([NSStrikethroughStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue, NSStrikethroughColorAttributeName: UIColor.red], range: NSMakeRange(0, index.length)) lbl.textColor = UIColor.red lbl.attributedText = index because index is your mutable string! not title! And you can not use strike through with multi line label. If you want strike through effect in multiple lines then you can use UITextView instead of label!
Write it like, self.label.numberOfLines = 0 let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: self.label.text!) attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 1, range: NSMakeRange(0, attributeString.length)) self.label.attributedText = attributeString Working at my end.
I came up with two solutions. They are based on #SivajeeBattina answer. First one is to strike out text with help of http://adamvarga.com/strike/. private func returnStrikedOutTextFromString(_ str: String) -> String { var newString = "" let normal = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя " let strikethrough = "А̶Б̶В̶Г̶Д̶Е̶Ё̶Ж̶З̶И̶Й̶К̶Л̶М̶Н̶О̶П̶Р̶С̶Т̶У̶Ф̶Х̶Ц̶Ч̶Ш̶Щ̶Ъ̶Ы̶Ь̶Э̶Ю̶Я̶а̶б̶в̶г̶д̶е̶ё̶ж̶з̶и̶й̶к̶л̶м̶н̶о̶п̶р̶с̶т̶у̶ф̶х̶ц̶ч̶ш̶щ̶ъ̶ы̶ь̶э̶ю̶я̶ ̶̶" for i in 0..<str.characters.count { let range: Range<String.Index> = normal.range(of: str .substring(to: str.index(str.startIndex, offsetBy: i + 1)) .substring(from: str.index(str.startIndex, offsetBy: i)))! let index: Int = normal.distance(from: normal.startIndex, to: range.lowerBound) newString = String(format: "%#%#", newString, NSLocalizedString(strikethrough .substring(to: strikethrough.index(strikethrough.startIndex, offsetBy: index + 1)) .substring(from: strikethrough.index(strikethrough.startIndex, offsetBy: index)), comment: "")) } return newString } And second one is: https://github.com/GuntisTreulands/UnderLineLabel
Swift: strikethroughStyle For Label (Middle Delete Line For Label)
How to add strikethroughStyle for label. A middle delete line to a number in UILabel. Check the attach image for reference.
Use attributeString Screenshot Code NSDictionary * attribtues = #{NSStrikethroughStyleAttributeName:#(NSUnderlineStyleSingle), NSStrikethroughColorAttributeName:[UIColor redColor]}; NSAttributedString * attr = [[NSAttributedString alloc] initWithString:#"label" attributes:attribtues]; self.label.attributedText = attr;
Updated function from #krunal answer for Swift4 extension String { func strikeThrough() -> NSAttributedString { let attributeString = NSMutableAttributedString(string: self) attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0,attributeString.length)) return attributeString } } and to call the function is the same yourLabel.attributedText = "yourString".strikeThrough()
For Programatically Use Below Code Prefer to Use Extension: extension String { func strikeThrough() -> NSAttributedString { let attributeString = NSMutableAttributedString(string: self) attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0,attributeString.length)) return attributeString } } And call this Function myLabel.attributedText = "my string".strikeThrough() And Result look like this.
If wanting to use a UILabel extension extension UILabel { func strikeThroughText() { let attributeString = NSMutableAttributedString(string: self.text ?? "") attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0,attributeString.length)) self.attributedText = attributeString } }
add middle delete line for UILabel in storyboard tap uilabel in storyboard, look at utilities. Select Attributed tap font icon, open font panel. Select the text you want to change Select middle line (ex: Single) completed.
extension String { func strikeThrough() -> NSAttributedString { let attributeString = NSMutableAttributedString(string: self) attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0,attributeString.length)) return attributeString } } Then in 'viewDidLoad' View Controller yourLabel.attributedText = "yourString".strikeThrough()
Swift change color of text using NSMutableAttributedStrings
I have a UITableView and i would like to display the text of each row using different colors within the same line. I've tried this code, trying to translate from Obj-C but i cannot have it working let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject var attrString: NSMutableAttributedString = NSMutableAttributedString(string: object.valueForKey("example1")!.description) attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, attrString.length)) var stringToCell:String = String(format: "%# %#", attrString, object.valueForKey("example2")!.description) cell.textLabel?.text = stringToCell The output of all this is where the number 34 correspond to object.valueForKey("example1")!.description, so the problem is that the number is not red, and the second part (object.valueForKey("example2")!.description) is replaced by {. If I leave this piece of code regarding NSAttributedString the row text is displayed correctly.
I think the problem might lie in assigning to cell.textLabel?.text instead of cell.textLabel?.attributedText. Perhaps something like this: let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject var attrString: NSMutableAttributedString = NSMutableAttributedString(string: object.valueForKey("example1")!.description) attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, attrString.length)) var descString: NSMutableAttributedString = NSMutableAttributedString(string: String(format: " %#", object.valueForKey("example2")!.description)) descString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: NSMakeRange(0, descString.length)) attrString.appendAttributedString(descString); cell.textLabel?.attributedText = attrString Wasn't sure if you wanted the second part of the string to be red or another color so I made it black.
If you can, avoid using NSMutableAttributedString and just pass in the attributes with the constructor: private func PullToRefreshAttributedStringWithColor(color:UIColor) -> NSAttributedString { return NSAttributedString(string: "Pull to Refresh", attributes: [ NSForegroundColorAttributeName:color ]) }
Here is a example of attribute text Swift 3 let mainText = "Here is a example of attributedString" let attributeText = "attributedString" let range = (mainText as NSString).range(of: attributeText) let attributedString = NSMutableAttributedString(string:mainText) attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: range) attribute.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14) , range: range) lblTitle.attributedText = attributedString
How can I create a UILabel with strikethrough text?
I want to create a UILabel in which the text is like this How can I do this? When the text is small, the line should also be small.
SWIFT 5 UPDATE CODE let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text") attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSRange(location: 0, length: attributeString.length)) then: yourLabel.attributedText = attributeString To make some part of string to strike then provide range let somePartStringRange = (yourStringHere as NSString).range(of: "Text") attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: somePartStringRange) Objective-C In iOS 6.0 > UILabel supports NSAttributedString NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:#"Your String here"]; [attributeString addAttribute:NSStrikethroughStyleAttributeName value:#2 range:NSMakeRange(0, [attributeString length])]; Swift let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your String here") attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length)) Definition : - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)aRange Parameters List: name : A string specifying the attribute name. Attribute keys can be supplied by another framework or can be custom ones you define. For information about where to find the system-supplied attribute keys, see the overview section in NSAttributedString Class Reference. value : The attribute value associated with name. aRange : The range of characters to which the specified attribute/value pair applies. Then yourLabel.attributedText = attributeString; For lesser than iOS 6.0 versions you need 3-rd party component to do this. One of them is TTTAttributedLabel, another is OHAttributedLabel.
In Swift, using the single strikethrough line style: let attributedText = NSAttributedString( string: "Label Text", attributes: [.strikethroughStyle: NSUnderlineStyle.single.rawValue] ) label.attributedText = attributedText Additional strikethrough styles (Remember to use the .rawValue): .none .single .thick .double Strikethrough patterns (to be OR-ed with the style): .patternDot .patternDash .patternDashDot .patternDashDotDot Specify that the strikethrough should only be applied across words (not spaces): .byWord
I prefer NSAttributedString rather than NSMutableAttributedString for this simple case: NSAttributedString * title = [[NSAttributedString alloc] initWithString:#"$198" attributes:#{NSStrikethroughStyleAttributeName:#(NSUnderlineStyleSingle)}]; [label setAttributedText:title]; Constants for specifying both the NSUnderlineStyleAttributeName and NSStrikethroughStyleAttributeName attributes of an attributed string: typedef enum : NSInteger { NSUnderlineStyleNone = 0x00, NSUnderlineStyleSingle = 0x01, NSUnderlineStyleThick = 0x02, NSUnderlineStyleDouble = 0x09, NSUnderlinePatternSolid = 0x0000, NSUnderlinePatternDot = 0x0100, NSUnderlinePatternDash = 0x0200, NSUnderlinePatternDashDot = 0x0300, NSUnderlinePatternDashDotDot = 0x0400, NSUnderlineByWord = 0x8000 } NSUnderlineStyle;
Strikethrough in Swift 5.0 let attributeString = NSMutableAttributedString(string: "Your Text") attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attributeString.length)) self.yourLabel.attributedText = attributeString It worked for me like a charm. Use it as extension extension String { func strikeThrough() -> NSAttributedString { let attributeString = NSMutableAttributedString(string: self) attributeString.addAttribute( NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range:NSMakeRange(0,attributeString.length)) return attributeString } } Call like this myLabel.attributedText = "my string".strikeThrough() UILabel extension for strikethrough Enable/Disable. extension UILabel { func strikeThrough(_ isStrikeThrough:Bool) { if isStrikeThrough { if let lblText = self.text { let attributeString = NSMutableAttributedString(string: lblText) attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0,attributeString.length)) self.attributedText = attributeString } } else { if let attributedStringText = self.attributedText { let txt = attributedStringText.string self.attributedText = nil self.text = txt return } } } } Use it like this : yourLabel.strikeThrough(btn.isSelected) // true OR false
SWIFT CODE let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text") attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length)) then: yourLabel.attributedText = attributeString Thanks to Prince answer ;)
SWIFT 4 let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text Goes Here") attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, attributeString.length)) self.lbl_productPrice.attributedText = attributeString Other method is to used String Extension Extension extension String{ func strikeThrough()->NSAttributedString{ let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: self) attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, attributeString.length)) return attributeString } } Calling the function : Used it like so testUILabel.attributedText = "Your Text Goes Here!".strikeThrough() Credit to #Yahya - update Dec 2017 Credit to #kuzdu - update Aug 2018
Strike out UILabel text in Swift iOS. PLease try this it's working for me 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 You can change your "strikethroughStyle" like styleSingle, styleThick,styleDouble
You can do it in IOS 6 using NSMutableAttributedString. NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:#"$198"]; [attString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0,[attString length])]; yourLabel.attributedText = attString;
Swift 5 extension String { /// Apply strike font on text func strikeThrough() -> NSAttributedString { let attributeString = NSMutableAttributedString(string: self) attributeString.addAttribute( NSAttributedString.Key.strikethroughStyle, value: 1, range: NSRange(location: 0, length: attributeString.length)) return attributeString } } Example: someLabel.attributedText = someText.strikeThrough()
For anyone looking on how to do this in a tableview cell (Swift) you have to set the .attributeText like this: 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 } If you want to remove the strikethrough do this otherwise it will stick around!: cell.textLabel?.attributedText = nil
I might be late to the party. Anyway, I am aware about the NSMutableAttributedString but recently I achieved the same functionality with slightly different approach. I added the UIView with height = 1. Matched the leading and trailing constraints of the UIView with the label's leading and trailing constraints Aligned the UIView at centre of the Label After following all the above steps my Label, UIView and its constraints were looking like below image.
Swift5 UILabel Extension. Removing strikethrough sometimes doesn't work. Use this code in that case. extension UILabel { func strikeThrough(_ isStrikeThrough: Bool = true) { guard let text = self.text else { return } if isStrikeThrough { let attributeString = NSMutableAttributedString(string: text) attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0,attributeString.length)) self.attributedText = attributeString } else { let attributeString = NSMutableAttributedString(string: text) attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: [], range: NSMakeRange(0,attributeString.length)) self.attributedText = attributeString } } }
Use below code 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;
Swift 4.2 let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: product.price) attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attributeString.length)) lblPrice.attributedText = attributeString
Swift 5 - short version let attributedText = NSAttributedString( string: "Label Text", attributes: [.strikethroughStyle: NSUnderlineStyle.single.rawValue] ) yourLabel.attributedText = attributedText
Change the text property to attributed and select the text and right click to get the font property. Click on the strikethrough.
On iOS 10.3 has an issue on rendering strikethrough line fixes by Adding a NSBaselineOffsetAttributeName, as explained here, to the attributed string brings back the strikethrough line. Overriding drawText:in: can be slow especially on Collection View or Table View Cells. One sol - Add view to render a line. Second sol - attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length)) attributeString.addAttribute(NSAttributedString.Key.baselineOffset, value: 2, range: NSMakeRange(0, attributeString.length))```
Swift 4 and 5 extension NSAttributedString { /// Returns a new instance of NSAttributedString with same contents and attributes with strike through added. /// - Parameter style: value for style you wish to assign to the text. /// - Returns: a new instance of NSAttributedString with given strike through. func withStrikeThrough(_ style: Int = 1) -> NSAttributedString { let attributedString = NSMutableAttributedString(attributedString: self) attributedString.addAttribute(.strikethroughStyle, value: style, range: NSRange(location: 0, length: string.count)) return NSAttributedString(attributedString: attributedString) } } Example let example = NSAttributedString(string: "440").withStrikeThrough(1) myLabel.attributedText = example Results
For those who face issue with multi line text strike 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
Create String extension and add below method static func makeSlashText(_ text:String) -> NSAttributedString { let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: text) attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length)) return attributeString } then use for your label like this yourLabel.attributedText = String.makeSlashText("Hello World!")
This is the one you can use in Swift 4 because NSStrikethroughStyleAttributeName has been changed to NSAttributedStringKey.strikethroughStyle let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text") attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length)) self.lbl.attributedText = attributeString