在 NSView 中添加边框和圆角矩形

发布于 2024-10-18 01:21:45 字数 995 浏览 2 评论 0原文

在我的应用程序中,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 技术交流群。

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

发布评论

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

评论(3

记忆之渊 2024-10-25 01:21:45

感谢您看到这个,这个逻辑对我有用,

- (void)drawRect:(NSRect)rect
{
   if([self hasBorder])
    [self drawBorder:rect];

}

-(void)drawBorder:(NSRect)rect{
    NSRect frameRect = [self bounds];

    if(rect.size.height < frameRect.size.height) 
        return;
    NSRect newRect = NSMakeRect(rect.origin.x+2, rect.origin.y+2, rect.size.width-3, rect.size.height-3);

    NSBezierPath *textViewSurround = [NSBezierPath bezierPathWithRoundedRect:newRect xRadius:10 yRadius:10];
    [textViewSurround setLineWidth:BORDER_WIDTH];
    [pBorderColor set];
    [textViewSurround stroke];
}

Thanks for looking at this, this logic worked for me,

- (void)drawRect:(NSRect)rect
{
   if([self hasBorder])
    [self drawBorder:rect];

}

-(void)drawBorder:(NSRect)rect{
    NSRect frameRect = [self bounds];

    if(rect.size.height < frameRect.size.height) 
        return;
    NSRect newRect = NSMakeRect(rect.origin.x+2, rect.origin.y+2, rect.size.width-3, rect.size.height-3);

    NSBezierPath *textViewSurround = [NSBezierPath bezierPathWithRoundedRect:newRect xRadius:10 yRadius:10];
    [textViewSurround setLineWidth:BORDER_WIDTH];
    [pBorderColor set];
    [textViewSurround stroke];
}
欢烬 2024-10-25 01:21:45

为了使图层属性产生任何影响,您需要首先将 NSView 上的 setWantsLayer 设置为 YES。

我在 InitWithFrame 中有这样的观点:

[self setWantsLayer: YES];
[self.layer setBorderWidth: 2];
[self.layer setCornerRadius: 10];

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:

[self setWantsLayer: YES];
[self.layer setBorderWidth: 2];
[self.layer setCornerRadius: 10];
静待花开 2024-10-25 01:21:45

对之前的答案做了一点改进。如果您不想子类化 NSView(如果它是控制器的基本视图),您也可以执行以下操作:

override func viewDidLoad() {
        super.viewDidLoad()

        self.view.wantsLayer = true
    }

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:

override func viewDidLoad() {
        super.viewDidLoad()

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