Skip to content

Commit

Permalink
Merge pull request #40 from wordpress-mobile/issue/placeholder-ios
Browse files Browse the repository at this point in the history
Add placeholder properties for iOS AztecView.
  • Loading branch information
SergioEstevao authored Aug 12, 2018
2 parents 1bb40c6 + 65e4d8e commit 42918cd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
60 changes: 56 additions & 4 deletions ios/RNTAztecView/RCTAztecView.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
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)
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()
}

Expand All @@ -33,7 +59,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)
Expand All @@ -42,7 +68,7 @@ class RCTAztecView: Aztec.TextView {

open override func deleteBackward() {
super.deleteBackward()

updatePlaceholderVisibility()
if let onChange = onChange {
let text = packForRN(getHTML(), withName: "text")
onChange(text)
Expand Down Expand Up @@ -74,6 +100,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
}
}

3 changes: 3 additions & 0 deletions ios/RNTAztecView/RCTAztecViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 42918cd

Please sign in to comment.