Tec*_*ain 0 string range ios swift
我有一个 UILabel 我想将它的某些部分作为下划线。我正在使用下面的代码。但它强调整个标签而不是标签的某些部分。请告诉我如何使标签的某些部分成为下划线。
let attributedString = NSMutableAttributedString(string: "UserAgreement".localized)
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSRange(location: 0, length: 10))
labelTc.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)
我不想处理故事板
尝试这个
雨燕4
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
Run Code Online (Sandbox Code Playgroud)
斯威夫特 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: [NSFontAttributeName:UIFont.systemFont(ofSize: 20),NSForegroundColorAttributeName: UIColor.white])
underLineTxt.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: rangeToUnderLine)
labelTc.attributedText = underLineTxt
Run Code Online (Sandbox Code Playgroud)