1

I want to use multiple text formats in one Text view in SwiftUI. this would look like this:

i want to have multiple formats for text in the same text box at the same time even if it is really really really really really long text so it would need to wrap correctly. (and maybe even include links!)

I found this(How to bold text in TextField swiftUI?)  thread after a basic search, but it is not at all what I am looking for.

This would work similar to HTML's span

ios coder
  • 1
  • 4
  • 31
  • 91
a1cd
  • 17,884
  • 4
  • 8
  • 28

2 Answers2

5

With iOS 15, you can use markdown for Text:

Text("i want to have multiple **formats** for text in the _same **text box**_ at the same time even if it is really _really_ **really _really_** ~~really~~ long text so it would need to wrap correctly. (and maybe even include [links](https://www.apple.com)!)")

Result:

Result

George
  • 25,988
  • 10
  • 79
  • 133
  • 1
    NO WAY! You are a lifesaver! – a1cd Aug 20 '21 at 22:47
  • Came here looking for reasons why this technique might not work. Discovered that this doesn't work: Text("**Prompt:** " + prompt) This however does. Text("**Prompt:** \(prompt)") – user139816 Jun 05 '23 at 16:30
  • @user139816 Check out https://developer.apple.com/documentation/swiftui/text#Localizing-strings on how to do it without string interpolation – George Jun 08 '23 at 13:51
3

Here a simple example for you, with my example code you can code for iOS 13 or 14 as well:

struct ContentView: View {
    
    var body: some View {
        
        Text("Hello, World!") + Text(" Hello, World!").bold() + Text(" Hello, World!").italic()
        
    }
    
}

output:

enter image description here

ios coder
  • 1
  • 4
  • 31
  • 91
  • this was what i meant when i said "really really really long text so it would need to wrap correctly." this will not wrap correctly if it is really really long text – a1cd Aug 20 '21 at 22:47
  • 1
    @Evergreen: It is possible to use this way for "really really really long text", no issue there. But i do not care about which code you use in your project! the answer you accepted is just available in iOS 15, or Xcode 13 beta which it is fresh and new there is a big downside that your project target need iOS 15! We have to use my way in past for such a things. but as I said do what u think is best for project. – ios coder Aug 20 '21 at 22:59
  • @Evergreen: You are welcome, If you just releasing your app for iOS 15 or higher then use the other answer, if not then use what is available for us, like my example code. – ios coder Aug 20 '21 at 23:30