summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/Views/InfoHeaderView.swift
blob: 403ba7bb27f92356529f42a63f11fdbfe98bb333 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
//  InfoHeaderView.swift
//  MullvadVPN
//
//  Created by Jon Petersson on 2024-10-01.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import MullvadSettings
import UIKit

/// Header view pinned at the top of a ``ViewController``.
class InfoHeaderView: UIView, UITextViewDelegate {
    /// Event handler invoked when user taps on the link.
    var onAbout: (() -> Void)?

    private let infoLabel = UILabel()
    private let config: InfoHeaderConfig

    init(config: InfoHeaderConfig) {
        self.config = config

        super.init(frame: .zero)

        infoLabel.backgroundColor = .clear
        infoLabel.attributedText = makeAttributedString()
        infoLabel.adjustsFontForContentSizeCategory = true
        infoLabel.numberOfLines = 0
        infoLabel.accessibilityTraits = .link

        directionalLayoutMargins = .zero

        addSubviews()
        addTapGestureRecognizer()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private let defaultTextAttributes: [NSAttributedString.Key: Any] = [
        .font: UIFont.mullvadTiny,
        .foregroundColor: UIColor.ContentHeading.textColor,
    ]

    private let defaultLinkAttributes: [NSAttributedString.Key: Any] = [
        .font: UIFont.mullvadTiny,
        .foregroundColor: UIColor.ContentHeading.linkColor,
        .attachment: "#",
    ]

    private func makeAttributedString() -> NSAttributedString {
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineBreakMode = .byWordWrapping

        let attributedString = NSMutableAttributedString()
        attributedString.append(NSAttributedString(string: config.body, attributes: defaultTextAttributes))
        attributedString.append(NSAttributedString(string: " ", attributes: defaultTextAttributes))
        attributedString.append(NSAttributedString(string: config.link, attributes: defaultLinkAttributes))
        attributedString.addAttribute(
            .paragraphStyle,
            value: paragraphStyle,
            range: NSRange(location: 0, length: attributedString.length)
        )
        return attributedString
    }

    private func addSubviews() {
        addConstrainedSubviews([infoLabel]) {
            infoLabel.pinEdgesToSuperviewMargins()
        }
    }

    private func addTapGestureRecognizer() {
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTextViewTap))
        addGestureRecognizer(tapGesture)
    }

    @objc
    private func handleTextViewTap() {
        onAbout?()
    }
}