【问题标题】:Drawing fixed symbol inside NSTextField在 NSTextField 内绘制固定符号
【发布时间】:2015-03-20 17:43:58
【问题描述】:

我有一个NSTextField,我希望一个符号始终出现在最左侧,文本字段的内容从右侧开始。例如,您可以想象一个在最左侧带有美元符号 ($) 前缀的文本字段。可以在它上面添加一个标签,然后子类NSTextFieldCell 来添加左填充,我已经这样做了,但是我需要在不同的位置重用这个文本字段,所以我宁愿直接在@987654323 中绘制符号@ 子类如果可能的话。我注意到drawWithFrame:inView: 方法,这个绘图代码应该放在哪里?如果是这样,如何在该框架中正确绘制标签?

【问题讨论】:

  • 自定义绘制矩形?

标签: objective-c macos cocoa nstextfield nstextfieldcell


【解决方案1】:

您可以在 drawWithFrame:inView: 中进行绘图。绘图后修改矩形,然后调用“[super drawWithFrame]。 喜欢

-(void)drawWithFrame:(NSRect)rect inView:(NSView*)view
{
  NSString *str = @"$";
  NSRect someRect;//string draw rect
  [str drawInRect:someRect withAttributes:nil];
  rect.origin.x += someRect.size.width;
  rect.size.width -= someRect.size.width;
  [super drawWithFrame:rect inView:view];

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多