我在容器中有一个NSTextField:

[textField setFrameOrigin:NSMakePoint(0, -t.frame.size.height)];
content = [[NSView alloc] init];
[content setWantLayer:YES]
content.layer=[CALayer layer];
[content addSubview:textField];
[content scaleUnitSquareToSize:NSMakeSize(1, -1)];
content.frame=textField.frame;
content.layer.backgroundColor=textBGColor.CGColor;


容器本身位于具有以下内容的视图中

[view scaleUnitSquareToSize:NSMakeSize(1, -1)];


这全部都是为了获取TextField的左上角原点,并且效果很好,唯一的问题在于InsertionPoint没有绘制(至少不在可见框架中)。

我假设InsertionPoint不能缩放,也不能用TextField转换。另一种可能性是,不能在基于图层的视图中绘制InsertionPoint。

有没有办法显示InsertionPoint光标?

编辑

在尝试了所有可能性之后,似乎未绘制InsertionPoint(和focusRing),因为它的框架被放置在Superviews范围之外,并且它的dirtyDrawRect也是如此。有没有办法删除NSView的剪辑?我需要能够将TextField放置在所有可能的绝对位置上。

最佳答案

我找到了一种方法:自己执行绘图。

1)提供一个自定义TextView作为窗口的编辑器。

- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
{

    if (!myCustomFieldEditor) {
        myCustomFieldEditor = [[TextView alloc] init];
        [myCustomFieldEditor setFieldEditor:YES];
    }
    return myCustomFieldEditor;
}


2)覆盖自定义TextView类中的drawInsertionPoint方法。

-(void)drawInsertionPointInRect:(NSRect)rect color:(NSColor *)color turnedOn:(BOOL)flag{
    [color set];
    NSRectFill(rect);
    [super drawInsertionPointInRect:rect color:color turnedOn:flag];
}

关于macos - NSTextField中的插入点丢失,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20148554/

10-11 11:57