Zuhaib Rasheed
Zuhaib Rasheed

Reputation: 111

Append Button at the end of Multi-Line Text

I have a multiline text with info button at the end of the text which is done with HStack at the moment.

          HStack(spacing: 15) {
                Image(uiImage: "questionMark")
                    .resizable()
                    .scaledToFit()
                    .frame(width: 15, height: 15)

                Text("Neque porro quisquam est qui dolorem ipsum quia dolor sit amet.")
                    .fixedSize(horizontal: false, vertical: true)
          }

which looks like this:

enter image description here

Goal:

But I want to have the button at the end of the last text like infoIcon should appear right after ipsum.

enter image description here

Note: What could be the best solution to achieve this and would like not to use UIKit in this regards?

Upvotes: 1

Views: 1527

Answers (1)

Raja Kishan
Raja Kishan

Reputation: 18964

The possible solution is to add use Image constructor of Text but you can not add action only the image. Like this

struct ContentView: View {
    var body: some View {
        fullImageText
            .onTapGesture {

            }
    }
    
    var fullImageText: Text {
        Text("Neque porro quisquam est qui dolorem ipsum quia dolor sit amet.") + Text(Image("edit"))
    }
}

enter image description here

Another solution is to use NSMutableAttributedString wrap with UIViewRepresentable. Here is demo : https://stackoverflow.com/a/62169577/14733292

Upvotes: 3

Related Questions