How would I get the current font of an NSTextView开发者_Go百科?
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.
An NSTextView
is a kind of NSText
. So, send it a font
message.
精彩评论