How to Create Label Doted Underline in Swift [duplicate] - ios

This question already has an answer here:
How to use NSUnderlineStyle.PatternDot
(1 answer)
Closed 2 years ago.
I Want to doted underline on label in Swift

//you can add doted underline string like this in swift 5
let string = NSMutableAttributedString(string: "Hello string")
string.addAttributes([.underlineStyle : NSUnderlineStyle.single.union(.patternDot).rawValue],
range: NSRange(location: 0, length: string.length))
yourLableName.attributedText = string

You can use NSAttributedString for setting underline in swift 5.
let string = NSAttributedString(string: "Receiver", attributes: [.underlineStyle: NSUnderlineStyle.single.union(.patternDash).rawValue])
label.attributedText = string

Related

Why is underline style not working for a NSAttributedString? [duplicate]

This question already has an answer here:
How to use NSUnderlineStyle.PatternDot
(1 answer)
Closed 1 year ago.
I have the following code to try to underline a label's string.
let attributedString = NSMutableAttributedString(string: "Hello World!")
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single, range: NSRange.init(location: 0, length: attributedString.length))
label.attributedText = attributedString
However, when running the code the label shows Hello World! with no underline.
I'm running Xcode 12.4, and using an iOS 14.4 iPhone 12 Pro Max simulator.
It seems like this is a bug. Changing the value from NSUnderlineStyle.single to 1 fixes the issue.
The final code would look something like the following.
let attributedString = NSMutableAttributedString(string: "Hello World!")
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range: NSRange.init(location: 0, length: attributedString.length))
label.attributedText = attributedString

NSStrikethroughStyleAttributeName Not working if append with another string or attributed string [duplicate]

This question already has answers here:
NSMutableAttributedString not working in tableviewcell with a specific range
(1 answer)
iOS 10.3: NSStrikethroughStyleAttributeName is not rendered if applied to a sub range of NSMutableAttributedString
(7 answers)
Closed 5 years ago.
Well, it is very strange for me that, NSStrikethroughStyleAttributeName doesn't working if I concatenate/ append two NSMutableAttributedString for a label.
let partTwo: NSMutableAttributedString = NSMutableAttributedString(string: "4450")
partTwo.addAttribute(NSStrikethroughStyleAttributeName, value: 1, range: NSMakeRange(0, partTwo.length))
cell.lbl11.attributedText = partTwo
Above code working perfect, but if I do bellow code:
let partOne: NSMutableAttributedString = NSMutableAttributedString(string: "WAS $")
let partTwo: NSMutableAttributedString = NSMutableAttributedString(string: "4450")
partTwo.addAttribute(NSStrikethroughStyleAttributeName, value: 1, range: NSMakeRange(0, partTwo.length))
let concate = NSMutableAttributedString(attributedString: partOne)
concate.append(partTwo)
cell.lbl11.attributedText = concate
NSStrikethroughStyleAttributeName doesn't working.
I needs like below image in one single label. Is it possible?

How do I change the font attributes of certain sections of a UILabel text in Swift? [duplicate]

This question already has answers here:
Example of NSAttributedString with two different font sizes?
(4 answers)
ios swift: Is it possible to change the font style of a certain word in a string?
(3 answers)
Closed 7 years ago.
Example:
let dummy = UILabel()
dummy.text = "this is a String"
I want the view to show something like this. Also, is it possible to change the font size of "String" in dummy?
this is a String
You can use NSAttributedString to do that:
let dummy = UILabel()
let theString = "this is a String"
let attributedString = NSMutableAttributedString(string: theString, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(12.0)])
let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFontOfSize(24.0)]
attributedString.addAttributes(boldFontAttribute, range: NSMakeRange(10, theString.characters.count))
dummy.attributedText = attributedString

UILabel text with different font size and color [duplicate]

This question already has answers here:
How do I make an attributed string using Swift?
(31 answers)
Closed 7 years ago.
Howto set different font size and color in a UILabel with Swift?
I need to color the first char of the string with different color and size than the rest of the string.
Suppose you want to have a smaller and gray currency symbol like this:
Just use a NSMutableAttributedString object:
let amountText = NSMutableAttributedString.init(string: "€ 60,00")
// set the custom font and color for the 0,1 range in string
amountText.setAttributes([NSFontAttributeName: UIFont.systemFontOfSize(12),
NSForegroundColorAttributeName: UIColor.grayColor()],
range: NSMakeRange(0, 1))
// if you want, you can add more attributes for different ranges calling .setAttributes many times
// set the attributed string to the UILabel object
myUILabel.attributedText = amountText
Swift 5.3:
let amountText = NSMutableAttributedString.init(string: "€ 60,00")
// set the custom font and color for the 0,1 range in string
amountText.setAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),
NSAttributedString.Key.foregroundColor: UIColor.gray],
range: NSMakeRange(0, 1))
// if you want, you can add more attributes for different ranges calling .setAttributes many times
// set the attributed string to the UILabel object
// set the attributed string to the UILabel object
myUILabel.attributedText = amountText

How can I bold part of a string in Swift? [duplicate]

This question already has answers here:
Any way to bold part of a NSString?
(11 answers)
Closed 7 years ago.
If I create a label like this:
var label = UILabel(...)
label.text = first_name + ", " + age
self.view.addSubview(label)
How can I change the age of the label to bold, but keep the first_name normal weight?
You can use an attributed string. Here, the bold size is 15, but you can also change that point size if you want.
var age = "13"
var att = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15)]
var boldAge = NSMutableAttributedString(string:age, attributes:att)
// Assigning to a UILabel
yourLabel.attributedText = boldAge
so now you would want to assign your text like this:
label.attributedText = first_name + ", " + boldAge

Resources