在 NSView 中添加边框和圆角矩形
在我的应用程序中,NSView应该有圆角矩形和边框,我尝试
static CGColorRef CGColorCreateFromNSColor (CGColorSpaceRef
colorSpace, NSColor *color)
{
NSColor *deviceColor = [color colorUsingColorSpaceName:
NSDeviceRGBColorSpace];
float components[4];
[deviceColor getRed: &components[0] green: &components[1] blue:
&components[2] alpha: &components[3]];
return CGColorCreate (colorSpace, components);
}
在InitWithframe中添加以下几行代码,
[[self layer] setCornerRadius:505];
[[self layer] setBorderWidth:500.0];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
CGColorRef cgColor = CGColorCreateFromNSColor (colorSpace, [NSColor whiteColor]);
CGColorSpaceRelease (colorSpace);
[[self layer] setBorderColor:cgColor];
但根本没有效果,有没有其他方法,
我能猜到的另一种方法是,在drawRect中绘制边框,但它似乎很复杂,有人可以建议我任何其他方法
亲切的问候
罗汉
In my Application , NSView should have rounded rect and border, i tried following
static CGColorRef CGColorCreateFromNSColor (CGColorSpaceRef
colorSpace, NSColor *color)
{
NSColor *deviceColor = [color colorUsingColorSpaceName:
NSDeviceRGBColorSpace];
float components[4];
[deviceColor getRed: &components[0] green: &components[1] blue:
&components[2] alpha: &components[3]];
return CGColorCreate (colorSpace, components);
}
and in InitWithframe added following lines of Code
[[self layer] setCornerRadius:505];
[[self layer] setBorderWidth:500.0];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
CGColorRef cgColor = CGColorCreateFromNSColor (colorSpace, [NSColor whiteColor]);
CGColorSpaceRelease (colorSpace);
[[self layer] setBorderColor:cgColor];
but no effects at all, is there any other method,
Another approach what i could guess is , in drawRect draw border and but it seems very complex, can anyone suggest me any other method
Kind Regards
Rohan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢您看到这个,这个逻辑对我有用,
Thanks for looking at this, this logic worked for me,
为了使图层属性产生任何影响,您需要首先将 NSView 上的 setWantsLayer 设置为 YES。
我在 InitWithFrame 中有这样的观点:
For the layer properties to have any affect you need to set setWantsLayer on your NSView to YES first.
I have this in InitWithFrame for my view:
对之前的答案做了一点改进。如果您不想子类化 NSView(如果它是控制器的基本视图),您也可以执行以下操作:
A little enhancement to the previous answer. If you don't want to subclass your NSView if it's the base view of your controller you can also do something like this: