Swift-使用 R.swift 优雅的使用资源文件
来源:互联网 发布:深圳软件开发工资水平 编辑:程序博客网 时间:2024/05/20 07:51
R.swift 让 Swift 更方便快捷安全的使用资源文件, 一旦学会使用, 我保证你会爱上它
R.swift GitHub 地址
R.swift 特点:
- 每当项目build时,R.swift开始运行
- 这个文件根据项目里的资源文件自动在 R.generated.swift 文件中按照类型生成结构体
- 强类型,无需类型判断和转换,自动返回对应类型
- 支持多种资源类型
- 自动完成,无需猜测图像名称,避免资源名称拼写错误
安装
添加
pod 'R.swift'
到你的 Podfile 文件中, 然后运行pod install
在项目中: 点击项目名称, 选择
TARGETS
, 点击Build Phases
,在点击左上角+
号添加New Run Script Phase
打开并复制下面
"$PODS_ROOT/R.swift/rswift" "$SRCROOT"
到黑色输入框中
4. Build 项目, 在 $SRCROOT
-folder目录下找到 R.generated.swift
文件 , 拖拽 R.generated.swift
文件到项目中,并不勾选 Copy items if needed
R.swift 的具体使用
一、图片-Images
原始用法
let settingsIcon = UIImage(named: "settings-icon")
使用 R.swift
let settingsIcon = R.image.settingsIcon()
二、字体-Fonts
原始用法
let lightFontTitle = UIFont(name: "Acme-Light", size: 22)
使用 R.swift
let settingsIcon = R.image.settingsIcon()
三、文件-Files
原始用法
let plistURL = Bundle.main.url(forResource: "book", withExtension: "plist")let jsonPath = Bundle.main.path(forResource: "data", ofType: "json")
使用 R.swift
let plistURL = R.file.bookPlist()let jsonPath = R.file.DataJson.path()
四、颜色-Colors
R.swift 对于颜色是通过(.clr)文件获取颜色
(.clr)文件具体详情请看上篇博客(Swift-颜色设置技巧和(.clr)文件的创建和使用)
R.swift 通过 R.color.xxx 使用颜色
原始用法
label.textColor = UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 1.0)
使用 R.swift
Colors are extracted from the *.clr files that are in your Xcode project
label.textColor = R.color.appColors.textColor()
五、故事版-Storyboards
原始用法
let storyboard = UIStoryboard(name: "Main", bundle: nil)let tabBarController = storyboard.instantiateInitialViewController() as? UITabBarControllerlet settingsController = storyboard.instantiateViewController(withIdentifier: "settingsController") as? SettingsController
使用 R.swift
let storyboard = R.storyboard.main()let tabBarController = R.storyboard.main.initialViewController()let settingsController = R.storyboard.main.settingsController()
六、Segues
原始用法
performSegue(withIdentifier: "openSettings", sender: self)
使用 R.swift
performSegue(withIdentifier: R.segue.overviewController.openSettings, sender: self)
七、Nibs
原始用法
let nib = UINib(nibName: "ToolBar", bundle: nil)let toolBar = nib.instantiate(withOwner: nil, options: nil).first as? ToolBar
使用 R.swift
let toolBar = R.nib.toolBar.firstView(owner: self)let toolBar2 = R.nib.toolBar.secondView(owner: self)
八、Reusable identifier / table view cells
原始用法
class FaqAnswerController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() let textCellNib = UINib(nibName: "TextCell", bundle: nil) tableView.register(textCellNib, forCellReuseIdentifier: "TextCellIdentifier") } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let textCell = tableView.dequeueReusableCell(withIdentifier: "TextCellIdentifier", for: indexPath) as! TextCell textCell.mainLabel.text = "Hello World" return textCell }}
使用 R.swift
class FaqAnswerController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() tableView.register(R.nib.textCell) } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let textCell = tableView.dequeueReusableCell(withIdentifier: R.nib.textCell.identifier, for: indexPath)! textCell.mainLabel.text = "Hello World" return textCell }}
阅读全文
0 0
- Swift-使用 R.swift 优雅的使用资源文件
- R.swift:以一种优雅安全的方式使用资源文件
- Swift UILabel的使用
- swift UIButton的使用
- Swift UILabel的使用
- swift UIButton的使用
- UIAlertController的使用(Swift)
- UIPageViewController的使用(Swift)
- Swift断言的使用
- Swift函数的使用
- swift -UIView的使用
- swift- tableView 的使用
- Swift字典的使用
- swift 定时器的使用
- Swift 类的使用
- Swift 三方库的使用
- swift 的 UIAlertController使用
- Swift ?? 符号的使用
- 持续集成篇-- SonarQube代码质量管理平台的安装
- word2vec浅析
- 【备忘】使用 Docker Machine 创建虚拟机的脚本及参数
- BootStrap 模态框禁用ESC关闭、禁用点击空白处关闭
- Android进阶之路
- Swift-使用 R.swift 优雅的使用资源文件
- java数组元素倒序的三种方法
- Nginx + uwsgi + Django 简单上线配置
- weblogic项目可以没有web.xml文件?
- js学习笔记
- 如何在predix上构建使用postgresql服务的Java Webapp
- FTP连接图片上传工具类
- python 位运算
- tomcat部署项目方式