自定义按钮框架看起来不如圆形矩形 UIButton

发布于 2024-12-12 07:10:18 字数 424 浏览 0 评论 0原文

我正在尝试绘制一个自定义按钮框架,如下所示:

UIBezierPath *stroke = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                  cornerRadius:RECT_CORNECR_RADIUS];

[stroke stroke];

但由于某种原因,角曲线看起来比侧面更周到。如果您查看 UIButton 的默认框架,您会发现它非常统一。 A是UIButton,B是自定义按钮。

任何想法如何让它更像 UIButton。

A 是 UIButton,B 是自定义按钮

I'm trying to draw a custom button frame as follows:

UIBezierPath *stroke = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                  cornerRadius:RECT_CORNECR_RADIUS];

[stroke stroke];

But for some reason the corner curves look thinker than the sides. If you look at the UIButton's default frame it's very uniform. A is a UIButton, B is a custom button.

Any ideas how I can make it more like the UIButton.

A is a UIButton, B is a custom button

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ记忆︶ㄣ 2024-12-19 07:10:19

您正在触摸按钮的边界。这将以视图边缘为中心绘制线条,因此线条厚度的一半在边界之外并且不会绘制。这就是为什么它的角落是全厚度的。在边界矩形上使用 CGRectInset(插入线条粗细的一半)并描边该矩形。

You are stroking the bounds of your button. This will draw your line centred over the edge the view, so half of the thickness of the line is outside the bounds and is not drawn. This is why it is full thickness in the corners. Use CGRectInset on your bounds rectangle (inset by half the thickness of your line) and stroke THAT rect.

故事还在继续 2024-12-19 07:10:19

您遇到的问题可能是由于抗锯齿引起的。您可以在绘制 beizerPath 之前尝试更改 CoreGraphics 的抗锯齿设置。

一个更简单的解决方案是使用按钮的 CALayer 及其 cornerRadius 属性。 绘制圆角会更容易

如果 self 是您的自定义按钮,那么

self.layer.cornerRadius = RECT_CORNER_RADIUS;
self.layer.borderWidth = 1.0f;
self.layer.borderColor = [UIColor blackColor].CGColor;

当然不要忘记导入 QuartzCore 框架并为此导入其标头去工作 (#import)

The problem you have is probably due to antialiasing. You can try to change the antialiasing settings of CoreGraphics before drawing your beizerPath.

An easier solution is to use the CALayer of your button and its cornerRadius property. It would be easier to draw a rounded corner

If self is your custom button:

self.layer.cornerRadius = RECT_CORNER_RADIUS;
self.layer.borderWidth = 1.0f;
self.layer.borderColor = [UIColor blackColor].CGColor;

Of course don't forget to import the QuartzCore framework and import its header for this to work (#import )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文