aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/iOS/ScrollableLabel.swift
blob: f2d0f58504aa14cc71f8c1e2521f85c331bcc20d (plain) (blame)
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
// SPDX-License-Identifier: MIT
// Copyright © 2018 WireGuard LLC. All Rights Reserved.

import UIKit

class ScrollableLabel: UIScrollView {
    var text: String {
        get { return label.text ?? "" }
        set(value) { label.text = value }
    }
    var textColor: UIColor {
        get { return label.textColor }
        set(value) { label.textColor = value }
    }

    let label: UILabel

    init() {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.textAlignment = .right
        self.label = label

        super.init(frame: CGRect.zero)

        self.isDirectionalLockEnabled = true
        self.showsHorizontalScrollIndicator = false
        self.showsVerticalScrollIndicator = false

        addSubview(label)
        label.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            label.leftAnchor.constraint(equalTo: self.contentLayoutGuide.leftAnchor),
            label.topAnchor.constraint(equalTo: self.contentLayoutGuide.topAnchor),
            label.bottomAnchor.constraint(equalTo: self.contentLayoutGuide.bottomAnchor),
            label.rightAnchor.constraint(equalTo: self.contentLayoutGuide.rightAnchor),
            label.heightAnchor.constraint(equalTo: self.heightAnchor)
        ])
        // If label has less content, it should expand to fit the scrollView,
        // so that right-alignment works in the label.
        let expandToFitValueLabelConstraint = NSLayoutConstraint(item: label, attribute: .width, relatedBy: .equal,
                                                                 toItem: self, attribute: .width, multiplier: 1, constant: 0)
        expandToFitValueLabelConstraint.priority = .defaultLow + 1
        expandToFitValueLabelConstraint.isActive = true
    }

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