I have UIButton. In interface builder I set its title to be \'Attributed\'. How can I make its title to be underlined from code in Swift?
@IBOutlet weak var myBt
Here you can add an underline and bold face too. You can just add an extention in your swift class file
Here is the extention (Swift 4 updated)
extension NSMutableAttributedString {
@discardableResult func bold(_ text:String) -> NSMutableAttributedString {
let attrs : [NSAttributedStringKey : Any] = [
NSAttributedStringKey.font : UIFont(name: "Montserrat-Bold", size: 12)!,
NSAttributedStringKey.foregroundColor : UIColor.white,
NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue]
let boldString = NSMutableAttributedString(string: text, attributes: attrs)
self.append(boldString)
return self
}
@discardableResult func normal(_ text:String)->NSMutableAttributedString {
let attrs : [NSAttributedStringKey : Any] = [
NSAttributedStringKey.font : UIFont(name: "Montserrat-Regular", size: 12)!,
NSAttributedStringKey.foregroundColor : UIColor.white
]
let normal = NSAttributedString(string: text, attributes:attrs)
self.append(normal)
return self
}
}
You can use it like this:
let FormattedText = NSMutableAttributedString()
FormattedText
.normal("By signing in, you agree with our ")
.bold("Terms of Service")
yourLabel.attributedText = FormattedText
and the Result will be display like this
May not be the best approach but I made an example to use it with a separated class and make only a one line
call to get the text.
Here is my class:
import Foundation
import UIKit
enum AttributedTextsType {
case underlined
case bold
case boldUnderlined
}
class AttributedTexts {
private static func underlinedText(color: UIColor, size: CGFloat) -> [NSAttributedString.Key : Any] {
let attrs = [
NSAttributedString.Key.font : UIFont.systemFont(ofSize: size),
NSAttributedString.Key.foregroundColor : color,
NSAttributedString.Key.underlineStyle : 1] as [NSAttributedString.Key : Any]
return attrs
}
private static func getAttibute(type: AttributedTextsType, color: UIColor, size: CGFloat) -> [NSAttributedString.Key : Any] {
var attributes: [NSAttributedString.Key : Any]!
switch type {
case .underlined:
attributes = AttributedTexts.underlinedText(color: color, size: size)
break
case .bold:
attributes = AttributedTexts.underlinedText(color: color, size: size)
break
case .boldUnderlined:
attributes = AttributedTexts.underlinedText(color: color, size: size)
break
}
return attributes
}
static func set(string: String, color: UIColor, type: AttributedTextsType, size: CGFloat = 19.0) -> NSMutableAttributedString {
let attributes = getAttibute(type: type, color: color, size: size)
let attributedString = NSMutableAttributedString(string:"")
let buttonTitleStr = NSMutableAttributedString(string: string, attributes: attributes)
attributedString.append(buttonTitleStr)
return attributedString
}
}
Usage let attributedString = AttributedTexts.set(string: "Skip", color: .white, type: .underlined, size: 19.0)
Best regards