iOS11新特性——Asset Catalogs中添加Color
iOS11之后,我們不止可以在Asset Catalogs中添加圖片,還可以在其中添加Color。
點擊"New Color Set"之后,我們便可以設置顏色的Asset Catalogs:
目前Xcode提供多種選擇顏色的方案,使用起來非常方便:
之后,我們在代碼中便可以很方便的使用Asset Catalogs中定義的Color:
//Objective-C
if (@available(iOS 11.0, *)) {
self.view.backgroundColor = [UIColor colorNamed:@"grass"];
} else {
// Fallback on earlier versions
self.view.backgroundColor = [UIColor redColor];
}
//swift
view.backgroundColor = UIColor(named:"grass")
另外,在Xcode9中提供“基于矢量的 asset”的支持。、
在之前的Xcode中,添加 image asset 的時候,我們可以添加pdf格式的。但Xcode只是把@1x,@2x,@3x資源提供給app包,而不是矢量圖。
在Xcode9中,提供了一個新的選項叫做"Preserve Vector Data"
這樣的話,當我們在代碼中加載圖像的時候,如果我們讓它顯示的尺寸比它本身尺寸大的話,系統在運行時會自動把它放大。
也就是說,在系統渲染圖片時,不會有任何的質量損失:
//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)
//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];
來自:http://www.cocoachina.com/ios/20170620/19586.html
本文由用戶 LeonorPower 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!