diff --git a/Example/Example/EditorDemoController.swift b/Example/Example/EditorDemoController.swift index 963783bec..687f78f28 100644 --- a/Example/Example/EditorDemoController.swift +++ b/Example/Example/EditorDemoController.swift @@ -39,19 +39,16 @@ class EditorDemoController: UIViewController { return textView }() - fileprivate(set) lazy var titleTextField: UITextField = { - let placeholderText = NSLocalizedString("Enter title here", comment: "Label for the title of the post field. Should be the same as WP core.") - let textField = UITextField() + fileprivate(set) lazy var titleTextField: UITextView = { + let textField = UITextView() textField.accessibilityLabel = NSLocalizedString("Title", comment: "Post title") - textField.attributedPlaceholder = NSAttributedString(string: placeholderText, - attributes: [NSForegroundColorAttributeName: UIColor.lightGray]) textField.delegate = self textField.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline) textField.returnKeyType = .next textField.textColor = UIColor.darkText textField.translatesAutoresizingMaskIntoConstraints = false - + textField.isScrollEnabled = false let toolbar = self.createToolbar(htmlMode: false) toolbar.enabled = false textField.inputAccessoryView = toolbar @@ -59,6 +56,20 @@ class EditorDemoController: UIViewController { return textField }() + fileprivate(set) lazy var titlePlaceholderLabel: UILabel = { + let placeholderText = NSLocalizedString("Enter title here", comment: "Label for the title of the post field. Should be the same as WP core.") + let textField = UILabel() + + textField.attributedText = NSAttributedString(string: placeholderText, + attributes: [NSForegroundColorAttributeName: UIColor.lightGray, NSFontAttributeName: UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline)]) + textField.sizeToFit() + textField.translatesAutoresizingMaskIntoConstraints = false + + return textField + }() + + fileprivate var titleHeightConstraint: NSLayoutConstraint! + fileprivate(set) lazy var separatorView: UIView = { let separatorView = UIView(frame: CGRect(x: 0, y: 0, width: 44, height: 1)) @@ -108,6 +119,7 @@ class EditorDemoController: UIViewController { view.backgroundColor = UIColor.white view.addSubview(titleTextField) + view.addSubview(titlePlaceholderLabel) view.addSubview(separatorView) view.addSubview(richTextView) view.addSubview(htmlTextView) @@ -158,22 +170,42 @@ class EditorDemoController: UIViewController { } + // MARK: - Title and Title placeholder position methods + func updateTitleHeight() { + let sizeThatShouldFitTheContent = titleTextField.sizeThatFits(CGSize(width:view.frame.width - ( 2 * Constants.margin), height: CGFloat.greatestFiniteMagnitude)) + let insets = titleTextField.textContainerInset + titleHeightConstraint.constant = max(sizeThatShouldFitTheContent.height, titleTextField.font!.lineHeight + insets.top + insets.bottom) + } + + func updateTitlePlaceholderVisibility() { + self.titlePlaceholderLabel.isHidden = !titleTextField.text.isEmpty + } // MARK: - Configuration Methods private func configureConstraints() { + titleHeightConstraint = titleTextField.heightAnchor.constraint(equalToConstant: titleTextField.font!.lineHeight) + updateTitleHeight() NSLayoutConstraint.activate([ titleTextField.leftAnchor.constraint(equalTo: view.leftAnchor, constant: Constants.margin), titleTextField.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -Constants.margin), titleTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: Constants.margin), - titleTextField.heightAnchor.constraint(equalToConstant: titleTextField.font!.lineHeight) + titleHeightConstraint + ]) + + let insets = titleTextField.textContainerInset + NSLayoutConstraint.activate([ + titlePlaceholderLabel.leftAnchor.constraint(equalTo: titleTextField.leftAnchor, constant: insets.left + titleTextField.textContainer.lineFragmentPadding), + titlePlaceholderLabel.rightAnchor.constraint(equalTo: titleTextField.rightAnchor, constant: -insets.right), + titlePlaceholderLabel.topAnchor.constraint(equalTo: titleTextField.topAnchor, constant: insets.top), + titlePlaceholderLabel.heightAnchor.constraint(equalToConstant: titleTextField.font!.lineHeight) ]) NSLayoutConstraint.activate([ separatorView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: Constants.margin), separatorView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -Constants.margin), - separatorView.topAnchor.constraint(equalTo: titleTextField.bottomAnchor, constant: Constants.margin), + separatorView.topAnchor.constraint(equalTo: titleTextField.bottomAnchor, constant: 0), separatorView.heightAnchor.constraint(equalToConstant: separatorView.frame.height) ]) @@ -317,7 +349,11 @@ extension EditorDemoController : UITextViewDelegate { changeRichTextInputView(to: nil) } - func textViewDidChange(_ textView: UITextView) { + func textViewDidChange(_ textView: UITextView) { + if textView == titleTextField { + updateTitleHeight() + updateTitlePlaceholderVisibility() + } } }