我使用NSTextView
进行文本编辑,设置字体颜色和大小,而未设置NSParagraphStyle
。
现在,我想隐藏NStextView
并从NSString
中取出NSAttributedString
或NSTextView
并将其绘制在NSView
对象后面的NSTextView
上。
我希望渲染的文本与NSTextView
中的文本完全相同,包括字体,单词间距,行高,行间距。
应该使用什么方法来做到这一点?
我目前的方法如下,但是效果不好。
行高与NSTextView
中的行高不一致。其他细节也有问题。
NSTextView* textView = [[NSTextView alloc]init];
[[self contentView] addSubview:textView positioned:NSWindowAbove relativeTo:myView];
[textView setAutoresizingMask:NSViewWidthSizable];
[textView setMinSize:NSMakeSize(25, 25)];
[textView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[textView setDrawsBackground:NO];
[[textView textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[[textView textContainer] setWidthTracksTextView:NO];
[textView setVerticallyResizable:YES];
[textView setHorizontallyResizable:YES];
[textView setTextColor:[NSColor redColor]];
[textView setFrame: NSMakeRect(200, 200, 25, 25)]; //the textView will auto resize when typing
[textView setFont:[NSFont fontWithName:@"Arial" size:16.0f];
[textView setHidden:NO];
///键入后,我将隐藏textView并在myView的drawInRect函数中绘制attributedString。
NSRect rct = textView.frame;
[textView setHidden: YES];
NSMutableAttributedString *textDraw = [[NSMutableAttributedString alloc] initWithAttributedString: textView.attributedString];
//i get the defaultParagraphStyle to set to NSMutableAttributedString
[textDraw setAttributes:textView.defaultParagraphStyle range:NSMakeRange(0, textDraw.length)];
NSMutableDictionary *md = [NSMutableDictionary dictionary];
[md setObject:[NSColor redColor] forKey:NSForegroundColorAttributeName];
[textDraw setAttributes:md range:NSMakeRange(0, textDraw.length)];
[textDraw drawInRect:rct];
答案 0 :(得分:0)
我找到了答案。
[string drawAtPoint:point]-使用NSTypesetterBehavior_10_2_WithCompatibility,但默认情况下,NSTextView使用NSTypesetterLatestBehavior。
我使用下面的代码来解决问题:
[[textView layoutManager] setTypesetterBehavior:NSTypesetterBehavior_10_2_WithCompatibility];
将TypesetterBehavier设置为与NSString绘制函数相同,然后问题消失。
原始链接在这里:
https://zachwaugh.com/posts/nstextview-and-nsstring-differences-in-text-rendering
所以最后一个代码是:
NSTextView* textView = [[NSTextView alloc]init];
[[self contentView] addSubview:textView positioned:NSWindowAbove relativeTo:myView];
[textView setAutoresizingMask:NSViewWidthSizable];
[textView setMinSize:NSMakeSize(25, 25)];
[textView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[textView setDrawsBackground:NO];
[[textView textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[[textView textContainer] setWidthTracksTextView:NO];
[textView setVerticallyResizable:YES];
[textView setHorizontallyResizable:YES];
[textView setTextColor:[NSColor redColor]];
[textView setFrame: NSMakeRect(200, 200, 25, 25)]; //the textView will auto resize when typing
[textView setFont:[NSFont fontWithName:@"Arial" size:16.0f];
[textView setHidden:NO];
[[textView layoutManager] setTypesetterBehavior:NSTypesetterBehavior_10_2_WithCompatibility];
和绘图代码:
NSRect rct = textView.frame;
[textView setHidden: YES];
NSAttributedString textDraw = textView.attributedString;
[textDraw drawInRect:rct];