Swift-->R.swift带你体验Android中R类的便利
R.swift可以很方便的管理IOS各种资源,有点类似Android中的R类…. 安装方法: http://www.jianshu.com/p/0c97ef3cdc38
相关用法:https://github.com/mac-cain13/R.swift/blob/master/Documentation/Examples.md#examples ImagesR.swift will find both images from Asset Catalogs and image files in your bundle. Vanilla let settingsIcon = UIImage(named: "settings-icon")
let gradientBackground = UIImage(named: "gradient.jpg")
With R.swift let settingsIcon = R.image.settingsIcon()
let gradientBackground = R.image.gradientJpg()
Custom fontsVanilla let lightFontTitle = UIFont(name: "Acme-Light",size: 22)
With R.swift let lightFontTitle = R.font.acmeLight(size: 22)
Resource filesVanilla let jsonURL = NSBundle.mainBundle().URLForResource("seed-data",withExtension: "json")
let jsonPath = NSBundle.mainBundle().pathForResource("seed-data",withExtension: "json")
With R.swift let jsonURL = R.file.seedDataJson()
let jsonPath = R.file.seedDataJson.path()
ColorsVanilla label.backgroundColor = UIColor(red: 0.9,green: 0.9,blue: 0.9,alpha: 0.5)
label.textColor = UIColor(red: 0.3,green: 0.3,blue: 0.3,alpha: 1.0)
With R.swift // Colors are extracted from the *.clr files that are in your Xcode project
label.backgroundColor = R.color.appColors.backgroundColor()
label.textColor = R.color.appColors.textColor()
There are some points to keep in mind when using Color palettes,see About Colors Localized stringsVanilla let welcomeMessage = NSLocalizedString("welcome.message",comment: "")
let settingsTitle = NSLocalizedString("title",tableName: "Settings",comment: "")
// Formatted strings
let welcomeName = String(format: NSLocalizedString("welcome.withName",comment: ""),locale: NSLocale.currentLocale(),"Alice")
// Stringsdict files
let progress = String(format: NSLocalizedString("copy.progress",4,23)
With R.swift // Localized strings are grouped per table (.strings file)
let welcomeMessage = R.string.localizable.welcomeMessage()
let settingsTitle = R.string.settings.title()
// Functions with parameters are generated for format strings
let welcomeName = R.string.localizable.welcomeWithName("Alice")
// Functions with named argument labels are generated for stringsdict keys
let progress = R.string.localizable.copyProgress(completed: 4,total: 23)
StoryboardsVanilla let storyboard = UIStoryboard(name: "Main",bundle: nil)
let initialTabBarController = storyboard.instantiateInitialViewController() as? UITabBarController
let settingsController = self.instantiateViewControllerWithIdentifier("settingsController") as? SettingsController
With R.swift let storyboard = R.storyboard.main()
let initialTabBarController = R.storyboard.main.initialViewController()
let settingsController = R.storyboard.main.settingsController()
SeguesVanilla // Trigger segue with:
performSegueWithIdentifier("openSettings",sender: self)
// And then prepare it:
override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
if let settingsController = segue.destinationViewController as? SettingsController,segue = segue as? CustomSettingsSegue
where segue.identifier == "openSettings" {
segue.animationType = .LockAnimation
settingsController.lockSettings = true
}
}
With R.swift // Trigger segue with:
performSegueWithIdentifier(R.segue.overviewController.openSettings,sender: self)
// And then prepare it:
override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
if let typedInfo = R.segue.overviewController.openSettings(segue: segue) {
typedInfo.segue.animationType = .LockAnimation
typedInfo.destinationViewController.lockSettings = true
}
}
Tip: Take a look at the SegueManager library,it makes segues block based and is compatible with R.swift. NibsVanilla let nameOfNib = "CustomView"
let customViewNib = UINib(nibName: "CustomView",bundle: nil)
let rootViews = customViewNib.instantiateWithOwner(nil,options: nil)
let customView = rootViews[0] as? CustomView
let viewControllerWithNib = CustomViewController(nibName: "CustomView",bundle: nil)
With R.swift let nameOfNib = R.nib.customView.name
let customViewNib = R.nib.customView()
let rootViews = R.nib.customView.instantiateWithOwner(nil)
let customView = R.nib.customView.firstView(owner: nil)
let viewControllerWithNib = CustomViewController(nib: R.nib.customView)
Reusable table view cellsVanilla class FaqAnswerController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
let textCellNib = UINib(nibName: "TextCell",bundle: nil)
tableView.registerNib(textCellNib,forCellReuseIdentifier: "TextCellIdentifier")
}
override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let textCell = tableView.dequeueReusableCellWithIdentifier("TextCellIdentifier",forIndexPath: indexPath) as! TextCell
textCell.mainLabel.text = "Hello World"
return textCell
}
}
With R.swift class FaqAnswerController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerNib(R.nib.textCell)
}
override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let textCell = tableView.dequeueReusableCellWithIdentifier(R.nib.textCell.identifier,forIndexPath: indexPath)!
textCell.mainLabel.text = "Hello World"
return textCell
}
}
Reusable collection view cellsVanilla class RecentsController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
let talkCellNib = UINib(nibName: "TalkCell",bundle: nil)
collectionView?.registerNib(talkCellNib,forCellWithReuseIdentifier: "TalkCellIdentifier")
}
override func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("TalkCellIdentifier",forIndexPath: indexPath) as! TalkCell
cell.configureCell("Item (indexPath.item)")
return cell
}
}
With R.swift class RecentsController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.registerNib(R.nib.talkCell)
}
override func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(R.reuseIdentifier.talkCell,forIndexPath: indexPath)!
cell.configureCell("Item (indexPath.item)")
return cell
}
}
至此: 文章就结束了,如有疑问: QQ群 Android:274306954 Swift:399799363 欢迎您的加入. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |