当UI从右到左时,如何一起设置按钮标题和图像?

时间:2017-10-12 11:45:59

标签: ios swift uibutton right-to-left left-to-right

我已根据当前语言强制我的应用UI向右到左,反之亦然。

当从左到右激活时,这是在UIButton中显示图像下方标题的代码(默认)。因此,当我将其从右向左更改时,它不会按原样运行。这里的任何人都可以帮我修改现有的吗?

extension UIButton {
    func alignImageAndTitleVertically(padding: CGFloat = 6.0) {
        let imageSize: CGSize = imageView!.image!.size
        titleEdgeInsets = UIEdgeInsetsMake(0.0, -imageSize.width, -(imageSize.height + padding), 0.0)
        let labelString = NSString(string: titleLabel!.text!)
        let titleSize = labelString.size(attributes: [NSFontAttributeName: titleLabel!.font])
        self.imageEdgeInsets = UIEdgeInsetsMake(-(titleSize.height + padding), 0.0, 0.0, -titleSize.width)
        let edgeOffset = abs(titleSize.height - imageSize.height) / 2.0;
        self.contentEdgeInsets = UIEdgeInsetsMake(edgeOffset, 0.0, edgeOffset, 0.0)
    }
}

这是我在语言为阿拉伯语时使用的那个:

func alignImageAndTitleVerticallyWhenArabic(padding: CGFloat = 6.0) {
    let imageSize: CGSize = imageView!.image!.size
    titleEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -(imageSize.height + padding), -imageSize.width + padding)
    let labelString = NSString(string: titleLabel!.text!)
    let titleSize = labelString.size(attributes: [NSFontAttributeName: titleLabel!.font])
    self.imageEdgeInsets = UIEdgeInsetsMake(-titleSize.width, 0.0, 0.0, padding)
    let edgeOffset = abs(titleSize.height - imageSize.height) / 2.0;
    self.contentEdgeInsets = UIEdgeInsetsMake(edgeOffset, 0.0, edgeOffset, 0.0)
}

备注

  1. 当我使用alignImageAndTitleVerticallyWhenArabic时,它位于按钮下方,但对齐仍然不正确。

  2. 阿拉伯语解决方案正在运行,但按钮图像有点拉伸。

0 个答案:

没有答案