You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 09:19:58