Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Complete placeholder font & icon active color #119

Closed
wants to merge 11 commits into from
34 changes: 23 additions & 11 deletions AnimatedTextInput/Classes/AnimatedTextInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ open class AnimatedTextInput: UIControl {
configureType()
}
}

open var autocorrection: UITextAutocorrectionType = .no {
didSet {
textInput.autocorrection = autocorrection
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -68,7 +68,7 @@ open class AnimatedTextInput: UIControl {
}

}

open var placeholderAlignment: CATextLayer.Alignment = .natural {
didSet {
placeholderLayer.alignmentMode = CATextLayerAlignmentMode(rawValue: String(describing: placeholderAlignment))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -319,6 +319,7 @@ open class AnimatedTextInput: UIControl {
textInput.font = style.textInputFont
textInput.autocorrection = autocorrection
textInput.view.translatesAutoresizingMaskIntoConstraints = false
configureRightView()
addSubview(textInput.view)
invalidateIntrinsicContentSize()
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
}
Expand Down Expand Up @@ -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 }

Expand Down
2 changes: 2 additions & 0 deletions AnimatedTextInput/Classes/AnimatedTextInputStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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)
Expand Down
13 changes: 9 additions & 4 deletions AnimatedTextInput/Classes/AnimatedTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -70,6 +70,11 @@ extension AnimatedTextView: TextInput {
set { self.autocorrectionType = newValue }
}

public var rightView: UIView? {
get { return nil }
set { }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove setter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UITextField public interface requires setter.
Yes, this code is unused in text view, but it's needed for AnimatedTextField

}

@available(iOS 10.0, *)
public var currentTextContentType: UITextContentType {
get { return self.textContentType }
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions Example/AnimatedTextInput/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious.. Have you tried that the component is working when placeHolderFont.pointSize > textInputFont.pointSize ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will work, but basically you want input font & placeholder font to be the same.

... Now I start thinking if we ever need to set placeholderFont to anything other than textInputFont.

let textInputFontColor = UIColor.black
let placeholderMinFontSize: CGFloat = 9
let counterLabelFont: UIFont? = UIFont.systemFont(ofSize: 9)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down