我想在我的视图中显示邮件 ID,以便单击邮件 ID 应该打开邮件编写器视图。
我想将按钮文本显示为下划线,以显示它是一个超链接,并让按钮单击事件调用邮件编写器视图。目前,我无法显示带下划线的按钮文本。
我想在按钮上方放置一个标签并调整标签属性以获取带下划线的文本,但这不起作用。
有没有不同的方法来获得我想要的外观和行为?
我想在我的视图中显示邮件 ID,以便单击邮件 ID 应该打开邮件编写器视图。
我想将按钮文本显示为下划线,以显示它是一个超链接,并让按钮单击事件调用邮件编写器视图。目前,我无法显示带下划线的按钮文本。
我想在按钮上方放置一个标签并调整标签属性以获取带下划线的文本,但这不起作用。
有没有不同的方法来获得我想要的外观和行为?
为@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)
}
}
在界面生成器中执行此操作
斯威夫特 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;
}
从 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];
要做你想做的事,你需要使用 CGContextStrokePath() 在 UILabel 的 CGLayer 上绘制下划线。
话虽如此,我会挑战你重新考虑你的设计选择。使按钮文本看起来是一个超链接对于 Windows 桌面应用程序(例如使用 )是有意义的LinkLabel
。然而,iPhone OS 有一套不同的用户界面约定,因为它的屏幕更小,并且需要更大的目标来适应触摸输入。
解决此问题的“Cocoa Touch”方法是使用 UITableView 显示邮件 ID 列表。为每一行提供一个适当的附件按钮。例如,您可以使用 a UITableViewCellAccessoryDetailDisclosureButton
或UIButton
with buttonType
of UIButtonTypeContactAdd
)。然后,附件按钮事件处理程序将调出邮件编写器。
祝你好运,编码愉快!
另一种选择是为按钮设置图像。创建一个显示适当文本的图像,按钮将显示该文本而不是文本。
我同意 Zack 的观点,即下划线文本是多余的并且违反了界面语法。您首先为链接的文本添加下划线,以表明它们具有类似按钮的行为。如果文本已经在按钮中,则无需为它加下划线以表明它的行为类似于按钮,因为用户已经期望响应单击它的操作。
很简单,使用 webview 并插入一个 html 块来显示任何 iphone 无法显示但 html 可以显示的文本。
感谢@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)
那么,如果您想在一段文本中提供指向网站的链接怎么办?用户需要一些视觉指示文本是一个链接并将它们带到某个地方。
蓝色带下划线的超链接外观是预期的规范,但请理解这是 PC 方式。那么什么是 iPhone 方式呢?
我过去使用过没有背景等和蓝色文本的自定义按钮,虽然没有下划线,但它确实允许可点击的文本。
很想知道其他人是怎么做的......
或者您可以创建 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)
}
}
}