Mac OS X: how do I get the field editor to scroll instead of wrapping text?

I have a Cocoa application based on a Mac OS X document.

One of the functions that I have in my application is a list of text elements that you can double-click to edit. When the user double-clicks on one of the text elements, I place the current text box of the window fieldEditoron the clicked text element to allow editing.

Everything is working fine, except for one problem. I can’t figure out how to make the text box fieldEditor clip + scroll , and not wrap its text. Here it looks like this:

enter image description here

See how the text wraps in the second line? I do not want it. I would like it to remain one line that scrolls (and the cropped one appears).

Here is an example of how it works correctly on a list item that has less text:

enter image description here

Here is what I am trying in my controller:

NSWindow *win = [listItemView window];
NSText *fieldEditor = [win fieldEditor:YES forObject:listItemView];
[fieldEditor setFont:[TDListItemView titleFont]];
[fieldEditor setAlignment:NSLeftTextAlignment];
[fieldEditor setDrawsBackground:YES];
[fieldEditor setBackgroundColor:[NSColor whiteColor]];
[fieldEditor setString:str];
[fieldEditor setDelegate:self];
[fieldEditor selectAll:nil];

if ([fieldEditor isKindOfClass:[NSTextView class]]) {
    NSTextView *tv = (NSTextView *)fieldEditor;
    NSMutableParagraphStyle *style = [[[tv defaultParagraphStyle] mutableCopy] autorelease];
    [style setLineBreakMode:NSLineBreakByClipping];
    [tv setDefaultParagraphStyle:style];
}

CGRect r = [self fieldEditorRectForBounds:[listItemView bounds] index:idx]; // height here is 10.0
[fieldEditor setFrame:r];
[fieldEditor setNeedsDisplay:YES];
[[self view] addSubview:fieldEditor];
[win makeFirstResponder:fieldEditor];

Note that the part is in the middle: I check to see if this fieldEditorinstance is NSTextViewto invoke a method setDefaultParagraphStyle:on it. This is my attempt to get fieldEditorits text for the clip - using the value NSLineBreakByClipping. This has no effect. And I'm not even sure if this is what I have to do to scroll fieldEditoron one line.

, , -fieldEditorRectForBounds:index:, , ( 14,0 ).

, fieldEditor / ?


:

    [[tv textContainer] setHeightTracksTextView:YES];
    [[tv textContainer] setWidthTracksTextView:YES];

fieldEditor, . , , , + .: (


:

NSStringDrawingUsesLineFragmentOrigin

options::

-[NSAttributedString drawWithRect:options:attributes:]

NSText, NSAttributedString. , NSText.

+5
3

Hmmm... NSTextView NSScrollView . , -, , , "" :

NSTextField IB ​​ . - .

... ...

NSScrollView ( , ) , , .

+3
NSMutableParagraphStyle* style = [[NSMutableParagraphStyle alloc] init];
[style setLineBreakMode:NSLineBreakByTruncatingHead];

NSMutableDictionary* attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:style forKey:NSParagraphStyleAttributeName];

NSTextView* textView = [[NSTextView alloc] init];
[textView setTypingAttributes:attributes];
0

NSTextView, ....

[self.textView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[self.textView setHorizontallyResizable:YES];
[[self.textView textContainer] setWidthTracksTextView:NO];
[[self.textView textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
0

All Articles