Skip to content

Commit

Permalink
Disable selectable text
Browse files Browse the repository at this point in the history
  • Loading branch information
mojganii committed Dec 3, 2024
1 parent 54302b4 commit 65e2c6f
Showing 1 changed file with 17 additions and 41 deletions.
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?()
}
}

0 comments on commit 65e2c6f

Please sign in to comment.