session-ios/Signal/src/Loki/Components/TextField.swift

53 lines
2.1 KiB
Swift
Raw Normal View History

2019-12-02 01:58:15 +01:00
final class TextField : UITextField {
2019-12-06 01:54:04 +01:00
private let usesDefaultHeight: Bool
2019-12-02 01:58:15 +01:00
2019-12-06 01:54:04 +01:00
init(placeholder: String, usesDefaultHeight: Bool = true) {
self.usesDefaultHeight = usesDefaultHeight
2019-12-02 01:58:15 +01:00
super.init(frame: CGRect.zero)
self.placeholder = placeholder
setUpStyle()
}
override init(frame: CGRect) {
preconditionFailure("Use init(placeholder:) instead.")
}
required init?(coder: NSCoder) {
preconditionFailure("Use init(placeholder:) instead.")
}
private func setUpStyle() {
textColor = Colors.text
font = .systemFont(ofSize: Values.smallFontSize)
let placeholder = NSMutableAttributedString(string: self.placeholder!)
let placeholderColor = Colors.text.withAlphaComponent(Values.unimportantElementOpacity)
placeholder.addAttribute(.foregroundColor, value: placeholderColor, range: NSRange(location: 0, length: placeholder.length))
attributedPlaceholder = placeholder
tintColor = Colors.accent
keyboardAppearance = .dark
2019-12-06 01:54:04 +01:00
if usesDefaultHeight {
set(.height, to: Values.textFieldHeight)
}
2019-12-02 01:58:15 +01:00
layer.borderColor = Colors.border.withAlphaComponent(Values.textFieldBorderOpacity).cgColor
layer.borderWidth = Values.borderThickness
layer.cornerRadius = Values.textFieldCornerRadius
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
2019-12-06 01:54:04 +01:00
if usesDefaultHeight {
2020-01-17 05:53:56 +01:00
return bounds.insetBy(dx: isSmallScreen ? Values.mediumSpacing : Values.largeSpacing, dy: isSmallScreen ? Values.smallSpacing : Values.largeSpacing)
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
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
2019-12-06 01:54:04 +01:00
if usesDefaultHeight {
2020-01-17 05:53:56 +01:00
return bounds.insetBy(dx: isSmallScreen ? Values.mediumSpacing : Values.largeSpacing, dy: isSmallScreen ? Values.smallSpacing : Values.largeSpacing)
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
}
}