From c968311a6564fad1777fcfd4bc855564046ae3e1 Mon Sep 17 00:00:00 2001 From: SergioEstevao Date: Tue, 31 Jul 2018 14:36:32 +0100 Subject: [PATCH 1/2] Add placeholder properties for iOS AztecView. --- ios/RNTAztecView/RCTAztecView.swift | 62 ++++++++++++++++++++++++-- ios/RNTAztecView/RCTAztecViewManager.m | 3 ++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/ios/RNTAztecView/RCTAztecView.swift b/ios/RNTAztecView/RCTAztecView.swift index 4c7848685ae12b..525d27446551cd 100644 --- a/ios/RNTAztecView/RCTAztecView.swift +++ b/ios/RNTAztecView/RCTAztecView.swift @@ -1,17 +1,45 @@ import Aztec import Foundation +import UIKit class RCTAztecView: Aztec.TextView { @objc var onChange: RCTBubblingEventBlock? = nil @objc var onContentSizeChange: RCTBubblingEventBlock? = nil private var previousContentSize: CGSize = .zero - + + private lazy var placeholderLabel: UILabel = { + let label = UILabel(frame: .zero) + label.text = "" + label.sizeToFit() + return label + }() + + override init(defaultFont: UIFont, defaultParagraphStyle: ParagraphStyle, defaultMissingImage: UIImage) { + super.init(defaultFont: defaultFont, defaultParagraphStyle: defaultParagraphStyle, defaultMissingImage: defaultMissingImage) + commonInit() + } + + required init?(coder aDecoder: NSCoder) { + super.init(coder: aDecoder) + commonInit() + } + + func commonInit() { + addSubview(placeholderLabel) + placeholderLabel.textAlignment = .natural + placeholderLabel.translatesAutoresizingMaskIntoConstraints = false + placeholderLabel.font = font + NSLayoutConstraint.activate([ + placeholderLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: contentInset.left + textContainerInset.left + textContainer.lineFragmentPadding), + placeholderLabel.topAnchor.constraint(equalTo: topAnchor, constant: contentInset.top + textContainerInset.top) + ]) + } + // MARK - View Height: Match to content height override func layoutSubviews() { super.layoutSubviews() - updateContentSizeInRN() } @@ -33,7 +61,7 @@ class RCTAztecView: Aztec.TextView { open override func insertText(_ text: String) { super.insertText(text) - + updatePlaceholderVisibility() if let onChange = onChange { let text = packForRN(getHTML(), withName: "text") onChange(text) @@ -42,7 +70,7 @@ class RCTAztecView: Aztec.TextView { open override func deleteBackward() { super.deleteBackward() - + updatePlaceholderVisibility() if let onChange = onChange { let text = packForRN(getHTML(), withName: "text") onChange(text) @@ -74,6 +102,32 @@ class RCTAztecView: Aztec.TextView { let html = contents["text"] as? String ?? "" setHTML(html) + updatePlaceholderVisibility() + } + + // MARK: - Placeholder + + @objc var placeholder: String { + set { + placeholderLabel.text = newValue + } + + get { + return placeholderLabel.text ?? "" + } + } + + @objc var placeholderTextColor: UIColor { + set { + placeholderLabel.textColor = newValue + } + get { + return placeholderLabel.textColor + } + } + + func updatePlaceholderVisibility() { + placeholderLabel.isHidden = !self.text.isEmpty } } diff --git a/ios/RNTAztecView/RCTAztecViewManager.m b/ios/RNTAztecView/RCTAztecViewManager.m index 8e9fc4bc3487c5..4d47c577d41ca2 100644 --- a/ios/RNTAztecView/RCTAztecViewManager.m +++ b/ios/RNTAztecView/RCTAztecViewManager.m @@ -9,4 +9,7 @@ @implementation RCTAztecViewManager (RCTExternModule) RCT_EXPORT_VIEW_PROPERTY(onContentSizeChange, RCTBubblingEventBlock) RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock) +RCT_EXPORT_VIEW_PROPERTY(placeholder, NSString) +RCT_EXPORT_VIEW_PROPERTY(placeholderTextColor, UIColor) + @end From 65e4d8e57b4640e05c284e91e74e7a061a9671ba Mon Sep 17 00:00:00 2001 From: SergioEstevao Date: Sun, 12 Aug 2018 17:12:34 +0100 Subject: [PATCH 2/2] Remove unnecessary initialisation. --- ios/RNTAztecView/RCTAztecView.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/ios/RNTAztecView/RCTAztecView.swift b/ios/RNTAztecView/RCTAztecView.swift index 525d27446551cd..65841ff0d94099 100644 --- a/ios/RNTAztecView/RCTAztecView.swift +++ b/ios/RNTAztecView/RCTAztecView.swift @@ -10,8 +10,6 @@ class RCTAztecView: Aztec.TextView { private lazy var placeholderLabel: UILabel = { let label = UILabel(frame: .zero) - label.text = "" - label.sizeToFit() return label }()