通过Color Set设置颜色
Xcode 15之前设置
通过字符串名字硬编码的方式
UIColor(named: "primary")
Xcode 15之后
最低支持iOS 11.0,系统会自动生成枚举值,通过枚举值设置
#if canImport(UIKit)
@available(iOS 11.0, tvOS 11.0, *)
@available(watchOS, unavailable)
extension UIKit.UIColor {
/// Initialize a `UIColor` with a color resource.
convenience init(resource: ColorResource) {
#if !os(watchOS)
self.init(named: resource.name, in: resource.bundle, compatibleWith: nil)!
#else
self.init()
#endif
}
}
代码中使用
UIColor
- swift
调用resource:
let color = UIColor(resource: .primary)
- oc
1.需要导航#import "GeneratedAssetSymbols.h"
头文件
2.使用colorNamed
, 颜色前缀为ACColorName...
#import "GeneratedAssetSymbols.h"
UIColor *color = [UIColor colorNamed:ACColorNamePrimary];
UIImage
- swift
调用resource:
let image = UIImage(resource: .flashsaleDetail)
- oc
1.需要导航#import "GeneratedAssetSymbols.h"
头文件
2.使用imageNamed
,图片前缀为ACImageName
#import "GeneratedAssetSymbols.h"
UIImage *image = [UIImage imageNamed:ACImageNameHomeIcFlashdeal];
xib中使用
在xib中选择颜色后会自动生成以下这段代码:
<resources>
<namedColor name="textMain">
<color red="0.20000000000000001" green="0.20000000000000001" blue="0.20000000000000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</namedColor>
</resources>
对应颜色设置代码为:
<color key="textColor" name="textMain"/>
命名规范
查看了GeneratedAssetSymbols.swfit
中ImageResource
的命名,系统会自动转换(建议统一使用小驼峰命名),规则如下:
- 以数字开头,转换命名时会添加_前缀
static let _11Sale = ImageResource(name: "11sale", bundle: resourceBundle)
- 全大写命名保留
static let COD = ImageResource(name: "COD", bundle: resourceBundle)
- 全小写命名保留
static let apple = ImageResource(name: "apple", bundle: resourceBundle)
- 小驼峰命名方式
static let aqsInstagramActivity = ImageResource(name: "AQSInstagramActivity", bundle: resourceBundle)
static let flashsaleDetail = ImageResource(name: "FlashsaleDetail", bundle: resourceBundle)
static let verifideBuyer = ImageResource(name: "Verifide-Buyer", bundle: resourceBundle)
static let aboutShippingTimeIcon = ImageResource(name: "about_shipping_time_icon", bundle: resourceBundle)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...