首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使下划线使用Swift?

如何使下划线使用Swift?
EN

Stack Overflow用户
提问于 2022-05-10 04:28:14
回答 1查看 115关注 0票数 -1

样本文本图像

我想画像画一样的下划线。但是"NSUnderlineStyleAttributeName“不起作用,请帮我

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-10 06:24:14

您可以使用NSAttributedString来完成这个任务。

例子:

代码语言:javascript
运行
复制
 let underlineAttribute = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.thick.rawValue]
 let underlineAttributedString = NSAttributedString(string: "StringWithUnderLine", attributes: underlineAttribute)
 YourLabel.attributedText = underlineAttributedString

扩展

代码语言:javascript
运行
复制
extension UILabel {
func underline() {
    if let textString = self.text {
      let attributedString = NSMutableAttributedString(string: textString)
        attributedString.addAttribute(NSAttributedString.Key.underlineStyle,
                                      value: NSUnderlineStyle.single.rawValue,
                                      range: NSRange(location: 0, length: attributedString.length))
      attributedText = attributedString
    }
}
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72180767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档