【问题标题】:Cocoa NSButton with underlined title带有下划线标题的 Cocoa NSButton
【发布时间】:2019-10-08 11:19:12
【问题描述】:

我正在尝试创建一个带有下划线标题的 NSButton。

@IBOutlet var myButton: NSButton!
let underlineAttribute = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single]
let underlineAttributedString = NSAttributedString(string: "Button Title", attributes: underlineAttribute)
myButton.attributedTitle = underlineAttributedString

但我得到了错误:

<NSATSTypesetter: 0x600003719500>: Exception -[__SwiftValue _getValue:forType:]: unrecognized selector sent to instance 0x600000ca4060 raised during typesetting layout manager <NSLayoutManager: 0x100e1a140>
    1 containers, text backing has 5 characters
    selected character range {0, 0} affinity: upstream granularity: character
    marked character range {0, 0}
    Currently holding 5 glyphs.
    Glyph tree contents:  5 characters, 5 glyphs, 1 nodes, 64 node bytes, 256 storage bytes, 320 total bytes, 64.00 bytes per character, 64.00 bytes per glyph
    Layout tree contents:  5 characters, 5 glyphs, 0 laid glyphs, 0 laid line fragments, 1 nodes, 64 node bytes, 0 storage bytes, 64 total bytes, 12.80 bytes per character, 12.80 bytes per glyph, 0.00 laid glyphs per laid line fragment, 0.00 bytes per laid line fragment
, glyph range {0 5}. Ignoring...

一般情况下是否可以创建带下划线的标题?

谢谢!

【问题讨论】:

标签: swift xcode cocoa nsbutton


【解决方案1】:

看起来你已经明白了,但出于文档目的:NSUnderlineStyle.single -> NSUnderlineStyle.single.rawValue

@IBOutlet var myButton: NSButton!

let underlineAttribute = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue]
let underlineAttributedString = NSAttributedString(string: "Button Title", attributes: underlineAttribute)
myButton.attributedTitle = underlineAttributedString

【讨论】:

  • 感谢您发布解决方案!一年后,很高兴找到您的答案。
猜你喜欢
  • 2016-03-21
  • 1970-01-01
  • 2015-01-12
  • 2012-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-12
  • 1970-01-01
相关资源
最近更新 更多