Files
swiftGrammar/AIGrammar/GrammarSubView/RichText.swift
2024-08-12 10:49:20 +08:00

204 lines
6.1 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// RichText.swift
// AIGrammar
//
// Created by oscar on 2024/4/3.
//
import SwiftUI
struct RichText: View {
var text1: AttributedString {
var text = AttributedString(localized:"登录即表示同意")
text.foregroundColor = .gray
return text
}
var text2: AttributedString {
var text = AttributedString(localized:"用户协议")
text.link = URL(string: "111")
text.foregroundColor = .red
return text
}
var text3: AttributedString {
var text = AttributedString(localized:"")
text.foregroundColor = .gray
return text
}
var text4: AttributedString {
var text = AttributedString(localized:"隐私协议")
text.link = URL(string: "222")
text.foregroundColor = .red
return text
}
var text: AttributedString {
text1 + text2 + text3 + text4
}
var body: some View {
VStack {
Text(text)
.environment(\.openURL, OpenURLAction { url in
let path = url.absoluteString
if path.hasPrefix("111") {
print("111...")
} else if path.hasPrefix("222") {
print("222...")
}
return .handled
})
Text("Device ID: \(globalEnvironment.deviceID)")
}
}
}
/*
import UIKit
import SwiftUI
class RichTextViewController: UIViewController, UITextViewDelegate {
var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// UITextView
textView = UITextView(frame: self.view.bounds)
textView.delegate = self
//
textView.isEditable = false
textView.isSelectable = true
//
let attributedString = NSMutableAttributedString(string: "")
//
let linkAttributes: [NSAttributedString.Key: Any] = [
.link: URL(string: "http://baidu.com")!, // 使URL scheme
.foregroundColor: UIColor.blue
]
attributedString.setAttributes(linkAttributes, range: NSRange(location: 0, length: 4)) // ""
textView.attributedText = attributedString
// UITextView
self.view.addSubview(textView)
}
//
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
print("")
print(URL.scheme as Any)
if URL.scheme == "http" {
//
return false // falseURL
}
return true
}
}
struct RichTextView: UIViewRepresentable {
func makeUIView(context: Context) -> UITextView {
// RichTextViewController textView
// textView 使
let controller = RichTextViewController()
controller.loadViewIfNeeded() // textView
return controller.textView
}
func updateUIView(_ uiView: UITextView, context: Context) {
// UI
}
}
struct RichText: View {
var body: some View {
VStack{
Text(" ")
// 使 RichTextView
RichTextView()
.frame(maxHeight: .infinity) //
//.edgesIgnoringSafeArea(.all) //
}
}
}
*/
/*
import SwiftUI
import UIKit
struct AttributedText: UIViewRepresentable {
var attributedString: NSAttributedString
func makeUIView(context: Context) -> UILabel {
let label = UILabel()
label.numberOfLines = 0 //
label.attributedText = attributedString
//
let tapGesture = UITapGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.labelTapped(_:)))
label.addGestureRecognizer(tapGesture)
label.isUserInteractionEnabled = true
return label
}
func updateUIView(_ uiView: UILabel, context: Context) {
//
uiView.attributedText = attributedString
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject {
var parent: AttributedText
init(_ parent: AttributedText) {
self.parent = parent
}
@objc func labelTapped(_ sender: UITapGestureRecognizer) {
//
// 使UILabel
print("Label was tapped")
}
}
}
struct RichText: View {
var body: some View {
// 使AttributedText
AttributedText(attributedString: attributedString)
}
var attributedString: NSAttributedString {
let fullString = NSMutableAttributedString(string: "Tap on ")
let clickablePart = NSAttributedString(string: "this text", attributes: [
.foregroundColor: UIColor.blue,
.underlineStyle: NSUnderlineStyle.single.rawValue
])
fullString.append(clickablePart)
fullString.append(NSAttributedString(string: " to see action."))
return fullString
}
}
*/
#Preview {
RichText()
}