簡體   English   中英

UIButton將圖像左對齊並居中對齊

[英]UIButton align image left and center text

介紹:

我有一個類,它繼承UIButton 在此類中,我想更新屬性,例如titleEdgeInsetsimageEdgeInsetscontentHorizontalAlignment

我的第一種方法是使用layoutSubviews

override func layoutSubviews() {
    super.layoutSubviews()

    // update properties 
}

layoutSubviews創建一個無限循環 ,因此我已經尋找了一種替代方法。

我的問題:

使用willMove方法更新 UIButton屬性是一種常見方法嗎?

override func willMove(toWindow newWindow: UIWindow?) {
    super.willMove(toWindow: newWindow)

    // update properties
}

如果沒有,為什么?

我的目標是使按鈕的imageView左對齊(帶有填充),並使文本居中。

更新:

我需要按鈕frame.size和bounds.width來計算文本和圖像視圖的位置

你上面提到的所有屬性都可以在設置init中的UIButton存在是完全沒有必要將它們設置在layoutSubviewswillMove(toWindowlayoutSubviews就會被多次如此反復ň阿恩設置這些屬性在這里是沒有意義的。 willMove(toWindow將按鈕添加到某個視圖並加載按鈕時,將調用willMove(toWindow但是您不必等到那時就設置這些屬性。因為您已經有了button的子類,所以建議您這樣做

class SomeButton: UIButton {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        self.contentHorizontalAlignment = .center
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

順便提一下,不建議創建UIButton的子類,因此,如果您只想將這些屬性分配給按鈕,則可以擴展UIButton

extension UIButton {
    func applyStyle() {
        self.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        self.contentHorizontalAlignment = .center
    }
}

編輯:

這是你想要的嗎??

在此處輸入圖片說明

在此處輸入圖片說明

無論文本是什么,文本始終位於中間,圖像以10像素的填充量位於左側

編輯2:

正如OP確認的那樣,他希望按鈕的樣式如上圖所示,並發布代碼以實現相同的功能

class SomeButton: UIButton {
    var titleFont: UIFont! = nil
    var textSize: CGFloat = 0
    let imageWidth: CGFloat = 20
    let buttonHeight: CGFloat = 30

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.titleFont = titleLabel!.font
        self.setTitle("here", for: .normal)
        self.setTitleColor(UIColor.red, for: .normal)
        self.setImage(UIImage(named: "hand"), for: .normal)
        self.backgroundColor = UIColor.green
    }

    override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
        if let string = self.title(for: .normal) {
            textSize = string.widthOfString(usingFont: self.titleFont)
            //30 because imageWidth + 10 padding
            return CGRect(origin: CGPoint(x: 30, y: 0), size: CGSize(width: textSize + 30, height: buttonHeight))
        }
        return CGRect.zero
    }

    override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
        return CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: imageWidth, height: buttonHeight))
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override var intrinsicContentSize: CGSize {
        //60 because you need eauql padding on both side 30 + 30 = 60
        return CGSize(width: textSize + 60, height: buttonHeight)
    }
}

extension String {

    func widthOfString(usingFont font: UIFont) -> CGFloat {
        let fontAttributes = [NSAttributedString.Key.font: font]
        let size = self.size(withAttributes: fontAttributes)
        return size.width
    }
}

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM