Reputation: 51
I have a UIButton
with image and text. How do I align the image to the right, and align the text to the left?
Upvotes: 1
Views: 1650
Reputation: 1405
@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;
}
}
}
}
Upvotes: 0
Reputation: 5060
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.
Upvotes: 0