[英]Is it possible to add two labels inside an UIButton on Swift?
I have an UIButton
on my Swift
project and I would like to use a font-awesome icon combined with a text inside of it. 我的
Swift
项目上有一个UIButton
,我想使用超棒的字体图标以及其中的文本。
I know that I can set a title to the button but, as far I need that the icon and the text will be in a different text-size, I cannot combine them in the same title tag. 我知道我可以为按钮设置标题,但是,由于我需要图标和文本使用不同的文本大小,因此我无法将它们组合在同一标题标签中。 I also have thought about to use an image (to set the font-awesome icon) with an attributed title but then I cannot change the color of the font-awesome icon.
我还考虑过使用带有属性标题的图像(设置超棒图标),但是后来我无法更改超棒图标的颜色。
I know that I can get this behaviour adding two labels inside an UIView
but I would like to know if it is possible to get this on an UIButton
. 我知道我可以在
UIView
添加两个标签来获得这种行为,但是我想知道是否有可能在UIButton
上获得它。
Thanks in advance! 提前致谢!
You can set attributed title of your button with attribute string something like, 您可以使用属性字符串设置按钮的属性标题,例如
let attributedTitle = NSAttributedString(string: "Click Here",
attributes: [NSForegroundColorAttributeName : UIColor.blueColor()])
button.setAttributedTitle(attributedTitle, forState: .Normal)
You can set multiple color,font,size for particular texts or part of string. 您可以为特定文本或字符串的一部分设置多种颜色,字体,大小。
another example, 另一个例子,
attributedTitle = NSMutableAttributedString(string: @"Click Here", attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
attributedTitle.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:4))
button.setAttributedTitle(attributedTitle, forState: .Normal)
So, you can specify location by using NSRange
and can set different color and font attributes for specified range! 因此,您可以使用
NSRange
指定位置,并可以为指定范围设置不同的颜色和字体属性!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.