How can I create a UILabel with strikethrough text?
Solution 1
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.
Solution 2
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
Solution 3
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;
Solution 4
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
Solution 5
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 ;)
Comments
-
Dev over 2 years
I want to create a
UILabel
in which the text is like thisHow can I do this? When the text is small, the line should also be small.
-
Dev over 11 yearsFor iOS 5.1.1 lesser version how can I use 3 party attributed Label to display attributed text: ?
-
Dev over 11 yearsCan You suggest a good Toutorial? The link which you provided is little bit difficult to understand.. :(
-
Dev over 11 yearsCan you explain what I should do for creating a third party attributed Label for ios
-
pronebird about 10 yearsWhat is @2? Magic number?
-
pronebird about 10 yearsI guess you forgot to commit that. You should use a proper value from NSUnderlineStyle instead of @2. I am little bit pedantic here.
-
Mikkel Selsøe about 9 yearsIt can be expressed shorter, without the need for mutability:
[[NSAttributedString alloc] initWithString:@"string" attributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle)}];
-
Mihai Fratu over 8 yearsUp voted for using the right constant instead of a number
-
Jonauz about 7 yearsFor me it doesn't work if I try to strikethrough only part of string, but not all. Is this intended? And is it possible to still make it strikethrough just one word inside long text?
-
Hassan Taleb about 7 yearsWhy NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue is not working in swift 4 ?
-
Rashid KC almost 7 yearsHow to get striked on the center of the label?
-
James Toomey almost 7 yearsSee this thread if you have issues with the strikethrough appearing.
-
kuzdu almost 6 yearsDoesn't work for me. Purnendu roy's answer work for me. The only difference is that you pass in
value
0
and Purnendu roy passvalue: NSUnderlineStyle.styleSingle.rawValue
-
Muhammad Asyraf almost 6 years@kuzdu funny thing that my answer was back in dec 2017 it works back then he just copied my code and add up NSUnderlineStyle.styleSingle.rawValue ^-^ But no problem i will update this answer just to make you happy
-
Naresh almost 5 yearsDifference between value: 1 and value: 2
-
Vladimir Pchelyakov almost 5 years@iOS value is the thickness of the line that strikethrough the text. The larger the value, the thicker the line that crosses out the text
-
Jeroen about 4 yearsDo you happen to know a solution to StrikeThrough not being removed? Similar to forums.developer.apple.com/thread/121366
-
Dania Delbani almost 4 yearssmart solution 👍
-
Leo Dabus over 3 years@VladimirPchelyakov No. The value corresponds to
NSUnderlineStyle
rawValue (NSNumber). 1 = single, 2 = thick, 9 = double, and there is many other styles between thick and double