【问题标题】:How to display Underlined Text in a Button on iOS?如何在 iOS 上的按钮中显示带下划线的文本?
【发布时间】:2011-02-08 09:05:12
【问题描述】:

我想在我的视图中显示邮件 ID,以便单击邮件 ID 会打开邮件编写器视图。

我想将按钮文本显示为下划线,以表明它是一个超链接,并且按钮单击事件可以调用邮件编写器视图。目前,我无法显示带下划线的按​​钮文本。

我想在按钮上方放置一个标签并调整标签属性以获取带下划线的文本,但没有奏效。

有没有不同的方法来获得我想要的外观和行为?

【问题讨论】:

标签: ios hyperlink uibutton uilabel mfmailcomposeviewcontroller


【解决方案1】:

要做你想做的,你需要使用 CGContextStrokePath() 在 UILabel 的 CGLayer 上绘制下划线。

话虽如此,我会挑战你重新考虑你的设计选择。使按钮文本看起来是一个超链接对于 Windows 桌面应用程序来说是有意义的(例如,使用 LinkLabel)。然而,iPhone OS 有一套不同的用户界面约定,因为它的屏幕更小,并且需要更大的目标来适应触摸输入。

解决此问题的“Cocoa Touch”方法是使用 UITableView 显示邮件 ID 列表。为每一行提供一个适当的附件按钮。例如,您可以使用UITableViewCellAccessoryDetailDisclosureButtonUIButtonbuttonTypeUIButtonTypeContactAdd)。然后,附件按钮事件处理程序将调出邮件编写器。

祝你好运,编码愉快!

【讨论】:

    【解决方案2】:

    另一种选择是为按钮设置图像。创建一个显示适当文本的图像,按钮将显示该文本而不是文本。

    我同意 Zack 的观点,即下划线文本是多余的并且违反了界面语法。您首先为链接的文本添加下划线,以表明它们具有类似按钮的行为。如果文本已经在按钮中,则无需为它加下划线以表明它的行为类似于按钮,因为用户已经期望响应单击它的操作。

    【讨论】:

    • 如果你有翻译,@TechZen,你必须为每种语言准备图像?
    【解决方案3】:

    那么,如果您想在一段文本中提供指向网站的链接怎么办?用户需要一些视觉指示文本是一个链接并将它们带到某个地方。

    带下划线的蓝色超链接外观是预期的标准,但请理解这是 PC 方式。那么 iPhone 的方式是什么?

    我过去使用过没有背景等和蓝色文本的自定义按钮,虽然没有下划线,但它确实允许点击文本。

    很想知道其他人在做什么......

    【讨论】:

      【解决方案4】:

      简单,使用 webview 并插入一个 html 块来显示任何 iphone 无法显示但 html 可以显示的文本。

      【讨论】:

      • 简单就是下划线 = YES。怎么这么简单?
      【解决方案5】:

      Swift 3.2

      if let title = button.titleLabel?.text, title != "" {
      
          let attributeString = NSMutableAttributedString(string: title)
      
          attributeString.addAttribute(NSAttributedStringKey.underlineStyle, value: 1, range: NSMakeRange(0, title.count))
      
          button.titleLabel?.attributedText = attributeString
      }
      

      目标 C

      NSString *tem = self.myButton.titleLabel.text;
      
      if (tem != nil && ![tem isEqualToString:@""]) {
          NSMutableAttributedString *temString=[[NSMutableAttributedString alloc]initWithString:tem];
          [temString addAttribute:NSUnderlineStyleAttributeName
                            value:[NSNumber numberWithInt:1]
                            range:(NSRange){0,[temString length]}];
      
          self.myButton.titleLabel.attributedText = temString;
      }
      

      【讨论】:

        【解决方案6】:

        从 iOS 6.0 开始,您可以使用 NSAttributedStringUIButton

        //create an underlined attributed string
        NSAttributedString *titleString = [[NSAttributedString alloc] initWithString:@"Title" attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle | NSUnderlinePatternSolid)}];
        
        //use the setAttributedTitle method
        [yourButton setAttributedTitle:titleString forState:UIControlStateNormal];
        

        【讨论】:

          【解决方案7】:

          在界面生成器中进行

          1. 点击按钮/标签
          2. 在属性检查器中,将 Title/Text 从普通更改为 Attributed
          3. 选择要显示的文本。
          4. 右击转到字体->下划线

          【讨论】:

            【解决方案8】:

            为@Haider 的回答添加截图。

            注意:只有您突出显示的文本会加下划线,因此您可以根据需要仅在特定范围内加下划线。

            无论如何,我最终还是在代码中为它加了下划线,只是因为出于某种原因我无法让 Bold 与 Underline 一起工作。可能是因为我使用的字体,谁知道呢。

            @IBOutlet weak var underlineButton: UIButton! {
                didSet {
                    let attrs = [
                        NSFontAttributeName : UIFont(name: "MyCustomFont-Bold", size: 19.0)!,
                        NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue,
                        NSForegroundColorAttributeName : UIColor.orangeColor()
                    ]
                    let attrString = NSMutableAttributedString(string: "Button text", attributes:attrs)
                    underlineButton.setAttributedTitle(attrString, forState: .Normal)
                }
            }
            

            【讨论】:

              【解决方案9】:

              感谢@Robert Chen 的回答,更新到 swift 4.1

              let btn = UIButton(type: .system)
              btn.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
              
              let attrs = NSAttributedString(string: "➟ More",
                             attributes:
              [NSAttributedStringKey.foregroundColor: UIColor(red:0.20, green:1.00, blue:1.00, alpha:1.0),
               NSAttributedStringKey.font: UIFont.systemFont(ofSize: 19.0),
               NSAttributedStringKey.underlineColor: UIColor(red:0.20, green:1.00, blue:1.00, alpha:1.0),
               NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])
              
              btn.setAttributedTitle(attrs, for: .normal)
              

              【讨论】:

                【解决方案10】:

                或者您可以为 UIButton (Swift 5) 创建一个扩展:

                extension UIButton {
                    func underline() {
                        if let textUnwrapped = self.titleLabel?.text {
                            let underlineAttribute = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue]
                            let underlineAttributedString = NSAttributedString(string: textUnwrapped, attributes: underlineAttribute)
                            self.setAttributedTitle(underlineAttributedString, for: .normal)
                        }
                    }
                }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2011-08-19
                  • 2013-08-22
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-04-22
                  • 2015-10-21
                  • 2016-03-13
                  • 2015-12-14
                  相关资源
                  最近更新 更多