session-ios/SessionUIKit/Components/TextField.swift
Morgan Pretty 8109a326cf Merge remote-tracking branch 'upstream/dev' into feature/theming
# Conflicts:
#	Session.xcodeproj/project.pbxproj
#	Session/Closed Groups/NewClosedGroupVC.swift
#	Session/Conversations/ConversationVC+Interaction.swift
#	Session/Conversations/Message Cells/CallMessageCell.swift
#	Session/Conversations/Views & Modals/JoinOpenGroupModal.swift
#	Session/Home/HomeVC.swift
#	Session/Home/New Conversation/NewDMVC.swift
#	Session/Home/NewConversationButtonSet.swift
#	Session/Meta/Translations/de.lproj/Localizable.strings
#	Session/Meta/Translations/en.lproj/Localizable.strings
#	Session/Meta/Translations/es.lproj/Localizable.strings
#	Session/Meta/Translations/fa.lproj/Localizable.strings
#	Session/Meta/Translations/fi.lproj/Localizable.strings
#	Session/Meta/Translations/fr.lproj/Localizable.strings
#	Session/Meta/Translations/hi.lproj/Localizable.strings
#	Session/Meta/Translations/hr.lproj/Localizable.strings
#	Session/Meta/Translations/id-ID.lproj/Localizable.strings
#	Session/Meta/Translations/it.lproj/Localizable.strings
#	Session/Meta/Translations/ja.lproj/Localizable.strings
#	Session/Meta/Translations/nl.lproj/Localizable.strings
#	Session/Meta/Translations/pl.lproj/Localizable.strings
#	Session/Meta/Translations/pt_BR.lproj/Localizable.strings
#	Session/Meta/Translations/ru.lproj/Localizable.strings
#	Session/Meta/Translations/si.lproj/Localizable.strings
#	Session/Meta/Translations/sk.lproj/Localizable.strings
#	Session/Meta/Translations/sv.lproj/Localizable.strings
#	Session/Meta/Translations/th.lproj/Localizable.strings
#	Session/Meta/Translations/vi-VN.lproj/Localizable.strings
#	Session/Meta/Translations/zh-Hant.lproj/Localizable.strings
#	Session/Meta/Translations/zh_CN.lproj/Localizable.strings
#	Session/Open Groups/JoinOpenGroupVC.swift
#	Session/Open Groups/OpenGroupSuggestionGrid.swift
#	Session/Settings/SettingsVC.swift
#	Session/Shared/BaseVC.swift
#	Session/Shared/OWSQRCodeScanningViewController.m
#	Session/Shared/ScanQRCodeWrapperVC.swift
#	Session/Shared/UserCell.swift
#	SessionMessagingKit/Configuration.swift
#	SessionShareExtension/SAEScreenLockViewController.swift
#	SessionUIKit/Style Guide/Gradients.swift
#	SignalUtilitiesKit/Media Viewing & Editing/OWSViewController+ImageEditor.swift
#	SignalUtilitiesKit/Screen Lock/ScreenLockViewController.m
2022-09-26 11:16:47 +10:00

86 lines
3.2 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import UIKit
@objc(SNTextField)
public final class TextField: UITextField {
private let usesDefaultHeight: Bool
private let height: CGFloat
private let horizontalInset: CGFloat
private let verticalInset: CGFloat
static let height: CGFloat = isIPhone5OrSmaller ? CGFloat(48) : CGFloat(80)
public static let cornerRadius: CGFloat = 8
@objc(initWithPlaceholder:usesDefaultHeight:)
public convenience init(placeholder: String, usesDefaultHeight: Bool) {
self.init(placeholder: placeholder, usesDefaultHeight: usesDefaultHeight, customHeight: nil, customHorizontalInset: nil, customVerticalInset: nil)
}
public init(placeholder: String, usesDefaultHeight: Bool = true, customHeight: CGFloat? = nil, customHorizontalInset: CGFloat? = nil, customVerticalInset: CGFloat? = nil) {
self.usesDefaultHeight = usesDefaultHeight
self.height = customHeight ?? TextField.height
self.horizontalInset = customHorizontalInset ?? (isIPhone5OrSmaller ? Values.mediumSpacing : Values.largeSpacing)
self.verticalInset = customVerticalInset ?? (isIPhone5OrSmaller ? Values.smallSpacing : Values.largeSpacing)
super.init(frame: CGRect.zero)
self.placeholder = placeholder
setUpStyle()
}
public override init(frame: CGRect) {
preconditionFailure("Use init(placeholder:) instead.")
}
public required init?(coder: NSCoder) {
preconditionFailure("Use init(placeholder:) instead.")
}
private func setUpStyle() {
font = .systemFont(ofSize: Values.smallFontSize)
themeTextColor = .textPrimary
themeTintColor = .primary
themeBorderColor = .borderSeparator
layer.borderWidth = 1
layer.cornerRadius = TextField.cornerRadius
if usesDefaultHeight {
set(.height, to: height)
}
ThemeManager.onThemeChange(observer: self) { [weak self] theme, _ in
switch theme.interfaceStyle {
case .light: self?.keyboardAppearance = .light
default: self?.keyboardAppearance = .dark
}
if let textSecondary: UIColor = theme.color(for: .textSecondary), let placeholder: String = self?.placeholder {
self?.attributedPlaceholder = NSAttributedString(
string: placeholder,
attributes: [ .foregroundColor: textSecondary]
)
}
}
}
public override func textRect(forBounds bounds: CGRect) -> CGRect {
if usesDefaultHeight {
return bounds.insetBy(dx: horizontalInset, dy: verticalInset)
}
else {
return bounds.insetBy(dx: Values.mediumSpacing, dy: Values.smallSpacing)
}
}
public override func editingRect(forBounds bounds: CGRect) -> CGRect {
if usesDefaultHeight {
return bounds.insetBy(dx: horizontalInset, dy: verticalInset)
}
else {
return bounds.insetBy(dx: Values.mediumSpacing, dy: Values.smallSpacing)
}
}
}