简体   繁体   English

文本视图占位符被切断

[英]Text View placeholder being cut off

I have a custom UITextView that shows a placeholder, like so:我有一个显示占位符的自定义UITextView ,如下所示:

extension UITextView {

    private class PlaceholderLabel: UILabel { }

    private var placeholderLabel: PlaceholderLabel {
        if let label = subviews.compactMap( { $0 as? PlaceholderLabel }).first {
            return label
        } else {
            let label = PlaceholderLabel(frame: .zero)
            label.font = font
            addSubview(label)
            return label
        }
    }

    @IBInspectable
    var placeholder: String {
        get {
            return subviews.compactMap( { $0 as? PlaceholderLabel }).first?.text ?? ""
        }
        set {
            let placeholderLabel = self.placeholderLabel
            placeholderLabel.text = newValue
            placeholderLabel.numberOfLines = 0
            let width = frame.width - textContainer.lineFragmentPadding * 2
            let size = placeholderLabel.sizeThatFits(CGSize(width: width, height: .greatestFiniteMagnitude))
            placeholderLabel.frame.size.height = size.height
            placeholderLabel.frame.size.width = width
            placeholderLabel.frame.origin = CGPoint(x: textContainer.lineFragmentPadding, y: textContainerInset.top)

            textStorage.delegate = self
        }
    }
    
    @IBInspectable
    var placeholderColor: UIColor? {
        get {
            self.placeholderColor
        }
        
        set {
            placeholderLabel.textColor = newValue
        }
    }

}

extension UITextView: NSTextStorageDelegate {

    public func textStorage(_ textStorage: NSTextStorage, didProcessEditing editedMask: NSTextStorage.EditActions, range editedRange: NSRange, changeInLength delta: Int) {
        if editedMask.contains(.editedCharacters) {
            placeholderLabel.isHidden = !text.isEmpty
        }
    }

}

I've put the UITextView inside a UIView which sits inside a UIStackView so that I can hide it and show it as needed.我已将UITextView放在位于UIView内的UIStackView中,以便我可以隐藏它并根据需要显示它。 It's hidden by default.默认情况下它是隐藏的。 However, when run the app, the text view looks like this:但是,当运行应用程序时,文本视图如下所示:

在此处输入图像描述

When it should look like this:什么时候应该是这样的:

在此处输入图像描述

I've noticed on the storyboard that the width of the UIView is 64 when it's hidden inside the UIStackView and 428 when it's not hidden.我在 storyboard 上注意到,当UIStackView UIView时,其宽度为 64,当它不隐藏时,其宽度为 428。 So I think what might be happening is that the placeholder is setup when the UIView is 64, but isn't updated when the UIView is shown in the UIStackView .所以我认为可能发生的情况是占位符在UIView为 64 时设置,但在UIView中显示UIStackView时未更新。

What should I do to the UITextView extension so that it shows the placeholder in its full uncut length?我应该对UITextView扩展做些什么,以便它以完整的未切割长度显示占位符?

I've tried adding the following below addSubview(label) , but it hasn't helped:我尝试在addSubview(label)下面添加以下内容,但没有帮助:

label.translatesAutoresizingMaskIntoConstraints = false
label.leadingAnchor.constraint(equalTo: superview!.leadingAnchor, constant: 0).isActive = true
label.trailingAnchor.constraint(equalTo: superview!.trailingAnchor, constant: 0).isActive = true
label.topAnchor.constraint(equalTo: superview!.topAnchor, constant: 0).isActive = true
label.bottomAnchor.constraint(equalTo: superview!.bottomAnchor, constant: 0).isActive = true

You have added addSubview(label) but not given it constraints.您添加了addSubview(label)但没有给它约束。 Could you try pinning its frame to the frame of the superview?您可以尝试将其框架固定到超级视图的框架吗? Don't forget to set translatesAutoresizingMaskIntoConstraints to false不要忘记将translatesAutoresizingMaskIntoConstraints设置为 false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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