我有一个UILabel我想把它的一部分作为下划线。我正在使用下面的代码。但它强调整个标签而不是标签的某些部分。请告诉我如何将标签的某些部分作为下划线。
let attributedString = NSMutableAttributedString(string: "UserAgreement".localized)
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSRange(location: 0, length: 10))
labelTc.attributedText = attributedString
我不想做故事板
答案 0 :(得分:4)
试试这个
Swift 4
RowID = 1
Swift 3.2
let wholeStr = "Don't have an account? Register"
//let rangeToUnderLine = (wholeStr as NSString).range(of: "Register")
let rangeToUnderLine = NSRange(location: 0, length: 10))
let underLineTxt = NSMutableAttributedString(string: wholeStr, attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 20),NSAttributedStringKey.foregroundColor: UIColor.white.withAlphaComponent(0.8)])
underLineTxt.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: rangeToUnderLine)
labelTc.attributedText = underLineTxt