mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
2.5 KiB
Swift
60 lines
2.5 KiB
Swift
5 years ago
|
import UIKit
|
||
6 years ago
|
|
||
5 years ago
|
public final class TextField : UITextField {
|
||
5 years ago
|
private let usesDefaultHeight: Bool
|
||
5 years ago
|
private let height: CGFloat
|
||
|
private let horizontalInset: CGFloat
|
||
|
private let verticalInset: CGFloat
|
||
5 years ago
|
|
||
5 years ago
|
public init(placeholder: String, usesDefaultHeight: Bool = true, customHeight: CGFloat? = nil, customHorizontalInset: CGFloat? = nil, customVerticalInset: CGFloat? = nil) {
|
||
5 years ago
|
self.usesDefaultHeight = usesDefaultHeight
|
||
5 years ago
|
self.height = customHeight ?? Values.textFieldHeight
|
||
5 years ago
|
self.horizontalInset = customHorizontalInset ?? (isIPhone5OrSmaller ? Values.mediumSpacing : Values.largeSpacing)
|
||
|
self.verticalInset = customVerticalInset ?? (isIPhone5OrSmaller ? Values.smallSpacing : Values.largeSpacing)
|
||
6 years ago
|
super.init(frame: CGRect.zero)
|
||
|
self.placeholder = placeholder
|
||
|
setUpStyle()
|
||
|
}
|
||
|
|
||
5 years ago
|
public override init(frame: CGRect) {
|
||
6 years ago
|
preconditionFailure("Use init(placeholder:) instead.")
|
||
|
}
|
||
|
|
||
5 years ago
|
public required init?(coder: NSCoder) {
|
||
6 years ago
|
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
|
||
5 years ago
|
keyboardAppearance = isLightMode ? .light : .dark
|
||
5 years ago
|
if usesDefaultHeight {
|
||
5 years ago
|
set(.height, to: height)
|
||
5 years ago
|
}
|
||
5 years ago
|
layer.borderColor = isLightMode ? Colors.text.cgColor : Colors.border.withAlphaComponent(Values.textFieldBorderOpacity).cgColor
|
||
6 years ago
|
layer.borderWidth = Values.borderThickness
|
||
|
layer.cornerRadius = Values.textFieldCornerRadius
|
||
|
}
|
||
|
|
||
5 years ago
|
public override func textRect(forBounds bounds: CGRect) -> CGRect {
|
||
5 years ago
|
if usesDefaultHeight {
|
||
5 years ago
|
return bounds.insetBy(dx: horizontalInset, dy: verticalInset)
|
||
5 years ago
|
} else {
|
||
|
return bounds.insetBy(dx: Values.mediumSpacing, dy: Values.smallSpacing)
|
||
|
}
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
public override func editingRect(forBounds bounds: CGRect) -> CGRect {
|
||
5 years ago
|
if usesDefaultHeight {
|
||
5 years ago
|
return bounds.insetBy(dx: horizontalInset, dy: verticalInset)
|
||
5 years ago
|
} else {
|
||
|
return bounds.insetBy(dx: Values.mediumSpacing, dy: Values.smallSpacing)
|
||
|
}
|
||
6 years ago
|
}
|
||
|
}
|