【问题标题】:Insertion Point in NSTextField missingNSTextField 中的插入点丢失
【发布时间】:2013-11-22 15:31:05
【问题描述】:

我在容器中有一个 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 无法在支持图层的 View 中绘制。

有没有办法显示 InsertionPoint 光标?

编辑

在尝试了所有可能性之后,似乎 InsertionPoint(和 focusRing)没有绘制,因为它的框架被定位在 superviews 边界和它的dirtyDrawRect之外。有没有办法删除 NSView 的剪辑?我需要能够将我的 TextField 放置在每个可能的绝对位置上。

【问题讨论】:

    标签: macos cocoa nstextfield nstextview


    【解决方案1】:

    我找到了解决办法:自己实现绘图。

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

    【讨论】:

      【解决方案2】:

      对于插入点,只需将您的文本字段设置为第一响应者。

        [myTextField becomeFirstResponder];
      

      【讨论】:

      • 当我在 TextField 内单击时,TextField 应该会自动成为 FirstResponder。我可以编辑/选择(选择正在绘制)/复制文本,只是 InsertionPoint 没有绘制。
      猜你喜欢
      • 2020-03-25
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-29
      • 1970-01-01
      相关资源
      最近更新 更多