如何使用自动调整大小,圆角和阴影创建UILabel

时间:2019-05-21 22:57:00

标签: uilabel rounded-corners autosize dropshadow

尝试实现满足以下三个要求的UILabel:

  • 根据其文本内容自动调整大小
  • 有圆角
  • 有个影子

我可以实现三个要求中的两个-但是不幸的是,三个条件都不能满足!

我尝试了lbl4.layer.masksToBounds = true,并完成了自动调整大小(但是没有阴影)的四角,请参见下面的图像1 ...

我尝试了lbl4.layer.masksToBounds = false,并且在完成自动调整大小后获得了阴影(但没有圆角)-请参见下面的图片2 ...

我需要做什么才能获得所有三个要求???

这是我的代码:

let label_4: PaddingLabel = {

    let lbl4 = PaddingLabel(padding: UIEdgeInsets(top: 16.0, left: 16.0, bottom: 16.0, right: 16.0))
    lbl4.translatesAutoresizingMaskIntoConstraints = false
    lbl4.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."


    lbl4.numberOfLines = 0
    lbl4.font = UIFont.systemFont(ofSize: 24.0)
    lbl4.textColor = .white
    lbl4.textAlignment = .center
    lbl4.textAlignment = .justified
    lbl4.backgroundColor = .blue
    lbl4.layer.cornerRadius = 15.0
    lbl4.clipsToBounds = false
    lbl4.layer.masksToBounds = true   // !!!!!!! OR false

    lbl4.layer.shadowColor = UIColor.black.cgColor
    lbl4.layer.shadowRadius = 15
    lbl4.layer.shadowOpacity = 0.9
    lbl4.layer.shadowOffset = .init(width: 7.0, height: 10.0)
    lbl4.layer.shouldRasterize = true
    return lbl4
}()

lbl4.layer.masksToBounds = true

enter image description here

lbl4.layer.masksToBounds = false

enter image description here

这里是label_4的其余锚点(出于完整性原因...):

label_4.topAnchor.constraint(equalTo: label_3.bottomAnchor, constant: 50).isActive = true
label_4.leftAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.leftAnchor, constant: 20).isActive = true
label_4.rightAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.rightAnchor, constant: -20).isActive = true

这里是PaddingLabel类:

class PaddingLabel: UILabel {

    var padding: UIEdgeInsets?

    init(padding: UIEdgeInsets) {
        self.padding = padding
        super.init(frame: .zero)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func drawText(in rect: CGRect) {
        if let insets = padding {
            super.drawText(in: rect.inset(by: insets))
        }
    }

    override open var intrinsicContentSize: CGSize {
        guard let text = self.text else { return super.intrinsicContentSize }

        var contentSize = super.intrinsicContentSize
        var textWidth: CGFloat = frame.size.width
        var insetsHeight: CGFloat = 0.0
        var insetsWidth: CGFloat = 0.0

        if let insets = padding {
            insetsWidth += insets.left + insets.right
            insetsHeight += insets.top + insets.bottom
            textWidth -= insetsWidth
        }

        let newSize = text.boundingRect(with: CGSize(width: textWidth, height: CGFloat.greatestFiniteMagnitude),
                                        options: NSStringDrawingOptions.usesLineFragmentOrigin,
                                        attributes: [NSAttributedString.Key.font: (self.font ?? UIFont.systemFont(ofSize: self.font.pointSize))], context: nil)

        contentSize.height = ceil(newSize.size.height) + insetsHeight
        contentSize.width = ceil(newSize.size.width) + insetsWidth

        return contentSize
    }
}

0 个答案:

没有答案