Swift中仅指定文字显示超链接却整段下划线的问题排查
iOS富文本超链接下划线问题解决
问题场景
需要仅让特定文字(条款、隐私政策相关内容)显示超链接下划线,但当前整段文本都带了下划线。
原代码与文本
原HTML文本字符串:
"En passant et en réglant une commande <a href=\"https://www.subway.com/fr-CA/Legal/order_apptermsofuse\">, vous acceptez les modalités de la politique de confidentialité </a>et <a href=\"https://www.subway.com/fr-CA/Legal/PrivacyStatement-FWH\">relatives à l\'application et à la commande.</a>."
原Swift处理代码:
let htmlData = NSString(string: descriptionText).data(using: String.Encoding.unicode.rawValue) let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html] if let attributedString = try? NSMutableAttributedString(data: htmlData ?? Data(), options: options, documentAttributes: nil) { let style = NSMutableParagraphStyle() style.alignment = .left style.lineSpacing = Constants.General.lineSpacingVal let termsFullRange = NSMakeRange(0, attributedString.length) attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.sihLegalCopyColor, range: termsFullRange) attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.init(font: .regular, size: 12), range: termsFullRange) attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: termsFullRange) termsAndCondText.linkTextAttributes = [.foregroundColor: UIColor.sihLegalCopyColor] termsAndCondText.attributedText = attributedString }
问题原因
- HTML标签包裹错误:不需要加链接的文本
vous acceptez les modalités de la politique de confidentialité被放到了<a>标签内部,导致这段文本被识别为链接一部分,自动带上下划线。 - 下划线样式未配置:
linkTextAttributes只设置了文字颜色,未明确指定下划线显示规则,且HTML转富文本时默认会给链接添加下划线。
解决方案
第一步:修正HTML文本,仅包裹需要链接的文字
把非链接文本移出<a>标签,只保留需要跳转的关键词在标签内,修正后的文本如下:
"En passant et en réglant une commande, vous acceptez les modalités de la <a href=\"https://www.subway.com/fr-CA/Legal/order_apptermsofuse\">politique de confidentialité</a> et <a href=\"https://www.subway.com/fr-CA/Legal/PrivacyStatement-FWH\">conditions relatives à l\'application et à la commande</a>."
第二步:修正Swift代码,控制下划线样式
在设置linkTextAttributes时明确添加下划线属性,同时移除富文本中全局的下划线:
let htmlData = NSString(string: descriptionText).data(using: String.Encoding.unicode.rawValue) let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html] if let attributedString = try? NSMutableAttributedString(data: htmlData ?? Data(), options: options, documentAttributes: nil) { let style = NSMutableParagraphStyle() style.alignment = .left style.lineSpacing = Constants.General.lineSpacingVal let termsFullRange = NSMakeRange(0, attributedString.length) // 移除全局可能存在的下划线属性 attributedString.removeAttribute(.underlineStyle, range: termsFullRange) attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.sihLegalCopyColor, range: termsFullRange) attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.init(font: .regular, size: 12), range: termsFullRange) attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: termsFullRange) // 仅给链接设置下划线和颜色 termsAndCondText.linkTextAttributes = [ .foregroundColor: UIColor.sihLegalCopyColor, .underlineStyle: NSUnderlineStyle.single.rawValue ] termsAndCondText.attributedText = attributedString }
说明
- 修正HTML标签后,只有被
<a>包裹的文字会被识别为链接 - 通过
removeAttribute(.underlineStyle, range: termsFullRange)移除全局下划线,避免非链接文本被错误添加下划线 - 在
linkTextAttributes中明确设置下划线样式,确保链接文字显示下划线
内容的提问来源于stack exchange,提问作者ios
相关产品推荐
相关产品推荐

