NSBezierPath绘制圆角矩形的圆角不够圆滑?

在Macos应用开发过程中,使用OC语言编码,效果是:圆角的线宽 比 边框的 大或者浓。

经过大量查询,发现:如果圆角矩形宽高和View的宽高一样大,就导致圆角矩形的边框线有一半在View外面而被裁剪。

调整后的代码如下:

- (void)drawRect:(NSRect)dirtyRect {
  const CGFloat cornerRadius = 8.0;
  const CGFloat lineWidth = 2.0;
  [[NSColor colorWithRed:(140.0/255.0) green:(171.0/255.0) blue:(239.0/255.0) alpha:1.0] setStroke];
  
  
  // 绘制圆角矩形 - 第1种方法。
  NSBezierPath *bezierPath = [NSBezierPath bezierPathWithRoundedRect:CGRectInset(dirtyRect, lineWidth/2, lineWidth/2) xRadius:cornerRadius yRadius:cornerRadius];
  [bezierPath setLineWidth:lineWidth];
  [bezierPath stroke];
  
  
  // // 绘制圆角矩形 - 第2种方法。
  // CGFloat top = CGRectGetMinY(dirtyRect) + lineWidth/2;
  // CGFloat bottom = CGRectGetMaxY(dirtyRect) - lineWidth/2;
  // CGFloat left = CGRectGetMinX(dirtyRect) + lineWidth/2;
  // CGFloat right = CGRectGetMaxX(dirtyRect) - lineWidth/2;
  // CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] CGContext];
  // CGContextBeginPath(context);
  // CGContextMoveToPoint(context, left, top+cornerRadius);
  // CGContextAddArcToPoint(context, left, top, left+cornerRadius, top, cornerRadius);
  // CGContextAddArcToPoint(context, right, top, right, top+cornerRadius, cornerRadius);
  // CGContextAddArcToPoint(context, right, bottom, right-cornerRadius, bottom, cornerRadius);
  // CGContextAddArcToPoint(context, left, bottom, left, bottom-cornerRadius, cornerRadius);
  // CGContextSetLineWidth(context, lineWidth);
  // CGContextClosePath(context);
  // CGContextDrawPath(context, kCGPathStroke);
}

参考链接:

https://outofmemory.cn/web/1018338.html    

https://www.coder.work/article/2362973    。

要使MATLAB GUIDE中的按钮具有圆角,可以尝试以下方法: 1.使用Java中的JButton并设置其外观 使用Java代码创建一个Java JButton并将其添加到MATLAB Guide中的GUI中。然后,使用Java的LookAndFeel类中的UIManager.setLookAndFeel()方法设置按钮外观。 例如,以下代码创建了一个具有圆角的Java JButton: ```matlab import javax.swing.* import java.awt.* import javax.swing.border.EmptyBorder % Create a JButton with rounded corners jButton = JButton('Click me') jButton.setBorder(BorderFactory.createEmptyBorder()) % remove the button border jButton.setForeground(Color.white) jButton.setBackground(Color.blue) jButton.setFocusPainted(false) jButton.setFont(Font('Helvetica', Font.PLAIN, 12)) % Set rounded corners jButton.setBorder(new javax.swing.border.LineBorder(Color.blue, 2, true)) ``` 将其添加到MATLAB GUIDE中的GUI中: ```matlab hFigure = figure() jPanel = get(handle(hFigure), 'JavaFrame') [jPanel, hContainer] = javacomponent(javax.swing.JPanel, [], hFigure) % Add the JButton to the GUI [jButton, hButton] = javacomponent(jButton, [], hContainer) set(hButton, 'Units', 'normalized', 'Position', [.1 .1 .3 .3]) ``` 2.使用MATLAB中的插件 在MATLAB File Exchange中,可以找到一些插件(例如Figure Buttons)可以将圆角按钮添加到MATLAB GUIDE中的GUI中。可以将其安装并使用该插件中提供的函数来创建圆角按钮。 例如: ```matlab f = figure(); % Add a button to the main figure b = createButton(f, 'ButtonText', 'Click Me', 'Units', 'percent', 'Position', [25 25 50 50]); % Set the button corners to be round set(b, 'CornerRadius', 5) ``` 请注意,并非所有MATLAB版本和操作系统都支持此方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值