I have an NSTextView that needs to be fixed. When the text comes to the bottom line and to the right edge of the assigned frame, I need to set NSLineBreakByTruncatingTail to present the user with elliptical characters and disable further typing. When I do this, setting the paragraph style to textStorage of NSTextView , the text scrolls up, and in the end I see only the last line with elliptical characters. My question is how to prevent NSTextView from scrolling when changing its lineBreakMode ?
Thanks Nav
EDIT: (screenshots added) Until:

After:

Now here is the code that does this:
- (void)textDidChange:(NSNotification *)notification { NSString *text = [textView string]; CGFloat width, height; NSRect newFrame = self.frame; width = round([text widthForHeight:newFrame.size.height font:textView.font]); height = round([text heightForWidth:newFrame.size.width font:textView.font]); if (height > newFrame.size.height && width > newFrame.size.width) { NSTextContainer *textContainer = textView.textContainer; [textContainer setWidthTracksTextView:NO]; [textContainer setHeightTracksTextView:NO]; [textView setHorizontallyResizable:NO]; [textView setVerticallyResizable:NO]; [textView setAutoresizingMask:NSViewNotSizable]; [textView setLineBreakMode:NSLineBreakByTruncatingTail]; } }
The widthForHeight and heightForWidth functions are some third-party add-ons that work well. The problem is this sudden scrolling that happens when I set NSLineBreakByTruncatingTail . Here is a function that sets the line break mode (also third-party addition - from a good developer (I donβt remember his name)):
- (void)setLineBreakMode:(NSLineBreakMode)lineBreakMode { NSDictionary* curAttributes = [[self textStorage] attributesAtIndex:0 effectiveRange:NULL]; NSParagraphStyle *currentStyle = [curAttributes objectForKey:NSParagraphStyleAttributeName]; if (currentStyle.lineBreakMode != lineBreakMode) { NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy] ; [paragraphStyle setLineBreakMode:lineBreakMode] ; NSMutableDictionary* attributes = [[[self textStorage] attributesAtIndex:0 effectiveRange:NULL] mutableCopy] ; [attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName] ; [paragraphStyle release] ; NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:[self string] attributes:attributes] ; [attributes release] ; [[self textStorage] setAttributedString:attributedString] ; [attributedString release] ; } }
Nava carmon
source share