fix: server rules prompt footer text is selectable issue

This commit is contained in:
CMK 2021-08-04 14:59:42 +08:00
parent b5e8df92de
commit 0ee2dea581
1 changed files with 70 additions and 33 deletions

View File

@ -10,6 +10,7 @@ import UIKit
import Combine import Combine
import MastodonSDK import MastodonSDK
import SafariServices import SafariServices
import MetaTextKit
final class MastodonServerRulesViewController: UIViewController, NeedsDependency { final class MastodonServerRulesViewController: UIViewController, NeedsDependency {
@ -53,15 +54,21 @@ final class MastodonServerRulesViewController: UIViewController, NeedsDependency
return view return view
}() }()
private(set) lazy var bottomPromptTextView: UITextView = { private(set) lazy var bottomPromptMetaText: MetaText = {
let textView = UITextView() let metaText = MetaText()
textView.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 17, weight: .regular), maximumPointSize: 22) metaText.textAttributes = [
textView.textColor = .label .font: UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 17, weight: .regular), maximumPointSize: 22),
textView.isSelectable = true .foregroundColor: UIColor.label,
textView.isEditable = false ]
textView.isScrollEnabled = false metaText.linkAttributes = [
textView.backgroundColor = Asset.Theme.Mastodon.systemGroupedBackground.color .font: UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 17, weight: .regular), maximumPointSize: 22),
return textView .foregroundColor: Asset.Colors.brandBlue.color,
]
metaText.textView.isEditable = false
metaText.textView.isSelectable = false
metaText.textView.isScrollEnabled = false
metaText.textView.backgroundColor = Asset.Theme.Mastodon.systemGroupedBackground.color // needs background color to prevent server rules text overlap
return metaText
}() }()
let confirmButton: PrimaryActionButton = { let confirmButton: PrimaryActionButton = {
@ -114,13 +121,13 @@ extension MastodonServerRulesViewController {
confirmButton.heightAnchor.constraint(equalToConstant: MastodonServerRulesViewController.actionButtonHeight).priority(.defaultHigh), confirmButton.heightAnchor.constraint(equalToConstant: MastodonServerRulesViewController.actionButtonHeight).priority(.defaultHigh),
]) ])
bottomPromptTextView.translatesAutoresizingMaskIntoConstraints = false bottomPromptMetaText.textView.translatesAutoresizingMaskIntoConstraints = false
bottomContainerView.addSubview(bottomPromptTextView) bottomContainerView.addSubview(bottomPromptMetaText.textView)
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
bottomPromptTextView.frameLayoutGuide.topAnchor.constraint(equalTo: bottomContainerView.topAnchor, constant: 20), bottomPromptMetaText.textView.frameLayoutGuide.topAnchor.constraint(equalTo: bottomContainerView.topAnchor, constant: 20),
bottomPromptTextView.frameLayoutGuide.leadingAnchor.constraint(equalTo: bottomContainerView.readableContentGuide.leadingAnchor), bottomPromptMetaText.textView.frameLayoutGuide.leadingAnchor.constraint(equalTo: bottomContainerView.readableContentGuide.leadingAnchor),
bottomPromptTextView.frameLayoutGuide.trailingAnchor.constraint(equalTo: bottomContainerView.readableContentGuide.trailingAnchor), bottomPromptMetaText.textView.frameLayoutGuide.trailingAnchor.constraint(equalTo: bottomContainerView.readableContentGuide.trailingAnchor),
confirmButton.topAnchor.constraint(equalTo: bottomPromptTextView.frameLayoutGuide.bottomAnchor, constant: 20), confirmButton.topAnchor.constraint(equalTo: bottomPromptMetaText.textView.frameLayoutGuide.bottomAnchor, constant: 20),
]) ])
scrollView.translatesAutoresizingMaskIntoConstraints = false scrollView.translatesAutoresizingMaskIntoConstraints = false
@ -175,34 +182,64 @@ extension MastodonServerRulesViewController {
} }
func configTextView() { func configTextView() {
let str = NSString(string: L10n.Scene.ServerRules.prompt(viewModel.domain)) let metaContent = ServerRulesPromptMetaContent(domain: viewModel.domain)
let termsOfServiceRange = str.range(of: L10n.Scene.ServerRules.termsOfService) bottomPromptMetaText.configure(content: metaContent)
let privacyRange = str.range(of: L10n.Scene.ServerRules.privacyPolicy) bottomPromptMetaText.textView.linkDelegate = self
let attributeString = NSMutableAttributedString( }
string: L10n.Scene.ServerRules.prompt(viewModel.domain),
attributes: [ struct ServerRulesPromptMetaContent: MetaContent {
NSAttributedString.Key.font: UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 17, weight: .regular), maximumPointSize: 22), let string: String
NSAttributedString.Key.foregroundColor: UIColor.label let entities: [Meta.Entity]
]
) init(domain: String) {
attributeString.addAttribute(.link, value: Mastodon.API.serverRulesURL(domain: viewModel.domain), range: termsOfServiceRange) let _string = L10n.Scene.ServerRules.prompt(domain)
attributeString.addAttribute(.link, value: Mastodon.API.privacyURL(domain: viewModel.domain), range: privacyRange) self.string = _string
let linkAttributes = [NSAttributedString.Key.foregroundColor: Asset.Colors.brandBlue.color]
bottomPromptTextView.attributedText = attributeString var _entities: [Meta.Entity] = []
bottomPromptTextView.linkTextAttributes = linkAttributes
bottomPromptTextView.delegate = self let termsOfServiceText = L10n.Scene.ServerRules.termsOfService
if let termsOfServiceRange = _string.range(of: termsOfServiceText) {
let url = Mastodon.API.serverRulesURL(domain: domain)
let entity = Meta.Entity(range: NSRange(termsOfServiceRange, in: _string), meta: .url(termsOfServiceText, trimmed: termsOfServiceText, url: url.absoluteString, userInfo: nil))
_entities.append(entity)
}
let privacyPolicyText = L10n.Scene.ServerRules.privacyPolicy
if let privacyPolicyRange = _string.range(of: privacyPolicyText) {
let url = Mastodon.API.privacyURL(domain: domain)
let entity = Meta.Entity(range: NSRange(privacyPolicyRange, in: _string), meta: .url(privacyPolicyText, trimmed: privacyPolicyText, url: url.absoluteString, userInfo: nil))
_entities.append(entity)
}
self.entities = _entities
}
func metaAttachment(for entity: Meta.Entity) -> MetaAttachment? {
return nil
}
} }
} }
extension MastodonServerRulesViewController: UITextViewDelegate { extension MastodonServerRulesViewController: UITextViewDelegate {
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
let safariVC = SFSafariViewController(url: URL)
self.present(safariVC, animated: true, completion: nil)
return false return false
} }
} }
// MARK: - MetaTextViewDelegate
extension MastodonServerRulesViewController: MetaTextViewDelegate {
func metaTextView(_ metaTextView: MetaTextView, didSelectMeta meta: Meta) {
switch meta {
case .url(_, _, let url, _):
guard let url = URL(string: url) else { return }
coordinator.present(scene: .safari(url: url), from: nil, transition: .safariPresent(animated: true, completion: nil))
default:
break
}
}
}
extension MastodonServerRulesViewController { extension MastodonServerRulesViewController {
@objc private func confirmButtonPressed(_ sender: UIButton) { @objc private func confirmButtonPressed(_ sender: UIButton) {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)