UIButton titleLabel frame size returning CGSize with zero width & height

前端 未结 6 441
执笔经年
执笔经年 2021-02-01 23:16

For iOS7, I used the UIButton titleLabel.frame.size.width property to determine the width of my button title under different localisations so I could p

6条回答
  •  醉梦人生
    2021-02-01 23:39

    So we can try this code :

    func withTitleAndImageAlign(title:String,image:UIImage,RL_Space:CGFloat) {
            setNeedsLayout()
            layoutIfNeeded()
            let buttonWidth = frame.size.width
            let textWidth = titleLabel?.frame.size.width
            let imageWidth = imageView?.frame.size.width
            let lim = buttonWidth  - (textWidth! + imageWidth! + (2 * RL_Space) )
            imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: lim)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: lim, bottom: 0, right: 0)
        }
    

提交回复
热议问题