在 SwiftUI 中突出显示文本的特定部分 [英] Highlight a specific part of the text in SwiftUI

查看:54
本文介绍了在 SwiftUI 中突出显示文本的特定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是 Swift 的新手,我正在将 SwiftUI 用于我的项目,我下载了一些天气数据并将其显示在 ContentView() 中.

如果文本中包含某些特定单词,我想突出显示它的某些部分,但我不知道如何开始.

在 ContentView() 中,我尝试设置一个函数来接收从网络下载的字符串并返回一个字符串.我认为这是错误的,因为 SwiftUI 根本没有为文本应用修饰符.

例如,在我的 ContentView() 中,我希望单词 Thunderstorm 具有 .bold() 修饰符:

struct ContentView: 查看 {let testo : String = "该地区有雷雨"var主体:一些视图{文本(突出显示(字符串:testo))}func highlight(str: String) ->细绳 {让 textToSearch = "雷暴"var 结果 = ""如果 str.contains(textToSearch) {让 index = str.startIndex结果 = String( str[index])}返回结果}}

解决方案

iOS 13, Swift 5. 在这篇中等文章中描述了一个通用的解决方案.使用它您可以在任何地方突出显示任何文本,唯一的问题是它的长度不能超过 64 个字符,因为它使用位掩码.

这是文章中的基本代码.

ForEach((0 ..

这个是用来掩盖文字的...

func colorCode(gate:Int, no:Int) ->布尔{让 bgr = String(gate, radix:2).pad(with: "0", toLength: 16)let bcr = String(no, radix:2).pad(with: "0", toLength: 16)让 binaryColumn = 1 <<没有 - 1让值 = UInt64(gate) &UInt64(binaryColumn)让 vr = String(value, radix:2).pad(with: "0", toLength: 16)打印(bg",bgr,bc",bcr,vr)返回值0 ?真假}

Hello I'm new to Swift and am using SwiftUI for my project where I download some weather data and I display it in the ContentView().

I would like to highlight some part of the Text if it contains some specific word, but I don't have any idea how to start.

In ContentView(), I have tried to set a function receiving the string downloaded from web and return a string. I believe this is wrong, because SwiftUI does not apply the modifiers at the all for the Text.

For example, in my ContentView() I would like the word thunderstorm to have the .bold() modifier:

struct ContentView: View {
  let testo : String = "There is a thunderstorm in the area"

  var body: some View {
    Text(highlight(str: testo))
  }

  func highlight(str: String) -> String {
    let textToSearch = "thunderstorm"
    var result = ""

    if str.contains(textToSearch) {
      let index = str.startIndex
      result = String( str[index])
    }

    return result
  }

}

解决方案

iOS 13, Swift 5. There is a generic solution described in this medium article. Using it you can highlight any text anywhere with the only catch being it cannot be more then 64 characters in length, since it using bitwise masks.

https://medium.com/@marklucking/an-interesting-challenge-with-swiftui-9ebb26e77376

This is the basic code in the article.

ForEach((0 ..< letter.count), id: \.self) { column in
      Text(letter[column])
        .foregroundColor(colorCode(gate: Int(self.gate), no: column) ? Color.black: Color.red)
        .font(Fonts.futuraCondensedMedium(size: fontSize))

    }

And this one to mask the text...

func colorCode(gate:Int, no:Int) -> Bool {

 let bgr = String(gate, radix:2).pad(with: "0", toLength: 16)
 let bcr = String(no, radix:2).pad(with: "0", toLength: 16)
 let binaryColumn = 1 << no - 1

 let value = UInt64(gate) & UInt64(binaryColumn)
 let vr = String(value, radix:2).pad(with: "0", toLength: 16)

 print("bg ",bgr," bc ",bcr,vr)
 return value > 0 ? true:false
}

这篇关于在 SwiftUI 中突出显示文本的特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆