gpt4 book ai didi

cocoa - 如何在 NSButton 上绘制右上角圆角?

转载 作者:行者123 更新时间:2023-12-03 16:16:37 24 4
gpt4 key购买 nike

在 Cocoa 中有多种绘制四个圆角的方法,可以使用 CALayer 或 NSBezierPath。但是如何在 NSButton 上绘制单个圆角呢?

按钮的当前结构是:

NSButton *button = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 50, 20)];
[button setTitle:@"My button"];
[button.cell setBackgroundColor:[NSColor grayColor]];

我想要做的是一个半径为 10 的圆角右上角。我该怎么做?

最佳答案

解决方案:

覆盖drawRect:

CGFloat cornerRadius = 10;

NSBezierPath *path = [NSBezierPath bezierPath];

// Start drawing from upper left corner
[path moveToPoint:NSMakePoint(NSMinX(self.bounds), NSMinY(self.bounds))];

// Draw top border and a top-right rounded corner
NSPoint topRightCorner = NSMakePoint(NSMaxX(self.bounds), NSMinY(self.bounds));
[path lineToPoint:NSMakePoint(NSMaxX(self.bounds) - cornerRadius, NSMinY(self.bounds))];
[path curveToPoint:NSMakePoint(NSMaxX(self.bounds), NSMinY(self.bounds) + cornerRadius)
controlPoint1:topRightCorner
controlPoint2:topRightCorner];

// Draw right border, bottom border and left border
[path lineToPoint:NSMakePoint(NSMaxX(self.bounds), NSMaxY(self.bounds))];
[path lineToPoint:NSMakePoint(NSMinX(self.bounds), NSMaxY(self.bounds))];
[path lineToPoint:NSMakePoint(NSMinX(self.bounds), NSMinY(self.bounds))];

// Fill path
[[NSColor whiteColor] setFill];
[path fill];

关于cocoa - 如何在 NSButton 上绘制右上角圆角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15064207/

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