|
| 1 | +// |
| 2 | +// IPOverrideHeaderView.swift |
| 3 | +// MullvadVPN |
| 4 | +// |
| 5 | +// Created by Jon Petersson on 2024-03-20. |
| 6 | +// Copyright © 2024 Mullvad VPN AB. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +/// Header view pinned at the top of ``IPOverrideViewController``. |
| 12 | +class IPOverrideHeaderView: UIView, UITextViewDelegate { |
| 13 | + /// Event handler invoked when user taps on the link to learn more about API access. |
| 14 | + var onAbout: (() -> Void)? |
| 15 | + |
| 16 | + private let textView = UITextView() |
| 17 | + |
| 18 | + override init(frame: CGRect) { |
| 19 | + super.init(frame: frame) |
| 20 | + |
| 21 | + textView.backgroundColor = .clear |
| 22 | + textView.dataDetectorTypes = .link |
| 23 | + textView.isSelectable = true |
| 24 | + textView.isEditable = false |
| 25 | + textView.isScrollEnabled = false |
| 26 | + textView.contentInset = .zero |
| 27 | + textView.textContainerInset = .zero |
| 28 | + textView.attributedText = makeAttributedString() |
| 29 | + textView.linkTextAttributes = defaultLinkAttributes |
| 30 | + textView.textContainer.lineFragmentPadding = 0 |
| 31 | + textView.delegate = self |
| 32 | + |
| 33 | + directionalLayoutMargins = .zero |
| 34 | + |
| 35 | + addSubviews() |
| 36 | + } |
| 37 | + |
| 38 | + required init?(coder: NSCoder) { |
| 39 | + fatalError("init(coder:) has not been implemented") |
| 40 | + } |
| 41 | + |
| 42 | + private let defaultTextAttributes: [NSAttributedString.Key: Any] = [ |
| 43 | + .font: UIFont.systemFont(ofSize: 13), |
| 44 | + .foregroundColor: UIColor.ContentHeading.textColor, |
| 45 | + ] |
| 46 | + |
| 47 | + private let defaultLinkAttributes: [NSAttributedString.Key: Any] = [ |
| 48 | + .font: UIFont.systemFont(ofSize: 13), |
| 49 | + .foregroundColor: UIColor.ContentHeading.linkColor, |
| 50 | + ] |
| 51 | + |
| 52 | + private func makeAttributedString() -> NSAttributedString { |
| 53 | + let body = NSLocalizedString( |
| 54 | + "IP_OVERRIDE_HEADER_BODY", |
| 55 | + tableName: "IPOverride", |
| 56 | + value: "Import files or text with new IP addresses for the servers in the Select location view.", |
| 57 | + comment: "" |
| 58 | + ) |
| 59 | + let link = NSLocalizedString( |
| 60 | + "IP_OVERRIDE_HEADER_LINK", |
| 61 | + tableName: "IPOverride", |
| 62 | + value: "About IP override...", |
| 63 | + comment: "" |
| 64 | + ) |
| 65 | + |
| 66 | + var linkAttributes = defaultLinkAttributes |
| 67 | + linkAttributes[.link] = "#" |
| 68 | + |
| 69 | + let paragraphStyle = NSMutableParagraphStyle() |
| 70 | + paragraphStyle.lineBreakMode = .byWordWrapping |
| 71 | + |
| 72 | + let attributedString = NSMutableAttributedString() |
| 73 | + attributedString.append(NSAttributedString(string: body, attributes: defaultTextAttributes)) |
| 74 | + attributedString.append(NSAttributedString(string: " ", attributes: defaultTextAttributes)) |
| 75 | + attributedString.append(NSAttributedString(string: link, attributes: linkAttributes)) |
| 76 | + attributedString.addAttribute( |
| 77 | + .paragraphStyle, |
| 78 | + value: paragraphStyle, |
| 79 | + range: NSRange(location: 0, length: attributedString.length) |
| 80 | + ) |
| 81 | + return attributedString |
| 82 | + } |
| 83 | + |
| 84 | + private func addSubviews() { |
| 85 | + addConstrainedSubviews([textView]) { |
| 86 | + textView.pinEdgesToSuperviewMargins() |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + func textView( |
| 91 | + _ textView: UITextView, |
| 92 | + shouldInteractWith URL: URL, |
| 93 | + in characterRange: NSRange, |
| 94 | + interaction: UITextItemInteraction |
| 95 | + ) -> Bool { |
| 96 | + onAbout?() |
| 97 | + return false |
| 98 | + } |
| 99 | + |
| 100 | + @available(iOS 17.0, *) |
| 101 | + func textView(_ textView: UITextView, menuConfigurationFor textItem: UITextItem, defaultMenu: UIMenu) -> UITextItem |
| 102 | + .MenuConfiguration? { |
| 103 | + return nil |
| 104 | + } |
| 105 | + |
| 106 | + @available(iOS 17.0, *) |
| 107 | + func textView(_ textView: UITextView, primaryActionFor textItem: UITextItem, defaultAction: UIAction) -> UIAction? { |
| 108 | + if case .link = textItem.content { |
| 109 | + return UIAction { [weak self] _ in |
| 110 | + self?.onAbout?() |
| 111 | + } |
| 112 | + } |
| 113 | + return nil |
| 114 | + } |
| 115 | +} |
0 commit comments