【问题标题】:NSTextView caret size sometimes ignoredNSTextView 插入符号大小有时会被忽略
【发布时间】:2013-08-17 15:28:27
【问题描述】:

我正在使用 - (void)drawInsertionPointInRect:(NSRect)aRect color:(NSColor *)aColor turnedOn:(BOOL)flag 更改插入符号的大小。无论如何,当我移动选择时,插入符号会暂时变回其默认大小。

是否有另一种方法可以绘制我需要覆盖的插入符号?

我目前在做什么

- (void)drawInsertionPointInRect:(NSRect)aRect color:(NSColor *)aColor turnedOn:(BOOL)flag {

    aRect.origin.y -= 1;
    aRect.origin.x -= 1;
    aRect.size.width += 1;
    aRect.size.height += 1;

    [super drawInsertionPointInRect:aRect color:aColor turnedOn:flag];

}

【问题讨论】:

    标签: nstextview caret


    【解决方案1】:

    原答案:

    如果您还没有这样做,请将 NSTextView 子类化,然后自己实现 drawInsertionPointInRect:color:turnedOn: 方法来自己绘制精美的插入符号。

    还要注意文档中的这一行:

    调用此方法时,焦点必须锁定在接收器上。 您不需要直接调用此方法。

    子类化是要走的路。

    更多特定于应用程序的答案:

    与其调用[super drawInsertionPointInRect...,不如考虑自己完成所有绘图。

    类似这样的:

    - (void)drawInsertionPointInRect:(NSRect)rect color:(NSColor *)color turnedOn:(BOOL)flag
    {
        //Block Cursor
        if( flag )
        {
            NSPoint aPoint=NSMakePoint( rect.origin.x,rect.origin.y+rect.size.height/2);
            int glyphIndex = [[self layoutManager] glyphIndexForPoint:aPoint inTextContainer:[self textContainer]];
            NSRect glyphRect = [[self layoutManager] boundingRectForGlyphRange:NSMakeRange(glyphIndex, 1)  inTextContainer:[self textContainer]];
    
            [color set ];
            rect.size.width =rect.size.height/2;
            if(glyphRect.size.width > 0 && glyphRect.size.width < rect.size.width) 
                rect.size.width=glyphRect.size.width;
            NSRectFillUsingOperation( rect, NSCompositePlusDarker);
        } else {
            [self setNeedsDisplayInRect:[self visibleRect] avoidAdditionalLayout:NO];
        }
    }
    

    I stole from here的代码)

    【讨论】:

    • 对不起,我实际上正在实现该方法。我更新了问题。 (感谢您的回答)
    • 是的,已经尝试过该代码。移动光标时,它会变回正常
    • 好的。我暂时放弃。这可能与处理插入符号绘图的字段编辑器有关。也许其他人会给你一个更好的答案。
    【解决方案2】:

    我遇到了同样的问题。 多诺万的回答解决了这个问题,但是当我尝试绘制“透明”插入符号时,我需要做更多的事情,我有一个解决方案。

    我为此写了一篇博文。 http://programming.jugglershu.net/wp/?p=765

    “如何画出又厚又透明的插入符号”的答案是……

    • 在 _drawInsertionPointInRect 中绘制透明插入符号:
    • 通过调用 setNeedsDisplay 方法清除 drawInsertionPointInRect 中的插入符号。

    我希望这对想要做同样事情的人有所帮助。

    【讨论】:

      【解决方案3】:

      似乎唯一的解决方案是覆盖未记录的方法- (void)_drawInsertionPointInRect:(NSRect)aRect color:(NSColor *)color。不过,我希望有更好的解决方案。

      【讨论】:

      • 当其他人提供信息时,您是否认真地为自己投票?
      • @uchuugaka 你的意思是迈克尔的回答?老实说,我不明白为什么我创建了另一个答案来重复该信息……很奇怪。感谢您向我指出这一点。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-20
      • 2011-01-16
      相关资源
      最近更新 更多