SwiftUI中如何为指定文本范围添加动态Lottie背景?
SwiftUI动态为文本关键词添加Lottie背景问题
我有一段包含加粗关键词的随机文本,希望为文本中的关键词添加特定的Lottie背景,但关键词位置不固定,需要实现动态处理。
尝试拆分文本并为指定部分添加背景时,始终报错:Cannot convert value of type 'some View' to expected argument type 'Text'。
相关代码如下:
@TextBuilder func textBuilder() -> Text { Text(originalText.prefix(upTo: originalText.range(of: boldText)?.lowerBound ?? "".startIndex)) Text(boldText) .foregroundColor(.blue) .background( LottieView(animationName: "animation", loopMode: .playOnce, contentMode: .scaleAspectFit) .frame(height: 10) .offset(CGSize(width: 0.0, height: 12.0)) ) Text(originalText.suffix(from: originalText.range(of: boldText)!.upperBound)) }
更新
已找到一个可行方案,但该方案下文本无法居中。
内容的提问来源于stack exchange,提问作者Mary B
相关产品推荐
相关产品推荐

