Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions Example/Example/EditorDemoController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,37 @@ 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

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))

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
])

Expand Down Expand Up @@ -317,7 +349,11 @@ extension EditorDemoController : UITextViewDelegate {
changeRichTextInputView(to: nil)
}

func textViewDidChange(_ textView: UITextView) {
func textViewDidChange(_ textView: UITextView) {
if textView == titleTextField {
updateTitleHeight()
updateTitlePlaceholderVisibility()
}
}
}

Expand Down