简体   繁体   中英

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

Clarification: First, cursor = the insertion point cursor, not the mouse cursor.

Ok, I would like to return the font / font size / font color wherever the cursor is in the NSTextView. I tried using attribute:atIndex:effectiveRange: , but I failed because I got my variables all mixed up. I think it is what I need. Some example code would just be appreciated, return ing the font. I think it will work the same for font size/color, I'll just have to substitute NSFontAttributeName for something else, right? 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 also has a property textStorage which returns back the instance of NSTextStorage which holds the text. An NSTextStorage inherits from NSMutableAttributedString , which inherits from NSAttributedString , and that has methods to obtain the attributes of the text.

Combined those two and you have your answer.

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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