Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable selectable text #7247

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 17 additions & 41 deletions ios/MullvadVPN/Views/InfoHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,23 @@ class InfoHeaderView: UIView, UITextViewDelegate {
/// Event handler invoked when user taps on the link.
var onAbout: (() -> Void)?

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

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

super.init(frame: .zero)

textView.backgroundColor = .clear
textView.dataDetectorTypes = .link
textView.isSelectable = true
textView.isEditable = false
textView.isScrollEnabled = false
textView.contentInset = .zero
textView.textContainerInset = .zero
textView.attributedText = makeAttributedString()
textView.linkTextAttributes = defaultLinkAttributes
textView.textContainer.lineFragmentPadding = 0
textView.delegate = self
infoLabel.backgroundColor = .clear
infoLabel.attributedText = makeAttributedString()
infoLabel.numberOfLines = 0
infoLabel.accessibilityTraits = .link

directionalLayoutMargins = .zero

addSubviews()
addTapGestureRecognizer()
}

required init?(coder: NSCoder) {
Expand All @@ -49,21 +43,19 @@ class InfoHeaderView: UIView, UITextViewDelegate {
]

private let defaultLinkAttributes: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 13),
.font: UIFont.boldSystemFont(ofSize: 13),
.foregroundColor: UIColor.ContentHeading.linkColor,
.attachment: "#",
]

private func makeAttributedString() -> NSAttributedString {
var linkAttributes = defaultLinkAttributes
linkAttributes[.link] = "#"

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: linkAttributes))
attributedString.append(NSAttributedString(string: config.link, attributes: defaultLinkAttributes))
attributedString.addAttribute(
.paragraphStyle,
value: paragraphStyle,
Expand All @@ -73,34 +65,18 @@ class InfoHeaderView: UIView, UITextViewDelegate {
}

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

func textView(
_ textView: UITextView,
shouldInteractWith URL: URL,
in characterRange: NSRange,
interaction: UITextItemInteraction
) -> Bool {
onAbout?()
return false
private func addTapGestureRecognizer() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTextViewTap))
addGestureRecognizer(tapGesture)
}

@available(iOS 17.0, *)
func textView(_ textView: UITextView, menuConfigurationFor textItem: UITextItem, defaultMenu: UIMenu) -> UITextItem
.MenuConfiguration? {
return nil
}

@available(iOS 17.0, *)
func textView(_ textView: UITextView, primaryActionFor textItem: UITextItem, defaultAction: UIAction) -> UIAction? {
if case .link = textItem.content {
return UIAction { [weak self] _ in
self?.onAbout?()
}
}
return nil
@objc
private func handleTextViewTap() {
onAbout?()
}
}
Loading