【发布时间】:2017-05-10 20:52:25
【问题描述】:
【问题讨论】:
-
看起来像网页
标签: ios swift user-interface uibutton uistoryboard
【问题讨论】:
标签: ios swift user-interface uibutton uistoryboard
使用属性字符串设置按钮标题。请参阅下面的代码以供参考。
func attributedText(){
// create attributed string
let attributedString = NSMutableAttributedString(string:"Don't have an account? ")
let myString = "Sign Up"
let myAttribute = [ NSForegroundColorAttributeName: UIColor(red: 194/255, green: 159/255, blue: 74/255, alpha: 1.0) ]
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
attributedString.append(myAttrString)
myCustomButton.setAttributedTitle(attributedString, for: .normal)
}
【讨论】:
这是一个标签和一个在蓝色文本上方没有名称的按钮,因此将一个标签拖到您的故事板并将其连接到您的 swift 文件(我将其命名为 labelText)和您想要的文本上方的一个按钮。
然后在你的 viewDidLoad 函数里面放这行:
// You can change the color as you want
let color: UIColor = UIColor(red: 2/255.0, green: 202/255.0, blue: 246/255.0, alpha: 1.0)
let string_to_color = "Get help signing in."
let titleStr: String = labelText.text!
let range: NSRange = (titleStr as NSString).rangeOfString(string_to_color)
let str: NSMutableAttributedString = NSMutableAttributedString(string: titleStr)
str.addAttribute(NSForegroundColorAttributeName, value: color, range: range)
//Below line is if you want a line under your colored text
//str.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: range)
labelText.attributedText = str
现在你准备好了。
编码愉快。
【讨论】: