summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/Extensions/UITextField+Appearance.swift
blob: 9b6e94b80a291fc9a68f1de014cd94ee03971740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
//  UITextField+Appearance.swift
//  MullvadVPN
//
//  Created by Jon Petersson on 2023-04-04.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import UIKit

extension UITextField {
    @MainActor
    struct SearchTextFieldAppearance {
        let placeholderTextColor: UIColor
        let textColor: UIColor
        let backgroundColor: UIColor
        let leftViewTintColor: UIColor

        static var active: SearchTextFieldAppearance {
            SearchTextFieldAppearance(
                placeholderTextColor: .SearchTextField.placeholderTextColor,
                textColor: .SearchTextField.textColor,
                backgroundColor: .SearchTextField.backgroundColor,
                leftViewTintColor: .SearchTextField.leftViewTintColor
            )
        }

        static var inactive: SearchTextFieldAppearance {
            SearchTextFieldAppearance(
                placeholderTextColor: .SearchTextField.inactivePlaceholderTextColor,
                textColor: .SearchTextField.inactiveTextColor,
                backgroundColor: .SearchTextField.inactiveBackgroundColor,
                leftViewTintColor: .SearchTextField.inactiveLeftViewTintColor
            )
        }

        func apply(to searchBar: UISearchBar) {
            searchBar.setImage(
                UIImage.Buttons.closeSmall.withTintColor(leftViewTintColor),
                for: .clear,
                state: .normal
            )

            apply(to: searchBar.searchTextField)
        }

        func apply(to textField: UITextField) {
            textField.leftView?.tintColor = leftViewTintColor
            textField.tintColor = textColor
            textField.textColor = textColor
            textField.backgroundColor = backgroundColor

            if let customTextField = textField as? CustomTextField {
                customTextField.placeholderTextColor = placeholderTextColor
            } else {
                textField.attributedPlaceholder = NSAttributedString(
                    string: textField.placeholder ?? "",
                    attributes: [.foregroundColor: placeholderTextColor]
                )
            }
        }
    }
}