diff --git a/AnimatedTextInput/Classes/AnimatedTextInput.swift b/AnimatedTextInput/Classes/AnimatedTextInput.swift index 245eb99..76d6ede 100755 --- a/AnimatedTextInput/Classes/AnimatedTextInput.swift +++ b/AnimatedTextInput/Classes/AnimatedTextInput.swift @@ -23,7 +23,7 @@ open class AnimatedTextInput: UIControl { configureType() } } - + open var autocorrection: UITextAutocorrectionType = .no { didSet { textInput.autocorrection = autocorrection @@ -46,7 +46,7 @@ open class AnimatedTextInput: UIControl { get { return textInput.currentKeyboardAppearance } set { textInput.currentKeyboardAppearance = newValue } } - + open var clearButtonMode: UITextField.ViewMode = .whileEditing { didSet { textInput.changeClearButtonMode(with: clearButtonMode) @@ -59,7 +59,7 @@ open class AnimatedTextInput: UIControl { textInput.view.accessibilityLabel = placeHolderText } } - + // Some letters like 'g' or 'รก' were not rendered properly, the frame need to be about 20% higher than the font size open var frameHeightCorrectionFactor : Double = 1.2 { @@ -68,7 +68,7 @@ open class AnimatedTextInput: UIControl { } } - + open var placeholderAlignment: CATextLayer.Alignment = .natural { didSet { placeholderLayer.alignmentMode = CATextLayerAlignmentMode(rawValue: String(describing: placeholderAlignment)) @@ -257,7 +257,7 @@ open class AnimatedTextInput: UIControl { } fileprivate func layoutPlaceholderLayer() { - placeholderLayer.frame = CGRect(origin: placeholderPosition, size: CGSize(width: bounds.width, height: (style.textInputFont.pointSize * CGFloat(self.frameHeightCorrectionFactor)) )) + placeholderLayer.frame = CGRect(origin: placeholderPosition, size: CGSize(width: bounds.width, height: (style.placeHolderFont.pointSize * CGFloat(self.frameHeightCorrectionFactor)) )) } // mark: Configuration @@ -298,8 +298,8 @@ open class AnimatedTextInput: UIControl { placeholderLayer.masksToBounds = false placeholderLayer.string = placeHolderText placeholderLayer.foregroundColor = style.placeholderInactiveColor.cgColor - placeholderLayer.fontSize = style.textInputFont.pointSize - placeholderLayer.font = style.textInputFont + placeholderLayer.fontSize = style.placeHolderFont.pointSize + placeholderLayer.font = style.placeHolderFont placeholderLayer.contentsScale = UIScreen.main.scale placeholderLayer.backgroundColor = UIColor.clear.cgColor layoutPlaceholderLayer() @@ -319,6 +319,7 @@ open class AnimatedTextInput: UIControl { textInput.font = style.textInputFont textInput.autocorrection = autocorrection textInput.view.translatesAutoresizingMaskIntoConstraints = false + configureRightView() addSubview(textInput.view) invalidateIntrinsicContentSize() } @@ -350,7 +351,7 @@ open class AnimatedTextInput: UIControl { fileprivate func configurePlaceholderAsDefault() { isPlaceholderAsHint = false - configurePlaceholderWith(fontSize: style.textInputFont.pointSize, + configurePlaceholderWith(fontSize: style.placeHolderFont.pointSize, foregroundColor: style.placeholderInactiveColor.cgColor, text: placeHolderText) lineView.animateToInitialState() @@ -378,6 +379,15 @@ open class AnimatedTextInput: UIControl { transactionAnimation(with: duration, timingFuncion: function, animations: applyConfiguration) } + fileprivate func configureRightView() { + if let button = textInput.rightView as? UIButton { + if let selectedImage = button.image(for: .selected) { + button.tintColor = style.activeColor + button.setImage(selectedImage.withRenderingMode(.alwaysTemplate), for: .selected) + } + } + } + // mark: Behaviours @objc fileprivate func viewWasTapped(sender: UIGestureRecognizer) { @@ -391,12 +401,13 @@ open class AnimatedTextInput: UIControl { fileprivate func styleDidChange() { lineView.defaultColor = style.lineInactiveColor placeholderLayer.foregroundColor = style.placeholderInactiveColor.cgColor - let fontSize = style.textInputFont.pointSize + let fontSize = style.placeHolderFont.pointSize placeholderLayer.fontSize = fontSize - placeholderLayer.font = style.textInputFont + placeholderLayer.font = style.placeHolderFont textInput.view.tintColor = style.activeColor textInput.textColor = style.textInputFontColor textInput.font = style.textInputFont + configureRightView() invalidateIntrinsicContentSize() layoutIfNeeded() } @@ -589,7 +600,8 @@ public protocol TextInput { var currentBeginningOfDocument: UITextPosition? { get } var currentKeyboardAppearance: UIKeyboardAppearance { get set } var contentInset: UIEdgeInsets { get set } - var autocorrection: UITextAutocorrectionType {get set} + var autocorrection: UITextAutocorrectionType { get set } + var rightView: UIView? { get set } @available(iOS 10.0, *) var currentTextContentType: UITextContentType { get set } diff --git a/AnimatedTextInput/Classes/AnimatedTextInputStyle.swift b/AnimatedTextInput/Classes/AnimatedTextInputStyle.swift index 5c81b76..1aad94f 100644 --- a/AnimatedTextInput/Classes/AnimatedTextInputStyle.swift +++ b/AnimatedTextInput/Classes/AnimatedTextInputStyle.swift @@ -9,6 +9,7 @@ public protocol AnimatedTextInputStyle { var lineHeight: CGFloat { get } var errorColor: UIColor { get } var textInputFont: UIFont { get } + var placeHolderFont: UIFont { get } var textInputFontColor: UIColor { get } var placeholderMinFontSize: CGFloat { get } var counterLabelFont: UIFont? { get } @@ -31,6 +32,7 @@ public struct AnimatedTextInputStyleBlue: AnimatedTextInputStyle { public let lineHeight: CGFloat = 1.0 / UIScreen.main.scale public let errorColor = UIColor.red public let textInputFont = UIFont.systemFont(ofSize: 14) + public let placeHolderFont = UIFont.systemFont(ofSize: 14) public let textInputFontColor = UIColor.black public let placeholderMinFontSize: CGFloat = 9 public let counterLabelFont: UIFont? = UIFont.systemFont(ofSize: 9) diff --git a/AnimatedTextInput/Classes/AnimatedTextView.swift b/AnimatedTextInput/Classes/AnimatedTextView.swift index f5ccc35..e05bd5b 100755 --- a/AnimatedTextInput/Classes/AnimatedTextView.swift +++ b/AnimatedTextInput/Classes/AnimatedTextView.swift @@ -59,7 +59,7 @@ extension AnimatedTextView: TextInput { public var currentBeginningOfDocument: UITextPosition? { return self.beginningOfDocument } - + public var currentKeyboardAppearance: UIKeyboardAppearance { get { return self.keyboardAppearance } set { self.keyboardAppearance = newValue} @@ -70,6 +70,11 @@ extension AnimatedTextView: TextInput { set { self.autocorrectionType = newValue } } + public var rightView: UIView? { + get { return nil } + set { } + } + @available(iOS 10.0, *) public var currentTextContentType: UITextContentType { get { return self.textContentType } @@ -79,13 +84,13 @@ extension AnimatedTextView: TextInput { public func changeReturnKeyType(with newReturnKeyType: UIReturnKeyType) { returnKeyType = newReturnKeyType } - + public func currentPosition(from: UITextPosition, offset: Int) -> UITextPosition? { return position(from: from, offset: offset) } - + public func changeClearButtonMode(with newClearButtonMode: UITextField.ViewMode) {} - + } extension AnimatedTextView: UITextViewDelegate { diff --git a/Example/AnimatedTextInput/ViewController.swift b/Example/AnimatedTextInput/ViewController.swift index 01a5877..0ad8256 100644 --- a/Example/AnimatedTextInput/ViewController.swift +++ b/Example/AnimatedTextInput/ViewController.swift @@ -81,6 +81,7 @@ struct CustomTextInputStyle: AnimatedTextInputStyle { let lineHeight: CGFloat = 3 let errorColor = UIColor.red let textInputFont = UIFont.systemFont(ofSize: 14) + let placeHolderFont = UIFont.systemFont(ofSize: 14) let textInputFontColor = UIColor.black let placeholderMinFontSize: CGFloat = 9 let counterLabelFont: UIFont? = UIFont.systemFont(ofSize: 9) diff --git a/README.md b/README.md index ffa5504..f116ee2 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ struct CustomTextInputStyle: AnimatedTextInputStyle { let inactiveColor = UIColor.grayColor().colorWithAlphaComponent(0.3) let errorColor = UIColor.redColor() let textInputFont = UIFont.systemFontOfSize(14) + let placeholderFont = UIFont.systemFont(ofSize: 14) let textInputFontColor = UIColor.blackColor() let placeholderMinFontSize: CGFloat = 9 let counterLabelFont: UIFont? = UIFont.systemFontOfSize(9)