iPhone:如何在按钮中显示带下划线的文本?

时间:2010-04-15 12:59:28

标签: iphone uibutton uilabel hyperlink mfmailcomposeviewcontroller

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

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

我想在标签上方放置一个标签并调整标签属性以获得带下划线的文字,但这不起作用。

是否有不同的方式来获得我想要的外观和行为?

10 个答案:

答案 0 :(得分:36)

为@ Haider的答案添加屏幕截图。

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

enter image description here

无论如何,我最终在代码中强调了它,因为由于某种原因,我无法 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)
    }
}

答案 1 :(得分:8)

在界面构建器中执行此操作

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

答案 2 :(得分:6)

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;
}

答案 3 :(得分:4)

自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];

答案 4 :(得分:3)

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

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

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

祝你好运,编码愉快!

答案 5 :(得分:3)

另一种方法是为按钮设置图像。创建一个显示相应文本的图像,按钮将显示该图像而不是文本。

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

答案 6 :(得分:1)

简单,使用webview并插入一个html块来显示除了html之外iphone无法执行的任何文本。

答案 7 :(得分:1)

感谢@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)

enter image description here

答案 8 :(得分:0)

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

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

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

有兴趣知道其他人做了什么......

答案 9 :(得分:-1)

或者您可以创建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)
        }
    }
}