summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/CustomButton.swift
blob: 1b5bcbece36269d98873c63b274fb5587a5db830 (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
64
65
66
67
68
//
//  CustomButton.swift
//  MullvadVPN
//
//  Created by pronebird on 23/05/2019.
//  Copyright © 2019 Mullvad VPN AB. All rights reserved.
//

import UIKit

@IBDesignable class CustomButton: UIButton {

    override init(frame: CGRect) {
        super.init(frame: frame)

        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        commonInit()
    }

    private func commonInit() {
        var contentInsets = contentEdgeInsets

        if contentInsets.top == 0 {
            contentInsets.top = 10
        }

        if contentInsets.bottom == 0 {
            contentInsets.bottom = 10
        }

        if contentInsets.right == 0 {
            contentInsets.right = 10
        }

        if contentInsets.left == 0 {
            contentInsets.left = 10
        }

        contentEdgeInsets = contentInsets
        titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .semibold)

        setTitleColor(UIColor.white, for: .normal)
        setTitleColor(UIColor.lightGray, for: .highlighted)
        setTitleColor(UIColor.lightGray, for: .disabled)
    }

    override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
        var imageRect = super.imageRect(forContentRect: contentRect)

        imageRect.origin.x = contentRect.maxX - imageRect.size.width

        return imageRect
    }

    override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
        var titleRect = super.titleRect(forContentRect: contentRect)

        titleRect.origin.x = contentRect.midX - titleRect.width * 0.5

        return titleRect
    }

}