summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/EmbeddedViewContainerView.swift
blob: 9bb8fe1d2b9e3dded95601fde0e452a0bb054e90 (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
//
//  EmbeddedViewContainerView.swift
//  MullvadVPN
//
//  Created by pronebird on 09/12/2019.
//  Copyright © 2019 Mullvad VPN AB. All rights reserved.
//

import Foundation
import UIKit

/// A `UIView` subclass that implements a host view for an embedded subview via an outlet.
@IBDesignable class EmbeddedViewContainerView: UIView {
    @IBOutlet var embeddedView: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()

        backgroundColor = .clear

        embeddedView.translatesAutoresizingMaskIntoConstraints = false

        addSubview(embeddedView)

        NSLayoutConstraint.activate([
            embeddedView.topAnchor.constraint(equalTo: topAnchor),
            embeddedView.leadingAnchor.constraint(equalTo: leadingAnchor),
            embeddedView.trailingAnchor.constraint(equalTo: trailingAnchor),
            embeddedView.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])
    }

    #if TARGET_INTERFACE_BUILDER
    override func draw(_ rect: CGRect) {
        UIColor.white.withAlphaComponent(0.3).setFill()
        UIColor.white.withAlphaComponent(0.6).setStroke()

        let bezierPath = UIBezierPath(rect: rect)
        bezierPath.lineWidth = 1
        bezierPath.fill()
        bezierPath.stroke()

        let attributedString = NSAttributedString(
            string: "UIView",
            attributes: [.foregroundColor: UIColor.white]
        )

        let textSize = attributedString.size()

        var textRect = rect
        textRect.origin.x = (rect.width - textSize.width) * 0.5
        textRect.origin.y = (rect.height - textSize.height) * 0.5
        textRect.size = textSize

        attributedString.draw(in: textRect)
    }
    #endif


}