Cocoa NSLineBreakByTruncingTail在NSTextView中设置为段落样式时滚动文本
我有一个Cocoa NSLineBreakByTruncingTail在NSTextView中设置为段落样式时滚动文本,cocoa,scroll,osx-lion,nstextview,line-breaks,Cocoa,Scroll,Osx Lion,Nstextview,Line Breaks,我有一个NSTextView,它应该是固定大小的。当文本到达指定帧的底线和右边缘时,我需要设置NSLineBreakByTruncatingTail,以便向用户显示椭圆符号并禁用进一步键入。 当我在NSTextView的textStorage上设置段落样式时,文本向上滚动,我只看到最后一行带有椭圆符号。我的问题是如何防止NSTextView在更改其lineBreakMode时滚动 谢谢, 纳瓦 编辑:(添加屏幕截图)之前: 之后: 下面是执行此操作的代码: - (void)textDidCh
NSTextView
,它应该是固定大小的。当文本到达指定帧的底线和右边缘时,我需要设置NSLineBreakByTruncatingTail
,以便向用户显示椭圆符号并禁用进一步键入。
当我在NSTextView
的textStorage
上设置段落样式时,文本向上滚动,我只看到最后一行带有椭圆符号。我的问题是如何防止NSTextView
在更改其lineBreakMode
时滚动
谢谢,
纳瓦
编辑:(添加屏幕截图)之前:
之后:
下面是执行此操作的代码:
- (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];
}
}
函数widthForHeight
和heightForWidth
是一些第三方添加的功能,它们运行良好。问题在于,当我设置NSLineBreakByTruncatingTail
时,会出现这种突然的滚动。
下面是设置换行模式的函数(也是第三方添加-来自一个优秀的开发人员(不记得他的名字)):
如果您的
NSTextView
实例不可编辑,我建议您创建NSView
的自定义子类并使用它。这就是问题所在。必须是这样。你们能添加屏幕截图,显示你们改变段落风格前后的情况吗?
- (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] ;
}
}