英文:
SwiftUI - Multi line text followed by button
问题
HStack {
Text("非常长的文本 非常长的文本 非常长的文本 非常长的文本 非常长的文本")
Button {
} label: {
Text("点击这里")
Image(uiImage: UIImage.add)
}
}
按钮应该是唯一可点击的,带有点击效果。
英文:
How can we design multiline text followed by button. I tried with the following snippet, text and button are placed horizontal.
HStack {
Text("Very long text Very long text Very long text Very long text Very long text")
Button {
} label: {
Text("Click here")
Image(uiImage: UIImage.add)
}
}
Only Button should be clickable with click effect.
答案1
得分: 1
这可以通过将更改后的label
提供给Button
来实现,如下所示。
代码:
var body: some View {
VStack {
Button(action: {
let tosURL = URL.init(string: "https://www.google.com")! // 在此添加您的链接
if UIApplication.shared.canOpenURL(tosURL) {
UIApplication.shared.open(tosURL)
}
}, label: {
(Text("非常长的文本 非常长的文本 非常长的文本 非常长的文本 非常长的文本 gte wkjeh ") +
Text("点击这里")
.foregroundColor(.blue)
.underline()
)
.frame(maxWidth: .infinity, alignment: .leading)
.font(Font.system(size: 14, weight: .medium))
.foregroundColor(Color.black)
.fixedSize(horizontal: false, vertical: true)
})
.padding([.horizontal], 20)
}
.padding()
}
结果:
英文:
This can be achieved by providing altered label
to Button
, as shown bellow.
Code:
var body: some View {
VStack {
Button(action: {
let tosURL = URL.init(string: "https://www.google.com")! // add your link here
if UIApplication.shared.canOpenURL(tosURL) {
UIApplication.shared.open(tosURL)
}
}, label: {
(Text("Very long text Very long text Very long text Very long text Very long text gte wkjeh ") +
Text("Click here")
.foregroundColor(.blue)
.underline()
)
.frame(maxWidth: .infinity, alignment: .leading)
.font(Font.system(size: 14, weight: .medium))
.foregroundColor(Color.black)
.fixedSize(horizontal: false, vertical: true)
})
.padding([.horizontal], 20)
}
.padding()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论