Mac之NSImageView的简单实用

MacNSImageView的简单实用

2019年01月31日 09:30:54 sundaysme 阅读数:1更多

个人分类: swift之NSImageView

https://blog.csdn.net/sundaysme/article/details/86710997

NSImageView控件

Image:指定图片文件名。除了使用开发者添加到项目中的文件外, Xcode内部自带很多图标,可以从下拉框中选择使用

BorderType:样式。设置图片视图的边框

Alignment:图像在图片视图中的对齐方式

圆角处理

通过层来处理,首先必须修改wantsLayer属性为true, 控表示使用层,然后设置层的cornerRadius属性为圆角半径,相关代码如下

//swift NSImageView

        let diameter : CGFloat = 70.0

        let imageView = NSImageView(frame: NSMakeRect(0, 0, diameter, diameter))

        imageView.image = NSImage(named: "name")

        self.view.addSubview(imageView)

        //设置圆角

        imageView.wantsLayer = true

        imageView.layer?.cornerRadius = diameter / 2.0

        imageView.layer?.backgroundColor = NSColor.green.cgColor

        imageView.layer?.masksToBounds = true

//oc之初始化NSImageView并设置它的大小

NSImageView *imgView = [[NSImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-35, 100, 70, 70)];

//给图片赋值和iOS开发是一样的

imgView.image = [NSImage imageNamed:@"1"];

[self.view addSubview:imgView];

//设置圆角

imgView.wantsLayer = YES;

imgView.layer.cornerRadius = 35.0f;

imgView.layer.borderWidth = 2;

imgView.layer.borderColor = [NSColor greenColor].CGColor;

imgView.layer.masksToBounds = YES;

最终效果图如下

pastedGraphic.png

NSimgView.png

链接:https://www.jianshu.com/p/46c95bdadba4

原文地址:https://www.cnblogs.com/sundaymac/p/10340582.html