- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有了这个应用程序,我正在编写它来熟悉 Swift 和 OSX 编程。这是一个笔记应用程序。笔记窗口由一个 NSTextView 和一个调出 NSFontPanel 的按钮组成。
更改字体效果很好。选择尺码?没问题。想要更改字体的属性,如颜色、下划线等?我完全不确定如何让它工作。
其他来源(例如 here 和 here)似乎表明 NSTextView 应该是 NSFontManager 的目标并且 NSTextView 有它自己的 changeAttributes() 实现。然而,将 NSTextView 设为目标并没有任何作用。当我在 NSTextView 中选择文本并调出字体面板时,我在 fontPanel 中所做的第一个选择会导致取消选择文本。
使我的 View Controller 成为 NSFontManager 的目标并为 changeAttributes 实现一个 stub 会产生一个 NSFontEffectsBox 类型的对象,我找不到任何好的文档。
问题是……我应该用 NSFontEffectsBox 做什么?如果在 fontPanel 中我选择带有双下划线的蓝色文本,我可以在调试器中看到这些属性,但我无法以编程方式访问它们。
相关代码如下:
override func viewDidLoad() {
super.viewDidLoad()
loadNoteIntoInterface()
noteBody.keyDelegate = self // noteBody is the NSTextView
noteBody.delegate = self
noteBody.usesFontPanel = true
fontManager = NSFontManager.sharedFontManager()
fontManager!.target = self
}
改变字体的代码。这很好用。
override func changeFont(sender: AnyObject?) {
let fm = sender as! NSFontManager
if noteBody.selectedRange().length>0 {
let theFont = fm.convertFont((noteBody.textStorage?.font)!)
noteBody.textStorage?.setAttributes([NSFontAttributeName: theFont], range: noteBody.selectedRange())
}
}
changeAttributes stub 代码:
func changeAttributes(sender: AnyObject) {
print(sender)
}
所以..我的目标有两个:
谢谢。
最佳答案
所以我确实设法找到了某种答案。以下是我在我的程序中实现 changeAttributes() 的方式:
func changeAttributes(sender: AnyObject) {
var newAttributes = sender.convertAttributes([String : AnyObject]())
newAttributes["NSForegroundColorAttributeName"] = newAttributes["NSColor"]
newAttributes["NSUnderlineStyleAttributeName"] = newAttributes["NSUnderline"]
newAttributes["NSStrikethroughStyleAttributeName"] = newAttributes["NSStrikethrough"]
newAttributes["NSUnderlineColorAttributeName"] = newAttributes["NSUnderlineColor"]
newAttributes["NSStrikethroughColorAttributeName"] = newAttributes["NSStrikethroughColor"]
print(newAttributes)
if noteBody.selectedRange().length>0 {
noteBody.textStorage?.addAttributes(newAttributes, range: noteBody.selectedRange())
}
}
调用 sender 上的 convertAttributes() 返回一个属性数组,但名称似乎不是 NSAttributedString 正在寻找的。所以我只是将它们从旧名称复制到新名称并继续发送。这是一个好的开始,但我可能会在添加属性之前删除旧 key 。
问题仍然存在,但是...这是做事的正确方法吗?
关于swift - NSTextView、NSFontManager 和 changeAttributes(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33438648/
如何调整 NSTextView 的文本以适合框的内容,而无需滚动?例如,如果用户输入一个长字符串,并且如果我稍后将该字符串显示为 NSTextView,我该如何使字体减小到足够小以适合该框? 最佳答案
我有一个包含格式化文本和嵌入图像的 NSTextView,如下所示。 我想将上面转换为纯文本,如下所示: Hi this is test data (...picture...)This is col
我对 Cocoa 很陌生,这可能是一个全新的问题。然而,我很沮丧。 我有一个非常简单的 Cocoa 应用程序(称为“行”)来测试将 1000 行文本发送到 TextView 。 我在 Xcode 4
我正在使用Markdown语法的Mac应用程序中的富文本编辑器上工作。我使用NSTextStorage监视Markdown语法中的匹配项,然后将样式实时应用于NSAttributedString,如下
当用户将数据粘贴到 NSTextView 中时,我需要收到通知,以便删除任何不允许的数据类型(url 链接等)。我不想使用 NSTextStorageDelegate 的 textStorageDid
我有一个继承自 NSTextView 的类,该类在初始化时使用 setContinouslySpellCheckingEnabled(true) 设置自动拼写检查,并且在聚焦时将自身设置为第一响应者。
如果您在 Mac 上安装 Microsoft Word 并打开文档,则可以通过按 Command + 2 将行距增加到 2。现在我的 cocoa 应用程序使用 NSTextView。我的用户希望应用程
NSTextView从哪里来? 在我的 OSX 应用程序中,我有一个 NSSearchField,我实现了 controlTextDidChange:,其中在键 NSFieldEditor 中包含一个
使用什么技术来创建一个包含电子邮件文本气泡的 TextView,与电子邮件中类似 最佳答案 这是一个 NSTokenField。看看它和 NSTokenFieldCell 类。如果您需要做的事情是高度
我想创建一个 TextView ,它将支持突出显示基本内容,例如链接和主题标签。类似的功能可以在 Twitter.app 中找到: 不需要支持点击这些链接,只需要在用户编辑 TextView 内容时正
我正在尝试更改 NSTextView 中的预设制表位。我发现了一个旧的帖子表单 2002,它说可以通过显示 NSTextView 的标尺然后编辑其上的选项卡来从 Interface Builder 中
我在 NSTextView 上打字时遇到挂起。 NSTextView 位于我以模态方式运行的窗口上。 [NSApp runModalForWindow:[自身窗口]]; 我不太确定是怎么回事,但我认为
我已经下载:NSTextView 突出显示:enter link description here 它按预期工作,如果您打开应用程序并输入符号“-”,它会变成红色,但后来我遇到了问题。我在.h文件中写
我正在尝试创建一个 NSTextView,其行为有点像一系列打字机页面 - 每个页面都有固定数量的可用行和列。 底层表示与显示完美匹配,而显示又与文件格式匹配。页面以换页符 '\f' 结尾。例如,两个
如何获取 NSTextView 插入符位置更改的回调? 我尝试过 KVO [textView addObserver:self forKeyPath:@"selectedRange" options:
我在 NSScrollView 中有一个 NSTextView,并且我以编程方式插入 NSView 子类作为 NSTextView 有什么方法可以让 NSTextView 接收其所有事件,即使我的 N
我的窗口上有一个 NSTextView 对象。我将 NSStrings 传递给它并显示出来。我想更改显示的文字大小。我怎么做?我是在检查器中执行此操作还是有办法在代码中执行此操作?请举个例子。谢谢!
我有一个带有 TextView 的 ScrollView 。如何找到当前选择的 TextView ? NSTextDelegate 中的委托(delegate)方法仅在您开始编辑时才起作用,而不是在首
我一定是做错了什么: 我的 Cocoa 应用程序有一个围绕自定义 View 的 ScrollView ,而自定义 View 又具有一个 TextView 。我只希望看到一个“这是一个”字符串,但角落里
我在使用 NSTextview 时遇到问题,它应该不断更新日志文件的内容。该应用程序是一个主从 UI,主视图包含一组“备份”对象,而详细 View 包含一个 NSTabView,其中一个选项卡包含 N
我是一名优秀的程序员,十分优秀!