Adding a space before and after the text in UILabel using the storyboard

岁酱吖の 提交于 2019-12-11 15:24:04

问题


How to add a space before and after the text in UILabel using storyboard.

Here is an example of label with a background.


回答1:


One approach is:

  • Use auto layout in the storyboard.

  • Use a UILabel subclass that overrides intrinsicContentSize to be a little wider than the default.

For example:

extension CGSize {
    func sizeByDelta(dw dw:CGFloat, dh:CGFloat) -> CGSize {
        return CGSizeMake(self.width + dw, self.height + dh)
    }
}

class MyWiderLabel : UILabel {
    override func intrinsicContentSize() -> CGSize {
        return super.intrinsicContentSize().sizeByDelta(dw: 20, dh: 0)    
    }
}

Now just set the class of every label in your storyboard to be a MyWiderLabel.



来源:https://stackoverflow.com/questions/34054210/adding-a-space-before-and-after-the-text-in-uilabel-using-the-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!