我想找到一个单词在句子中的位置,以便为该单词设置一个框架。比如像这样:
目前是否可以在 SwiftUI 中执行此操作
我写了一些代码,这就是我得到的
import SwiftUI
struct ContentView: View {
@State var text = "I usually get ----- around nine o'clock every morning"
@State var rects = [CGRect.zero]
var body: some View {
ZStack {
TextView(text: $text, rects: $rects)
.overlay(
ForEach(0..<self.rects.count, id: \.self) { index in
RoundedRectangle(cornerRadius: 6)
.frame(width: self.rects[index].size.width, height: self.rects[index].size.height)
.position(self.rects[index].origin)
.foregroundColor(Color.red)
}
)
}
}
}
struct TextView: UIViewRepresentable {
@Binding var text: String
@Binding var rects: [CGRect]
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
textView.delegate = context.coordinator
textView.isEditable = true
textView.isUserInteractionEnabled = true
textView.font = UIFont.systemFont(ofSize: 24)
DispatchQueue.main.async {
let ranges = self.searchRanges(in: textView.text)
self.rects = self.viewRects(for: ranges, textView: textView)
}
return textView
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = text
}
class Coordinator : NSObject, UITextViewDelegate {
var parent: TextView
init(_ uiTextView: TextView) {
self.parent = uiTextView
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
return true
}
func textViewDidChange(_ textView: UITextView) {
self.parent.text = textView.text
}
}
func searchRanges(in text: String) -> [Range<String.Index>] {
var ranges = [Range<String.Index>]()
var searchRange = text.startIndex ..< text.endIndex
var range = text.range(
of: "-----",
options: .caseInsensitive,
range: searchRange,
locale: nil
)
while let findedRange = range {
ranges.append(findedRange)
searchRange = findedRange.upperBound ..< text.endIndex
range = text.range(
of: "-----",
options: .caseInsensitive,
range: searchRange,
locale: nil
)
}
return ranges
}
func viewRects(for rnges: [Range<String.Index>], textView: UITextView) -> [CGRect] {
var rects = [CGRect]()
for range in rnges {
let upperBound = range.upperBound.utf16Offset(in: textView.text)
let lowerBound = range.lowerBound.utf16Offset(in: textView.text)
let length = upperBound - lowerBound
if let start = textView.position(from: textView.beginningOfDocument, offset: lowerBound),
let end = textView.position(from: start, offset: length),
let txtRange = textView.textRange(from: start, to: end) {
var rect = textView.firstRect(for: txtRange)
rect.origin.x = rect.origin.x
rect.origin.y = rect.midY
rects.append(rect)
}
}
return rects
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
UIKit 上的相同代码与情节提要