gpt4 book ai didi

objective-c - 带圆角的 NSTextField?

转载 作者:太空狗 更新时间:2023-10-30 03:56:46 25 4
gpt4 key购买 nike

我正在尝试围绕 NSTextField 绘制圆角。

我继承了 NSTextField,尝试了下面的代码,但没有任何结果......

有什么想法吗?

- (void)drawRect:(NSRect)dirtyRect
{

// black outline
NSRect blackOutlineFrame = NSMakeRect(0.0, 0.0, [self bounds].size.width, [self bounds].size.height-1.0);
NSGradient *gradient = nil;
if ([NSApp isActive]) {
gradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.24 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:0.374 alpha:1.0]];
}
else {
gradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.55 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:0.558 alpha:1.0]];
}
[gradient drawInBezierPath:[NSBezierPath bezierPathWithRoundedRect:blackOutlineFrame xRadius:5 yRadius:5] angle:90];

}

最佳答案

最好将 NSTextFieldCell 子类化以绘制圆角以保留 NSTextField 功能,例如:

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSBezierPath *betterBounds = [NSBezierPath bezierPathWithRoundedRect:cellFrame xRadius:CORNER_RADIUS yRadius:CORNER_RADIUS];
[betterBounds addClip];
[super drawWithFrame:cellFrame inView:controlView];
if (self.isBezeled) {
[betterBounds setLineWidth:2];
[[NSColor colorWithCalibratedRed:0.510 green:0.643 blue:0.804 alpha:1] setStroke];
[betterBounds stroke];
}
}

产生一个完美的圆形文本字段(如果您首先将其设置为绘制矩形边框,至少):

enter image description here

关于objective-c - 带圆角的 NSTextField?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9930329/

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