简体   繁体   English

如何绘制与NSTextView中的文本完全相同的文本

[英]how to draw the text exactly the same as what they are in the NSTextView

I used NSTextView for text editing, setting font color and size, without set NSParagraphStyle . 我使用NSTextView进行文本编辑,设置字体颜色和大小,而未设置NSParagraphStyle Now I want to hide NStextView and take out NSString or NSAttributedString from NSTextView and draw it on the NSView behind the NSTextView object. 现在我想隐藏NStextView并取出NSStringNSAttributedStringNSTextView和借鉴它的NSView的背后NSTextView对象。 I want the rendered text to be exactly the same as in NSTextView , including fonts, word spacing, line height, line spacing. 我希望渲染的文本与NSTextView文本完全相同,包括字体,单词间距,行高,行间距。

What methods should be used to do this? 应该使用什么方法来做到这一点?

My current approach is as follows, but the effect is not good. 我目前的方法如下,但是效果不好。 Line height is not consistent with that in NSTextView . 行高与NSTextView行高NSTextView There is also a problem with other details. 其他细节也有问题。

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];

//after typing, i will hide the textView and draw the attributedString in myView's drawInRect function. //输入后,我将隐藏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];

i have found the answer. 我找到了答案。
[string drawAtPoint:point] - uses NSTypesetterBehavior_10_2_WithCompatibility, but NSTextView uses NSTypesetterLatestBehavior by default. [string drawAtPoint:point]-使用NSTypesetterBehavior_10_2_WithCompatibility,但是NSTextView默认使用NSTypesetterLatestBehavior。

i use the code bellow to solve the problem: 我使用下面的代码来解决问题:

[[textView layoutManager] setTypesetterBehavior:NSTypesetterBehavior_10_2_WithCompatibility];

set the TypesetterBehavier the same as NSString draw functions, then the problem disapears. 将TypesetterBehavier设置为与NSString绘制函数相同,然后问题消失。

the origin link is here: 原始链接在这里:

https://zachwaugh.com/posts/nstextview-and-nsstring-differences-in-text-rendering https://zachwaugh.com/posts/nstextview-and-nsstring-differences-in-text-rendering

so the last code is: 所以最后的代码是:

    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];

and drawing code: 和绘图代码:

    NSRect rct = textView.frame;
    [textView setHidden: YES];
    NSAttributedString textDraw = textView.attributedString;        
    [textDraw drawInRect:rct];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM