From 89b795609b95fdd76ef39b63762116b0a63013c7 Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Mon, 27 Mar 2017 18:56:13 +0100 Subject: [PATCH 1/3] Convert titleField to UITextView. --- Example/Example/EditorDemoController.swift | 32 +++++++++++++++------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/Example/Example/EditorDemoController.swift b/Example/Example/EditorDemoController.swift index 2a9c37bef..4562cb9f6 100644 --- a/Example/Example/EditorDemoController.swift +++ b/Example/Example/EditorDemoController.swift @@ -42,19 +42,20 @@ class EditorDemoController: UIViewController { return textView }() - fileprivate(set) lazy var titleTextField: UITextField = { + fileprivate(set) lazy var titleTextField: UITextView = { 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() + let textField = UITextView() textField.accessibilityLabel = NSLocalizedString("Title", comment: "Post title") - textField.attributedPlaceholder = NSAttributedString(string: placeholderText, - attributes: [NSForegroundColorAttributeName: UIColor.lightGray]) + //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.backgroundColor = .red + textField.isScrollEnabled = false let toolbar = self.createToolbar(htmlMode: false) toolbar.enabled = false textField.inputAccessoryView = toolbar @@ -62,6 +63,8 @@ class EditorDemoController: UIViewController { 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)) @@ -161,22 +164,28 @@ class EditorDemoController: UIViewController { } - + func updateTitleHeight() { + let sizeThatShouldFitTheContent = titleTextField.sizeThatFits(CGSize(width:self.view.frame.size.width-(2*type(of: self).margin), height: CGFloat.greatestFiniteMagnitude)) + let insets = titleTextField.textContainerInset + titleHeightConstraint.constant = max(sizeThatShouldFitTheContent.height, titleTextField.font!.lineHeight + insets.top + insets.bottom) + } // MARK: - Configuration Methods private func configureConstraints() { + titleHeightConstraint = titleTextField.heightAnchor.constraint(equalToConstant: titleTextField.font!.lineHeight) + updateTitleHeight() NSLayoutConstraint.activate([ titleTextField.leftAnchor.constraint(equalTo: view.leftAnchor, constant: type(of: self).margin), titleTextField.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -type(of: self).margin), - titleTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: type(of: self).margin), - titleTextField.heightAnchor.constraint(equalToConstant: titleTextField.font!.lineHeight) + titleTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: 0), + titleHeightConstraint ]) NSLayoutConstraint.activate([ separatorView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: type(of: self).margin), separatorView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -type(of: self).margin), - separatorView.topAnchor.constraint(equalTo: titleTextField.bottomAnchor, constant: type(of: self).margin), + separatorView.topAnchor.constraint(equalTo: titleTextField.bottomAnchor, constant: 0), separatorView.heightAnchor.constraint(equalToConstant: separatorView.frame.height) ]) @@ -320,7 +329,10 @@ extension EditorDemoController : UITextViewDelegate { changeRichTextInputView(to: nil) } - func textViewDidChange(_ textView: UITextView) { + func textViewDidChange(_ textView: UITextView) { + if textView == titleTextField { + updateTitleHeight() + } } } From 040212dd2a653187e953d9cd8f90a027c3e8d207 Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Wed, 29 Mar 2017 13:57:27 +0100 Subject: [PATCH 2/3] Add placeholder label for title. --- Example/Example/EditorDemoController.swift | 34 ++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/Example/Example/EditorDemoController.swift b/Example/Example/EditorDemoController.swift index a1109a61b..85da72665 100644 --- a/Example/Example/EditorDemoController.swift +++ b/Example/Example/EditorDemoController.swift @@ -39,19 +39,15 @@ class EditorDemoController: UIViewController { return textView }() - fileprivate(set) lazy var titleTextField: UITextView = { - let placeholderText = NSLocalizedString("Enter title here", comment: "Label for the title of the post field. Should be the same as WP core.") + 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.backgroundColor = .red textField.isScrollEnabled = false let toolbar = self.createToolbar(htmlMode: false) toolbar.enabled = false @@ -60,6 +56,18 @@ 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 = { @@ -111,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) @@ -161,11 +170,17 @@ class EditorDemoController: UIViewController { } + // MARK: - Title and Title placeholder position methods func updateTitleHeight() { let sizeThatShouldFitTheContent = titleTextField.sizeThatFits(CGSize(width:self.view.frame.size.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 = !self.titleTextField.text.isEmpty + } + // MARK: - Configuration Methods private func configureConstraints() { @@ -179,6 +194,14 @@ class EditorDemoController: UIViewController { 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), @@ -329,6 +352,7 @@ extension EditorDemoController : UITextViewDelegate { func textViewDidChange(_ textView: UITextView) { if textView == titleTextField { updateTitleHeight() + updateTitlePlaceholderVisibility() } } } From 5138a5bafb957a5ca705e48a182b73b27f95552b Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Thu, 30 Mar 2017 11:29:46 +0100 Subject: [PATCH 3/3] Address review comments. --- Example/Example/EditorDemoController.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Example/Example/EditorDemoController.swift b/Example/Example/EditorDemoController.swift index 85da72665..687f78f28 100644 --- a/Example/Example/EditorDemoController.swift +++ b/Example/Example/EditorDemoController.swift @@ -172,13 +172,13 @@ class EditorDemoController: UIViewController { // MARK: - Title and Title placeholder position methods func updateTitleHeight() { - let sizeThatShouldFitTheContent = titleTextField.sizeThatFits(CGSize(width:self.view.frame.size.width - ( 2 * Constants.margin), height: CGFloat.greatestFiniteMagnitude)) + 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 = !self.titleTextField.text.isEmpty + self.titlePlaceholderLabel.isHidden = !titleTextField.text.isEmpty } // MARK: - Configuration Methods