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
Related
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
I have a label and I added a attributed string to it. The string is,
let nameText = "My name is Shreesha and Im an iOS developer. My name is Shreesha and Im an iOS developer."`My name is Shreesha and Im an iOS developer. My name is Shreesha and Im an iOS developer.`
In this text I'm trying to add a * in the beginning of the text so I used an attributed string and the code looks like this,
func attributedTextForFeeApplies() -> NSAttributedString {
let attributedText = NSMutableAttributedString(string: "* " + nameText)
attributedText.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue, range: NSMakeRange(0, attributedText.length))
attributedText.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 10), range: NSMakeRange(0, attributedText.length))
let superScriptString = "* "
attributedText.addAttribute(NSAttributedStringKey.baselineOffset, value: 2, range: NSMakeRange(0, superScriptString.characters.count))
attributedText.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 9), range: NSMakeRange(0, superScriptString.characters.count))
attributedText.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue, range: NSMakeRange(0, superScriptString.characters.count))
let superscriptAttributedString = attributedText
let paragraph = NSMutableParagraphStyle()
paragraph.lineBreakMode = .byTruncatingTail
superscriptAttributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraph, range: NSMakeRange(0, superscriptAttributedString.length))
return superscriptAttributedString
}
I gave the constraints to the Label like this,
Even though I set number of lines to 0 and no height constraint the label is truncating like this,
But when I don't use this line of code attributedText.addAttribute(NSAttributedStringKey.baselineOffset, value: 2, range: NSMakeRange(0, superScriptString.characters.count)) it is working fine like this,
And also if add * in the middle (without removing attributedText.addAttribute(NSAttributedStringKey.baselineOffset, value: 2, range: NSMakeRange(0, superScriptString.characters.count))) it works fine but just doesn't work if I use it in the beginning of the text,
Screen shot:
And it also works if I increase the font size.
I think there is an issue with NSAttributedString and if not I want to know what is the issue. Could someone please help me.
Your problem is as soon as you assign attributed string you have to re calculate the height. but there is a quick fix which you don't need to calculate it your self. give the label a force line break at the end of your label in which case it has to re calculate the height automatically.
// see the \n at the end of your string that will cause the label to recalculate it's height.
let nameText = "My name is Shreesha and Im an iOS developer. My name is Shreesha and Im an iOS developer."`My name is Shreesha and Im an iOS developer. My name is Shreesha and Im an iOS developer. \n"
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?
Im trying to make a custom font using attributed string for custom UIButton in xcode using swift 2.0
However it works fine in interface builder but not working in any device or simulator, I'm using xcode 7.3 latest with ios 9.3 but unfortunately it seems a bug in xcode/ios. After searching it from google, I came to realize that its should be treat programatically with swift.
My goal is to just put custom font with NSRange such that I have attributed string of ABCDEF with new line after each alphabet. And I just need to setup the Custom font for first two elements via swift. Below is the example
A <--- font1
B <--- fontMiddle
CDEF <--- font1
P.S: I need exact font size and color which is set in interface builder and just want to set the custom readable font for swift
Here's my try:
let size = (tryButton?.titleLabel?.font?.pointSize)!
let labelFont = UIFont(name: "font1", size: size)
let middleFont = UIFont(name: "fontMiddle", size: size)
let attributedString = NSMutableAttributedString(string:(tryButton.titleLabel?.text)!)
attributedString.addAttribute(NSFontAttributeName, value: labelFont!, range: NSRange(location: 0, length: 1) )
attributedString.addAttribute(NSFontAttributeName, value: middleFont!, range: NSRange(location: 1, length: 2) )
attributedString.addAttribute(NSFontAttributeName, value: labelFont!, range: NSRange(location: 2, length: attributedString.lenght) )
tryButton.setAttributedTitle(attributedString, forState: .Normal)
I am trying to use NSMutableAttributedString with dotted underline below is my code but none of the Patterns seem to work am I missing something ?
var str : NSMutableAttributedString = NSMutableAttributedString(string: "HelloWorld")
str.addAttribute(NSUnderlineStyleAttributeName , value: NSNumber(integer:(NSUnderlineStyle.PatternDot).rawValue), range: NSMakeRange(0, str.length))
You have to do it like this:
Xcode 10 • Swift 4.2 or later
yourLabel.attributedText = NSAttributedString(string: "Hello World !!!", attributes: [.underlineStyle: NSUnderlineStyle.patternDot.union(.single).rawValue])
Note: for older Swift syntax check edit history