Files
MessageKit/Sources/Views/InputTextView.swift
T
2017-11-01 15:12:30 +08:00

177 lines
6.7 KiB
Swift

/*
MIT License
Copyright (c) 2017 MessageKit
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import UIKit
/**
A UITextView that has a UILabel embedded for placeholder text
## Important Notes ##
1. Changing the font, textAlignment or textContainerInset automatically performs the same modifications to the placeholderLabel
2. Intended to be used in an `MessageInputBar`
3. Default placeholder text is "New Message"
4. Will pass a pasted image it's `MessageInputBar`'s `InputManager`s
*/
open class InputTextView: UITextView {
// MARK: - Properties
open override var text: String! {
didSet {
placeholderLabel.isHidden = !text.isEmpty
}
}
open override var attributedText: NSAttributedString! {
didSet {
placeholderLabel.isHidden = !text.isEmpty
}
}
/// A UILabel that holds the InputTextView's placeholder text
open let placeholderLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.textColor = .lightGray
label.text = "New Message"
label.backgroundColor = .clear
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
/// The placeholder text that appears when there is no text. The default value is "New Message"
open var placeholder: String? = "New Message" {
didSet {
placeholderLabel.text = placeholder
}
}
/// The placeholderLabel's textColor
open var placeholderTextColor: UIColor? = .lightGray {
didSet {
placeholderLabel.textColor = placeholderTextColor
}
}
/// The UIEdgeInsets the placeholderLabel has within the InputTextView
open var placeholderLabelInsets: UIEdgeInsets = UIEdgeInsets(top: 4, left: 7, bottom: 4, right: 7) {
didSet {
updateConstraintsForPlaceholderLabel()
}
}
/// The font of the InputTextView. When set the placeholderLabel's font is also updated
open override var font: UIFont! {
didSet {
placeholderLabel.font = font
}
}
/// The textAlignment of the InputTextView. When set the placeholderLabel's textAlignment is also updated
open override var textAlignment: NSTextAlignment {
didSet {
placeholderLabel.textAlignment = textAlignment
}
}
open override var scrollIndicatorInsets: UIEdgeInsets {
didSet {
// When .zero a rendering issue can occur
if scrollIndicatorInsets == .zero {
scrollIndicatorInsets = UIEdgeInsets(top: .leastNonzeroMagnitude,
left: .leastNonzeroMagnitude,
bottom: .leastNonzeroMagnitude,
right: .leastNonzeroMagnitude)
}
}
}
/// A weak reference to the MessageInputBar that the InputTextView is contained within
open weak var messageInputBar: MessageInputBar?
/// The constraints of the placeholderLabel
private var placeholderLabelConstraintSet: NSLayoutConstraintSet?
// MARK: - Initializers
public convenience init() {
self.init(frame: .zero)
}
public override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
setup()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
// MARK: - Setup
/// Sets up the default properties
open func setup() {
font = UIFont.preferredFont(forTextStyle: .body)
textContainerInset = UIEdgeInsets(top: 4, left: 4, bottom: 4, right: 4)
scrollIndicatorInsets = UIEdgeInsets(top: .leastNonzeroMagnitude,
left: .leastNonzeroMagnitude,
bottom: .leastNonzeroMagnitude,
right: .leastNonzeroMagnitude)
isScrollEnabled = false
layer.cornerRadius = 5.0
layer.borderWidth = 1.25
layer.borderColor = UIColor.lightGray.cgColor
setupPlaceholderLabel()
}
/// Adds the placeholderLabel to the view and sets up its initial constraints
private func setupPlaceholderLabel() {
addSubview(placeholderLabel)
placeholderLabelConstraintSet = NSLayoutConstraintSet(
top: placeholderLabel.topAnchor.constraint(equalTo: topAnchor, constant: placeholderLabelInsets.top),
bottom: placeholderLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -placeholderLabelInsets.bottom),
left: placeholderLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: placeholderLabelInsets.left),
right: placeholderLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: (placeholderLabelInsets.left + placeholderLabelInsets.right)),
centerX: placeholderLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
centerY: placeholderLabel.centerYAnchor.constraint(equalTo: centerYAnchor)
).activate()
}
/// Updates the placeholderLabels constraint constants to match the placeholderLabelInsets
private func updateConstraintsForPlaceholderLabel() {
placeholderLabelConstraintSet?.top?.constant = placeholderLabelInsets.top
placeholderLabelConstraintSet?.bottom?.constant = -placeholderLabelInsets.bottom
placeholderLabelConstraintSet?.left?.constant = placeholderLabelInsets.left
placeholderLabelConstraintSet?.right?.constant = -placeholderLabelInsets.right
}
}