You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何禁用SwiftUI中Text组件的邮箱地址自动识别功能?

SwiftUI 默认会对 Text 组件内的可识别数据(邮箱、网址、手机号等)自动添加链接样式和点击跳转能力,要让邮箱文本和普通文本展示、交互完全一致,可使用以下两种方案:

  • 方案一:快速覆盖样式+拦截跳转,代码改动量最小
struct ContentView: View {
    var body: some View {
        Text("hello@gmail.com")
            .foregroundColor(.primary) // 系统默认普通文本颜色,可自行替换为需要的颜色
            .underline(false) // 强制取消下划线
            .environment(\.openURL, OpenURLAction { _ in
                return .discarded // 拦截所有URL跳转请求,不执行跳转
            })
            // 如果文本不需要任何点击交互,可额外加上下面这行彻底禁用点击
            // .allowsHitTesting(false)
            .padding()
    }
}
  • 方案二:使用富文本从根源禁用链接识别,适配性更强
struct ContentView: View {
    // 手动构造富文本,明确移除链接属性
    let plainEmail: AttributedString = {
        var attrStr = AttributedString("hello@gmail.com")
        attrStr.link = nil // 清除自动识别的链接属性
        // 配置和普通文本完全一致的样式
        attrStr.foregroundColor = .primary
        attrStr.underlineStyle = .none
        return attrStr
    }()
    
    var body: some View {
        Text(plainEmail)
            .padding()
    }
}

内容的提问来源于stack exchange,提问作者Patrick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 00:36:08