2

根据当前语言,我已强制我的应用程序 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. 阿拉伯语解决方案正在运行,但按钮图像几乎没有拉伸。

4

0 回答 0