15

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

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

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

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

4

10 回答 10

42

为@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)
    }
}
于 2016-01-28T16:27:51.900 回答
9

在界面生成器中执行此操作

  1. 点击按钮/标签
  2. 在属性检查器中,将标题/文本从普通更改为属性
  3. 选择要显示的文本。
  4. 右键单击它转到字体->下划线
于 2015-10-13T08:13:42.847 回答
7

斯威夫特 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;
}
于 2014-05-17T06:32:45.473 回答
5

从 iOS 6.0 开始,您可以NSAttributedString使用UIButton.

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

//use the setAttributedTitle method
[yourButton setAttributedTitle:titleString forState:UIControlStateNormal];
于 2014-11-20T13:59:16.780 回答
3

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

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

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

祝你好运,编码愉快!

于 2010-04-15T13:38:19.587 回答
2

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

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

于 2010-04-15T13:46:10.123 回答
1

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

于 2011-11-03T08:16:54.897 回答
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)

在此处输入图像描述

于 2018-04-26T08:12:29.690 回答
0

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

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

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

很想知道其他人是怎么做的......

于 2010-05-29T12:02:08.160 回答
-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)
        }
    }
}
于 2019-11-30T16:48:56.223 回答