[英]Aligning image on right side of title on UIButton with imageEdgeInsets and titleEdgeInsets
I would like to add an image on the right side of the title something like this我想在标题的右侧添加像这样的图像
|Button [image]|
|title |
And I am able to achieve this by this code我可以通过这段代码实现这一点
button.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0)
button.imageEdgeInsets = UIEdgeInsetsMake(0, 135, 0, 5)
But when the title is large, the title label and image intersect with each other.但是当标题很大时,标题标签和图像相互交叉。 How can I break the title label?
我怎样才能打破标题标签?
try this, it will never intersect title and image with each other.试试这个,它永远不会与标题和图像相交。 and i created a button in storyboard in centre.
我在中间的故事板中创建了一个按钮。
@IBOutlet weak var myButton: UIButton!
let somespace: CGFloat = 10
self.myButton.setImage(UIImage(named: "cross"), forState: UIControlState.Normal)
self.myButton.imageEdgeInsets = UIEdgeInsetsMake(0, self.myButton.frame.size.width - somespace , 0, 0)
print(self.myButton.imageView?.frame)
self.myButton.titleEdgeInsets = UIEdgeInsetsMake(0, (self.myButton.imageView?.frame.width)! + somespace, 0, 10 )
and if you want multiline title for button change line break mode for button's title并且如果您想要按钮的多行标题更改按钮标题的换行符模式
self.myButton.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping
Swift 3.0斯威夫特 3.0
In swift 3.0 you can easily do it in interface builder.在 swift 3.0 中,您可以在界面构建器中轻松完成。
Select the UIButton -> Attributes Inspector -> go to size inspector and modify the image or title insets.选择 UIButton -> Attributes Inspector -> go to size inspector 并修改图像或标题插入。 and if you want image on button's right side simply select the force
Right-to-left
in semantic view utilities如果您想要按钮右侧的图像,只需在语义视图实用程序中选择强制
Right-to-left
This extension works for me:这个扩展对我有用:
Extension:扩展:
extension UIButton {
func imageToRight() {
transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
titleLabel?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
imageView?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
}
}
Usage:用法:
button.imageToRight()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.