在Macos應(yīng)用開發(fā)過程中,使用OC語言編碼,效果是:圓角的線寬 比 邊框的 大或者濃。
經(jīng)過大量查詢,發(fā)現(xiàn):如果圓角矩形寬高和View的寬高一樣大,就導(dǎo)致圓角矩形的邊框線有一半在View外面而被裁剪。
調(diào)整后的代碼如下:
- (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? ??文章來源:http://www.zghlxwxcb.cn/news/detail-682877.html
https://www.coder.work/article/2362973? ? 。文章來源地址http://www.zghlxwxcb.cn/news/detail-682877.html
到了這里,關(guān)于NSBezierPath繪制圓角矩形的圓角不夠圓滑?的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!