session-ios/SessionUIKit/Components/TextField.swift

69 lines
2.9 KiB
Swift
Raw Normal View History

2020-11-09 06:03:59 +01:00
import UIKit
2019-12-02 01:58:15 +01:00
2021-03-01 03:15:54 +01:00
@objc(SNTextField)
2020-11-09 06:03:59 +01:00
public final class TextField : UITextField {
2019-12-06 01:54:04 +01:00
private let usesDefaultHeight: Bool
2020-04-07 03:56:16 +02:00
private let height: CGFloat
private let horizontalInset: CGFloat
private let verticalInset: CGFloat
2020-01-30 10:09:02 +01:00
2021-01-29 01:46:32 +01:00
static let height: CGFloat = isIPhone5OrSmaller ? CGFloat(48) : CGFloat(80)
public static let cornerRadius: CGFloat = 8
2021-03-01 03:15:54 +01:00
@objc(initWithPlaceholder:usesDefaultHeight:)
public convenience init(placeholder: String, usesDefaultHeight: Bool) {
self.init(placeholder: placeholder, usesDefaultHeight: usesDefaultHeight, customHeight: nil, customHorizontalInset: nil, customVerticalInset: nil)
}
2020-11-09 06:03:59 +01:00
public init(placeholder: String, usesDefaultHeight: Bool = true, customHeight: CGFloat? = nil, customHorizontalInset: CGFloat? = nil, customVerticalInset: CGFloat? = nil) {
2019-12-06 01:54:04 +01:00
self.usesDefaultHeight = usesDefaultHeight
2021-01-29 01:46:32 +01:00
self.height = customHeight ?? TextField.height
2020-06-18 06:41:02 +02:00
self.horizontalInset = customHorizontalInset ?? (isIPhone5OrSmaller ? Values.mediumSpacing : Values.largeSpacing)
self.verticalInset = customVerticalInset ?? (isIPhone5OrSmaller ? Values.smallSpacing : Values.largeSpacing)
2019-12-02 01:58:15 +01:00
super.init(frame: CGRect.zero)
self.placeholder = placeholder
setUpStyle()
}
2020-11-09 06:03:59 +01:00
public override init(frame: CGRect) {
2019-12-02 01:58:15 +01:00
preconditionFailure("Use init(placeholder:) instead.")
}
2020-11-09 06:03:59 +01:00
public required init?(coder: NSCoder) {
2019-12-02 01:58:15 +01:00
preconditionFailure("Use init(placeholder:) instead.")
}
private func setUpStyle() {
textColor = Colors.text
font = .systemFont(ofSize: Values.smallFontSize)
let placeholder = NSMutableAttributedString(string: self.placeholder!)
2021-01-29 01:46:32 +01:00
let placeholderColor = Colors.text.withAlphaComponent(Values.mediumOpacity)
2019-12-02 01:58:15 +01:00
placeholder.addAttribute(.foregroundColor, value: placeholderColor, range: NSRange(location: 0, length: placeholder.length))
attributedPlaceholder = placeholder
tintColor = Colors.accent
2020-02-17 02:08:46 +01:00
keyboardAppearance = isLightMode ? .light : .dark
2019-12-06 01:54:04 +01:00
if usesDefaultHeight {
2020-04-07 03:56:16 +02:00
set(.height, to: height)
2019-12-06 01:54:04 +01:00
}
2021-01-29 01:46:32 +01:00
layer.borderColor = isLightMode ? Colors.text.cgColor : Colors.border.withAlphaComponent(Values.lowOpacity).cgColor
layer.borderWidth = 1
layer.cornerRadius = TextField.cornerRadius
2019-12-02 01:58:15 +01:00
}
2020-11-09 06:03:59 +01:00
public override func textRect(forBounds bounds: CGRect) -> CGRect {
2019-12-06 01:54:04 +01:00
if usesDefaultHeight {
2020-01-30 10:09:02 +01:00
return bounds.insetBy(dx: horizontalInset, dy: verticalInset)
2019-12-06 01:54:04 +01:00
} else {
return bounds.insetBy(dx: Values.mediumSpacing, dy: Values.smallSpacing)
}
2019-12-02 01:58:15 +01:00
}
2020-11-09 06:03:59 +01:00
public override func editingRect(forBounds bounds: CGRect) -> CGRect {
2019-12-06 01:54:04 +01:00
if usesDefaultHeight {
2020-01-30 10:09:02 +01:00
return bounds.insetBy(dx: horizontalInset, dy: verticalInset)
2019-12-06 01:54:04 +01:00
} else {
return bounds.insetBy(dx: Values.mediumSpacing, dy: Values.smallSpacing)
}
2019-12-02 01:58:15 +01:00
}
}