Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 单击启用后未隐藏Swift UITextView占位符_Ios_Swift - Fatal编程技术网

Ios 单击启用后未隐藏Swift UITextView占位符

Ios 单击启用后未隐藏Swift UITextView占位符,ios,swift,Ios,Swift,在我的场景中,我试图使用下面的代码创建带有自定义占位符的UITextView,但每当我点击UITextView时,我的占位符就不会隐藏。如何解决这个问题 下面是我的代码 在ViewDidLoad myTextview.delegate = self self.myTextview.text = "Enter Comments" self.myTextview.textColor = UIColor .lightGray 在下面添加了两个UITextView代理

在我的场景中,我试图使用下面的代码创建带有自定义占位符的
UITextView
,但每当我点击UITextView时,我的
占位符就不会隐藏。如何解决这个问题

下面是我的代码

ViewDidLoad

  myTextview.delegate = self

  self.myTextview.text = "Enter Comments"
            self.myTextview.textColor = UIColor .lightGray
在下面添加了两个
UITextView
代理

func textViewDidBeginEditing(_ textView: UITextView) {
        if myTextview.textColor == UIColor.lightGray {
            myTextview.text = nil
            myTextview.textColor = UIColor.black
        }
    }

    func textViewDidEndEditing(_ textView: UITextView) {
        if myTextview.text.isEmpty {
            myTextview.text = "Enter Comments"
            myTextview.textColor = UIColor.lightGray
        }
    }
将代码更改为

func textViewDidBeginEditing(_ textView: UITextView) {
    if myTextview.text == "Enter Comments" {
        myTextview.text = ""
        myTextview.textColor = UIColor.black
    }
}

func textViewDidEndEditing(_ textView: UITextView) {
    if myTextview.text == "" {
        myTextview.text = "Enter Comments"
        myTextview.textColor = UIColor.lightGray
    }
}

UITextView
占位符的代码下工作

实际问题及其解决方案

每当用户单击
UITextView
时,如果用户在UITextView中输入任何文本,占位符也应可见,则占位符应隐藏并允许显示用户键入的文本。但问题不是在用户启用键入时隐藏占位符

它应该像一个
TextField
占位符一样工作。下面的代码帮助我解决我的问题

import UIKit

class PlaceholderTextView: UITextView {

    @IBInspectable var placeholderColor: UIColor = UIColor.lightGray
    @IBInspectable var placeholderText: String = ""

    override var font: UIFont? {
        didSet {
            setNeedsDisplay()
        }
    }

    override var contentInset: UIEdgeInsets {
        didSet {
            setNeedsDisplay()
        }
    }

    override var textAlignment: NSTextAlignment {
        didSet {
            setNeedsDisplay()
        }
    }

    override var text: String? {
        didSet {
            setNeedsDisplay()
        }
    }

    override var attributedText: NSAttributedString? {
        didSet {
            setNeedsDisplay()
        }
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUp()
    }

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
    }

    private func setUp() {
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.textChanged(notification:)),
                                               name: Notification.Name("UITextViewTextDidChangeNotification"),
                                               object: nil)
    }

    @objc func textChanged(notification: NSNotification) {
        setNeedsDisplay()
    }

    func placeholderRectForBounds(bounds: CGRect) -> CGRect {
        var x = contentInset.left + 4.0
        var y = contentInset.top  + 9.0
        let w = frame.size.width - contentInset.left - contentInset.right - 16.0
        let h = frame.size.height - contentInset.top - contentInset.bottom - 16.0

        if let style = self.typingAttributes[NSAttributedString.Key.paragraphStyle] as? NSParagraphStyle {
            x += style.headIndent
            y += style.firstLineHeadIndent
        }
        return CGRect(x: x, y: y, width: w, height: h)
    }

    override func draw(_ rect: CGRect) {
        guard let text = self.text,
            let font = self.font else {
                return
        }

        let fontKey = NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue)
        let colorKey = NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue)
        let paragraphKey = NSAttributedString.Key(rawValue: NSAttributedString.Key.paragraphStyle.rawValue)
        if text.isEmpty && !placeholderText.isEmpty {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.alignment = textAlignment
            let attributes: [NSAttributedString.Key: Any] = [
                fontKey: font,
                colorKey: placeholderColor,
                paragraphKey: paragraphStyle]

            placeholderText.draw(in: placeholderRectForBounds(bounds: bounds), withAttributes: attributes)
        }
        super.draw(rect)
    }
}

你好,我有相同的问题,我得到了如下解决方案,我希望它的工作为你

下面是我的代码

代码

var placeholderLabel : UILabel!
将此代码放入您的viewDidLoad()

然后在
textViewDidChange
中写下下面的代码

func textViewDidChange(_ textView: UITextView) {
        placeholderLabel.isHidden = !yourTextView.text.isEmpty
}

请尝试使用此选项,并告诉我您的问题是否已解决

是否正在调用
textViewDidBeginEditing
?您是否设置了
委托
?@rmaddy是的,我甚至没有设置working@rmaddytextViewDidBeginEditing its not calling文本视图的委托未设置。
myTextView.delegate
self.myTextView.delegate
不是一回事。OP已明确表示,
textViewDidBeginEditing
未被调用。所以改变它的功能并不能解决任何问题。@rmaddy我们不能检查颜色的均匀性。他试图检查if条件中的颜色。但是该方法没有被调用,因此
if
语句在这一点上是不相关的。我试图解释他在委托方法中使用的代码逻辑。你没有抓住要点<未调用代码>文本视图didbeginediting
。其中的代码目前不相关。您的回答根本没有试图解决委托方法没有被调用的问题。您更新的回答没有解释问题中的代码有什么问题。@rmaddy在我的问题中,我提到在UITextView中键入文本后代码没有隐藏占位符。我不知道我还需要为你解释什么,伙计。:)您的答案应该解释为什么您的原始代码不起作用。
func textViewDidChange(_ textView: UITextView) {
        placeholderLabel.isHidden = !yourTextView.text.isEmpty
}