使用情节提要在 UILabel 中的文本前后添加一个空格 [英] Adding a space before and after the text in UILabel using the storyboard

查看:74
本文介绍了使用情节提要在 UILabel 中的文本前后添加一个空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用故事板在 UILabel 中的文本前后添加空格.

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

这是一个带有背景的标签示例.

Here is an example of label with a background.

推荐答案

一种方法是:

  • 在故事板中使用自动布局.

  • Use auto layout in the storyboard.

使用覆盖 intrinsicContentSize 的 UILabel 子类使其比默认值稍宽.

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

例如:

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)    
    }
}

现在只需将故事板中每个标签的类设置为 MyWiderLabel.

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

这篇关于使用情节提要在 UILabel 中的文本前后添加一个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆