xcode技巧

Swift技巧 - Color Literal颜色智能提示 与

2019-07-05  本文已影响0人  小小啄

颜色辅助功能Color Literal 与 图片辅助功能Image Literal 是Xcode8之后官方提供的功能,但仅能在Swift中使用,OC中不能使用;

1、Color Literal 功能:

过去我们在代码中如果需给文字或者 View 设置一些 UIColor 颜色值,通常的写法无非就是下面这几种:

//使用UIColor预制的几种常用颜色
UIColor.orange
//使用自定义的RGB配色(10机制形式)
UIColor(red: 55/255, green: 186/255, blue: 89/255, alpha: 0.5)
//使用自定义的RGB配色(16机制形式)
UIColor(red: 0x37/255, green: 0xba/255, blue: 0x46/255, alpha: 0.5)

这样不仅写起来麻烦,看起来也不直观;而Xcode提供的Color Literal功能就能很好的解决这些问题;如何使用:

2、Image Literal 功能:

同样之前我们使用代码给一个UIImageView添加图片时都会使用:

imageV.image = UIImage(named: "loading")

这样写同样不直观,name也容易写错;而Xcode提供的Image Literal功能就能很好的解决这些问题;如何使用:

上一篇下一篇

猜你喜欢

热点阅读