iOS11新特性——Asset Catalogs中添加Color
LeonorPower
7年前
<p>iOS11之后,我们不止可以在Asset Catalogs中添加图片,还可以在其中添加Color。</p> <p>点击"New Color Set"之后,我们便可以设置颜色的Asset Catalogs:</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/fc6e0355953a3fd76978537f9dfdaf3f.png"></p> <p>目前Xcode提供多种选择颜色的方案,使用起来非常方便:</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/5a9c5647d01abbb9619493b0f17f5790.png"></p> <p style="text-align:center"><img src="https://simg.open-open.com/show/b34f0d539f95d519756996d883ac3306.png"></p> <p>之后,我们在代码中便可以很方便的使用Asset Catalogs中定义的Color:</p> <pre> <code class="language-objectivec">//Objective-C if (@available(iOS 11.0, *)) { self.view.backgroundColor = [UIColor colorNamed:@"grass"]; } else { // Fallback on earlier versions self.view.backgroundColor = [UIColor redColor]; }</code></pre> <pre> <code class="language-objectivec">//swift view.backgroundColor = UIColor(named:"grass")</code></pre> <p>另外,在Xcode9中提供“基于矢量的 asset”的支持。、</p> <p>在之前的Xcode中,添加 image asset 的时候,我们可以添加pdf格式的。但Xcode只是把@1x,@2x,@3x资源提供给app包,而不是矢量图。</p> <p>在Xcode9中,提供了一个新的选项叫做"Preserve Vector Data"</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/315d3d89654dfb2aad653c5e97b35fd6.png"></p> <p>这样的话,当我们在代码中加载图像的时候,如果我们让它显示的尺寸比它本身尺寸大的话,系统在运行时会自动把它放大。</p> <p>也就是说,在系统渲染图片时,不会有任何的质量损失:</p> <pre> <code class="language-objectivec">//swift let normal = UIImageView(image: UIImage(named: "cocoapod")) normal.tintColor = UIColor(named: "covfefe") view.addSubview(normal) let big = UIImageView(image: UIImage(named: "cocoapod")) big.tintColor = UIColor(named: "cream") big.frame = CGRect(x: 50, y: 200, width: normal.bounds.size.width * 2, height: normal.bounds.size.height * 2) view.addSubview(big)</code></pre> <p style="text-align:center"><img src="https://simg.open-open.com/show/b421de37520c8e8b53dda98463169a69.png"></p> <pre> <code class="language-objectivec">//Objective-c UIImageView *normal = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cluck"]]; normal.center = CGPointMake(100, 150); [self.view addSubview:normal]; UIImageView *big = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cluck"]]; big.frame = CGRectMake(50, 250, normal.bounds.size.width * 2, normal.bounds.size.height * 2); [self.view addSubview:big];</code></pre> <p> </p> <p>来自:http://www.cocoachina.com/ios/20170620/19586.html</p> <p> </p>