Selectable text in an NSView subclass
I was surprised to see that there was no documentation or examples of any sort on how this would be done, but I was wondering what was the best way to draw and manage selectable text in an NSView
subclass. I have 开发者_开发知识库taken a look at NSText
, but I think that's a bit overkill for my needs (or is it?). I don't need the text to be editable or anything like that, just simple selectable text (without having to resort to creating separate NSTextView
instances every time I want selectable text.
Any pointers are appreciated.
NSTextField
should do it for you. A "label-style" field can be made selectable but not editable.
NSTextField * myTextField = [[NSTextField alloc] initWithFrame:rectWhereIWantTheTextField];
[myTextField setEditable:NO];
//[myTextField setSelectable:YES]; // This is YES by default
[myTextField setStringValue:myStringForDisplay];
Set 'Editable' and 'Field Editor' unchecked from behavior of TextField from Interface Builder. Like this.
I ended up using an NSTextView
subview to embed selectable text, but for anyone who's interested, twitter's TwUI framework has a class called TUITextRenderer
that lets you embed selectable text in a TUIView
subclass.
精彩评论