1
我有UIButton
图片和文字。如何将图像对齐到右侧,并将文本对齐到左侧?如何将图片放在按钮的右侧并将文字左对齐
我有UIButton
图片和文字。如何将图像对齐到右侧,并将文本对齐到左侧?如何将图片放在按钮的右侧并将文字左对齐
@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;
}
}
}
}
使用以下两种方法
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)
通行证值左右,调整。 按照您的要求设置偏移量。
这里有几个很好的答案:http://stackoverflow.com/q/4564621/2274694 – 2014-11-24 00:41:39