Joshua
Joshua

Reputation: 15500

Getting the Current font of a NSTextView

How would I get the current font of an NSTextView?

Upvotes: 0

Views: 1023

Answers (2)

Rob Keniger
Rob Keniger

Reputation: 46020

Peter's answer is correct but is not the whole story. Calling -font on the NSTextView will return the font of the first character in the text view's string, or if the text view is empty it will return the font at the insertion point.

However, if the text view is configured to accept rich text, the text view can have several different fonts.

In that case, you would need to obtain the font from a specific location in the text view's NSTextStorage object, which is a subclass of NSAttributedString. You obtain the NSTextStorage object by calling -textStorage on the text view.

You can get the font at a particular location in the NSTextStorage object by using the -attribute:atIndex:effectiveRange: method of NSAttributedString and passing in NSFontAttributeName for the attribute parameter.

Upvotes: 9

Peter Hosey
Peter Hosey

Reputation: 96323

An NSTextView is a kind of NSText. So, send it a font message.

Upvotes: 4

Related Questions