R.swift 让 Swift 更方便快捷安全的使用资源文件, 一旦学会使用, 我保证你会爱上它
R.swift GitHub 地址
添加 pod 'R.swift'
到你的 Podfile 文件中, 然后运行 pod install
在项目中: 点击项目名称, 选择 TARGETS
, 点击 Build Phases
,在点击左上角 +
号添加 New Run Script Phase
打开并复制下面 "$PODS_ROOT/R.swift/rswift" generate "$SRCROOT"
到黑色输入框中
拖动这个脚本在 Check Pods Manifest.lock
之下
4. Build 项目, 在 $SRCROOT
-folder目录下找到 R.generated.swift
文件 , 拖拽 R.generated.swift
文件到项目中,并不勾选 Copy items if needed
let settingsIcon = UIImage(named: "settings-icon")
let settingsIcon = R.image.settingsIcon()
let lightFontTitle = UIFont(name: "Acme-Light", size: 22)
let settingsIcon = R.image.settingsIcon()
let plistURL = Bundle.main.url(forResource: "book", withExtension: "plist")
let jsonPath = Bundle.main.path(forResource: "data", ofType: "json")
let plistURL = R.file.bookPlist()
let jsonPath = R.file.DataJson.path()
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)
Colors are extracted from the *.clr files that are in your Xcode project
label.textColor = R.color.appColors.textColor()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateInitialViewController() as? UITabBarController
let settingsController = storyboard.instantiateViewController(withIdentifier: "settingsController") as? SettingsController
let storyboard = R.storyboard.main()
let tabBarController = R.storyboard.main.initialViewController()
let settingsController = R.storyboard.main.settingsController()
performSegue(withIdentifier: "openSettings", sender: self)
performSegue(withIdentifier: R.segue.overviewController.openSettings, sender: self)
let nib = UINib(nibName: "ToolBar", bundle: nil)
let toolBar = nib.instantiate(withOwner: nil, options: nil).first as? ToolBar
let toolBar = R.nib.toolBar.firstView(owner: self)
let toolBar2 = R.nib.toolBar.secondView(owner: self)
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
}
}
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
}
}