简体   繁体   English

如何在NSTextView中的光标处获取当前字体/字体大小?

[英]How do I get the current font/font size at cursor in NSTextView?

Clarification: First, cursor = the insertion point cursor, not the mouse cursor. 说明:首先,cursor =插入点光标,而不是鼠标光标。

Ok, I would like to return the font / font size / font color wherever the cursor is in the NSTextView. 好的,无论光标在NSTextView中的什么位置,我都想返回字体/字体大小/字体颜色。 I tried using attribute:atIndex:effectiveRange: , but I failed because I got my variables all mixed up. 我尝试使用attribute:atIndex:effectiveRange:但是失败了,因为我的变量都混在一起了。 I think it is what I need. 我认为这是我所需要的。 Some example code would just be appreciated, return ing the font. 一些示例代码将不胜感激, return字体。 I think it will work the same for font size/color, I'll just have to substitute NSFontAttributeName for something else, right? 我认为它对字体大小/颜色也一样,我只需NSFontAttributeName替代其他内容,对吗? Thanks in advance! 提前致谢!

NSFont *font = [textView.textStorage attribute:NSFontAttributeName atIndex:textView.selectedRange.location effectiveRange:nil];

应该为所有人的名字而努力。

What have you tried? 你尝试了什么?

NSTextView has a method selectedRanges which returns the current selection(s) - just one of zero-length if there is just an insertion point. NSTextView有一个方法selectedRanges ,它返回当前的选择-如果只有一个插入点,则只是零长度之一。

NSTextView also has a property textStorage which returns back the instance of NSTextStorage which holds the text. NSTextView还有一个属性textStorage ,该属性返回保存文本的NSTextStorage实例。 An NSTextStorage inherits from NSMutableAttributedString , which inherits from NSAttributedString , and that has methods to obtain the attributes of the text. NSTextStorage继承自NSMutableAttributedString ,后者继承自NSAttributedString ,并且具有获取文本属性的方法。

Combined those two and you have your answer. 将这两个结合起来,您就会得到答案。

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM