简体   繁体   中英

Text padding on UILabel not working

This post is to complete one I made a while ago titled Text padding on UILabel , left with no answer.

The screenshot below shows it all. The first label, though the shortest is truncated. The second and the third even more have far too wide spaces on the right side. I want to solve those 2 problems.

在此处输入图片说明

I made a tiny project and provide it on GitHub to simply illustrate the issu and give other people the opportunity to easily take a close look.

Please update your NSLayoutConstraint like this

view.addConstraints([
                NSLayoutConstraint(item: label,
                                   attribute: .left,
                                   relatedBy: .lessThanOrEqual,
                                   toItem: view,
                                   attribute: .left,
                                   multiplier: 1.0,
                                   constant: sideMargin),
                NSLayoutConstraint(item: label,
                                   attribute: .right,
                                   relatedBy: .lessThanOrEqual,
                                   toItem: view,
                                   attribute: .right,
                                   multiplier: 1.0,
                                   constant: sideMargin * -2),
                NSLayoutConstraint(item: label,
                                   attribute: .top,
                                   relatedBy: .equal,
                                   toItem: topView,
                                   attribute: firstLoop ? .top : .bottom,
                                   multiplier: 1.0,
                                   constant: sideMargin),
                NSLayoutConstraint(item: label,
                                   attribute: .centerX,
                                   relatedBy: .equal,
                                   toItem: view,
                                   attribute: .centerX,
                                   multiplier: 1,
                                   constant: 0)
                ])

校正后

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM