NSTextField-使所选文本加粗,斜体或下划线? [英] NSTextField - make selected text bold, italic or underline?

查看:308
本文介绍了NSTextField-使所选文本加粗,斜体或下划线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSTextField,用户可以在其中编写文本。我希望能够创建3个按钮:粗体,斜体和下划线;这些按钮应将用户在文本字段中的选择更改为粗体,斜体或下划线。

I have an NSTextField where the user can write text. I would like to be able to make 3 buttons: bold, italic and underline; these buttons should change the user selection in the textfield to either bold, italic or underline.

任何人都可以给我一个如何执行此操作的指针吗?

Can anyone give me a pointer on how to do this?

推荐答案

第一件事是启用富文本支持,您可以在Interface Builder中通过选中富文本来实现。检查器中的选项或使用 setAllowsEditingTextAttributes:的代码。

The first thing is to enable rich text support, and you can do it either in Interface Builder by checking the "Rich Text" option in the inspector or by code using setAllowsEditingTextAttributes:.

然后,所有内容均与有关NSAttributedString s。

Then it's all about NSAttributedStrings.

尽管如此,最大的问题是您似乎需要对所选文本进行更改。对于 NSTextField s,这是不可能的。仅使用 NSTextView s。

The big problem though is that looks like you need to apply changes to the selected text. This is not possible with NSTextFields. Only with NSTextViews.

如果可以更改它,请继续进行,它将使事情变得更加容易。但是,如果确实需要使用 NSTextField ,则可能需要访问字段编辑器。每个窗口都有一个关联的窗口,它是在后台处理文本的窗口。

If you can change it, go ahead and it will make things easier. However, if you do need to stick with NSTextField you may want to access the field editor. Each window has one associated, and it's what process the text behind the scenes.

NSTextView *editor = (NSTextView *)[window fieldEditor:YES forObject:myTextField]

然后您可以调用NSTextView的方法 setSelectedTextAttributes: 愉快。

Then you can call NSTextView's method setSelectedTextAttributes: happily.

了解有关字段编辑器的更多信息此处为Apple 在CocoaDev中

Read more about the field editor here at Apple and in CocoaDev

这篇关于NSTextField-使所选文本加粗,斜体或下划线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆