【问题标题】:Adding underline attribute to partial text UILabel in storyboard为情节提要中的部分文本 UILabel 添加下划线属性
【发布时间】:2015-03-31 19:51:54
【问题描述】:

如何仅使用情节提要为UILabel 的部分文本添加下划线?我可以在代码中做到这一点,我可以在标签的整个文本下划线,但不仅仅是字符串中的一两个单词。

【问题讨论】:

    标签: ios text attributes storyboard uilabel


    【解决方案1】:
    1. 选择UILabel 并转到属性检查器部分。
      将文本值从 plain 更改为 Attributed

    2. 选择您想要下划线的特定文本部分。

      注意:如果您希望全文为下划线,请选择全文。

    3. 现在右键单击并将字体更改为下划线

    它将下划线文本

    【讨论】:

    • 非常有用的感谢 - 奇怪的是 Apple 如何隐藏所有这些功能!
    • 此答案适用于 iPhone 5s,但在 6 和 6 plus 设备中不显示下划线。有没有其他办法
    • 救了我的命,花了一个多小时搜索它
    • 找这个很久了,谢谢楼主。
    • 如此简单的解决方案!谢谢@RizwanShaikh???‍??
    【解决方案2】:

    步骤:-

    1. 转到 TextEdit 并创建带有下划线的 UILabel 文本。
    2. 将 UILabel 文本更改为 Attributed(Attributed Selector)。
    3. 复制带下划线的文本并分配给 UILabel。

    【讨论】:

      【解决方案3】:

      通过代码:

          func underLineText(text: String)-> NSMutableAttributedString
                 let attributedText = NSMutableAttributedString(string: text)
           attributedText.addAttribute(NSMutableAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: attributedText.length))
                return attributedText
              }
      

      //func的调用

         yourLbl.attributedText = underLineText(text:yourLbl.text!)
      

      其他下划线类型列表

       NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.single.rawValue
       NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.thick.rawValue
       NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.double.rawValue
       NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.patternDot.rawValue
       NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.patternDash.rawValue
       NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.patternDashDot.rawValue
       NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.patternDashDotDot.rawValue
       NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.byWord.rawValue
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 2011-11-25
      • 2012-09-07
      • 1970-01-01
      相关资源
      最近更新 更多