ios - 无法在 UILabel、Swift 中换行文本

标签 ios swift uilabel

我见过几个这样的问题,但没有太多帮助。

我正在 View Controller 中使用自定义类创建一个带有边缘插入的 UILabel,如下所示 var message = InsetLabel()。在 viewDidLoad 中,我正在自定义标签以包裹文本,但它似乎不起作用。我哪里出错了?

我没有得到全文,如下图所示:

enter image description here

class InsetLabel: UILabel {
    override func drawText(in rect: CGRect) {
        super.drawText(in: UIEdgeInsetsInsetRect(rect, UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)))
    }
}


class ViewController: UIViewController {
var messageHeading  =   UILabel()
var message         =   InsetLabel()
var messageText  =  "Your profile is currently going through the approval process, You will be notified as soon as it has been appproved."

override func viewDidLoad() {
        super.viewDidLoad()

        messageHeading.text             =   "APPROVAL STATUS"
        messageHeading.textColor        =   Colors.darkGrey
        messageHeading.textAlignment    =   .left

        message.text                =   messageText
        message.textColor           =   Colors.darkGrey
        message.textAlignment       =   .left
        message.lineBreakMode       =   .byWordWrapping
        message.numberOfLines       =   10
        message.backgroundColor     =   Colors.white
        message.sizeToFit()
        view.addSubview(messageHeading)
        view.addSubview(message)

        messageHeading.translatesAutoresizingMaskIntoConstraints = false
        message.translatesAutoresizingMaskIntoConstraints = false

        let views = ["messageHeading"   :   messageHeading,
                     "message"          :   message
        ]

        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[messageHeading]", options: [], metrics: nil, views: views))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[messageHeading]-10-|", options: [], metrics: nil, views: views))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[messageHeading]-10-[message]", options: [], metrics: nil, views: views))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[message]|", options: [], metrics: nil, views: views))
}
}

最佳答案

更改您的自定义标签类别

class InsetLabel: UILabel {
    override func drawText(in rect: CGRect) {
        super.drawText(in: rect)
       // super.drawText(in: UIEdgeInsetsInsetRect(rect, UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)))
    }
}

然后添加如下约束:

  let msgHeadVerticalConst =  NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[messageHeading(>=20)]", options: [], metrics: nil, views: views)
let msgHeadHorizontalConst = NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[messageHeading]-10-|", options: [], metrics: nil, views: views)

let msgVerticalConst =          NSLayoutConstraint.constraints(withVisualFormat: "V:[messageHeading]-10-[message(>=20)]", options: [], metrics: nil, views: views)

let msgHorizontalConst = NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[message]-10-|", options: [], metrics: nil, views: views)

NSLayoutConstraint.activate(msgHeadVerticalConst + msgHeadHorizontalConst + msgVerticalConst + msgHorizontalConst)

关于ios - 无法在 UILabel、Swift 中换行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45557681/

相关文章:

ios - 保护随 iPhone 应用程序分发的数据

ios - 无法在 Swift 4 中将 UILabel 添加到 UIView

ios - 是否可以同时为同一应用程序的不同设备(iphone4s、iphone6、pad2 等)启动 iOS 模拟器

ios - XCode6/swift : unrecognized selector sent to instance

ios - iphone中的UILabel文本对齐问题?

ios - 使用方法 : prepareForReuse 时遇到问题

iphone - 在模态转场之后移除最后一个 UIViewController

macos - 不调用委托(delegate)函数 - NSURLConnection

ios - Swift-将不同类型的 UI 元素放入数组中

objective-c - UILabel 中数值的对齐