gpt4 book ai didi

objective-c - 在 NSTextField 内绘制固定符号

转载 作者:行者123 更新时间:2023-12-03 17:50:06 26 4
gpt4 key购买 nike

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

最佳答案

您可以在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];

}

关于objective-c - 在 NSTextField 内绘制固定符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29172719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com