【问题标题】:How to display highlighted words using swift ui如何使用 swift ui 显示突出显示的单词
【发布时间】:2021-08-23 07:47:04
【问题描述】:

var str:String = "当我 ###young$$$ 的时候,我认为 ###time$$$ 就是金钱" VStsck{ 文本(字符串) } enter image description here

【问题讨论】:

标签: swiftui


【解决方案1】:

您可以使用正则表达式解析字符串并根据找到的范围构建Text

struct HighlightedText: View{
    let text: Text
    
    private static let regularExpression = try! NSRegularExpression(
        pattern: "###(?<content>((?!\\$\\$\\$).)*)\\$\\$\\$"
    )
    
    private struct SubstringRange {
        let content: NSRange
        let full: NSRange
    }
    
    init(_ string: String) {
        let ranges = Self.regularExpression
            .matches(
                in: string,
                options: [],
                range: NSRange(location: 0, length: string.count)
            )
            .map { match in
                SubstringRange(
                    content: match.range(withName: "content"),
                    full: match.range(at: 0)
                )
            }
        var nextNotProcessedSymbol = 0
        var text = Text("")
        let nsString = string as NSString
        func appendSubstringStartingNextIfNeeded(until endLocation: Int) {
            if nextNotProcessedSymbol < endLocation {
                text = text + Text(nsString.substring(
                    with: NSRange(
                        location: nextNotProcessedSymbol,
                        length: endLocation - nextNotProcessedSymbol
                    )
                ))
            }
        }
        for range in ranges {
            appendSubstringStartingNextIfNeeded(until: range.full.location)
            text = text + Text(nsString.substring(with: range.content))
                .foregroundColor(Color.red)
            nextNotProcessedSymbol = range.full.upperBound
        }
        appendSubstringStartingNextIfNeeded(until: string.count)
        self.text = text
    }
    
    var body: some View {
        text
    }
}

用法:

HighlightedText("When I was ###young$$$, I thought ###time$$$ was money")

【讨论】:

    【解决方案2】:
    func hilightText(str:String) -> Text{
       var resultText:Text = Text("")
       if(str.contains("###")){
            let titleArr:Array<String> = str.components(spearatedBy:"###")
            for(title in titleArr){
                resultText = resultText 
                + Text(title.components(spearatedBy:"$$$")[0]).foregroundColor(Color.red)
                + Text(title.components(spearatedBy:"$$$")[1]).foregroundColor(Color.gray)
            }else{
                resultText = resultText + Text(title).foregroundColor(Color.gray)
            }
       }
       return resultText 
    }else{
       return Text(str).foregroundColor(Color.gray)
    }
    
    struct View1:View{
         @State var title = "When I was ###young$$$, I thought ###time$$$ was money"
         var body : some View{
             hilightText(str:title)
         }
    }
    

    【讨论】:

    • 我找到了一个简单的解决方案,虽然看起来并不优雅
    猜你喜欢
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 2010-09-12
    • 2015-11-13
    相关资源
    最近更新 更多