在NSView中添加边框和圆角矩形 [英] Adding border and Rounded Rect in the NSView

查看:1435
本文介绍了在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绘制边框,但它似乎很复杂,任何人都可以建议我任何其他方法



/ p>

Rohan

解决方案

   - (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];
}


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

解决方案

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];
}

这篇关于在NSView中添加边框和圆角矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆