[英]How do I put the image on the right side of the button and the text align left
I have a UIButton
with image and text. 我有一个带有图像和文本的
UIButton
。 How do I align the image to the right, and align the text to the left? 如何将图像向右对齐,并将文本向左对齐?
@IBDesignable class RightImageButton: UIButton {
@IBInspectable var alignImageToRight: Bool = true
override func layoutSubviews() {
super.layoutSubviews()
if alignImageToRight {
if let imageView = self.imageView, let titleLabel = self.titleLabel {
let imageWidth = imageView.frame.size.width
var imageFrame: CGRect = imageView.frame;
var labelFrame: CGRect = titleLabel.frame;
labelFrame.origin.x = self.titleEdgeInsets.left + self.contentEdgeInsets.left
imageFrame.origin.x = frame.width - self.imageEdgeInsets.right - self.contentEdgeInsets.right - imageWidth
imageView.frame = imageFrame;
titleLabel.frame = labelFrame;
}
}
}
}
Use below two methods 使用以下两种方法
button.imageEdgeInsets = UIEdgeInsets(top: 0.0, left: 0, bottom: 0.0, right: 0.0)
button.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: 0, bottom: 0.0, right: 0.0)
Pass value for left and right and adjust it. 传递左右的值并进行调整。 Set offset as per you requirement.
根据您的要求设置偏移量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.