R.swift はがし
R.swift を使わなくてもよくなってきた。Warningが多いので剥がすことにした。
使っていたところ
-
R.image
-
R.color
-
R.nib
-
R.reuseIdentifier
-
R.string.localizable
-
R.font
大枠
| 項目 | 🟠R.swift | 🟢R.swiftはがし |
|---|---|---|
| 画像 | R.image.hogeImage() |
UIImage(resource: .hogeImage) |
| 色 | R.color.lightPink() |
UIColor(resource: .lightPink) |
| nib | R.nib.hogeView |
UINib(nibName: "HogeView", bundle: nil) |
| UITableView UICollectionView |
R.reuseIdentifier.hogeCollectionViewCell |
HogeCollectionViewCell.className(クラス名じゃなくても全然ok) |
| フォント | R.font.hogeBold() |
UIFont(name: "hogeBold.otf", size: 20) |
| ローカライズ | R.string.localizable.hogeText() |
String(localized: "Hoge.Text", defaultValue: "Hoge")String Catalogs を使う |
画像
🟠 R.swift
R.image.hogeImage()
🟢 R.swift はがし
UIImage(resource: .hogeImage)
色
🟠 R.swift
R.color.lightPink()
🟢 R.swift はがし
UIColor(resource: .lightPink)
xib
ViewController系
🟠 R.swift
HogeViewController(nib: R.nib.hogeViewController)
🟢 R.swift はがし
HogeViewController()
// もしくは
HogeViewController(nibName: HogeViewController.nibName, bundle: nil)
import UIKit
extension UIViewController {
static var nibName: String {
String(describing: self)
}
}
カスタムView系
🟠 R.swift
R.nib.hogeView
🟢 R.swift はがし
UINib(nibName: HogeView.nibName, bundle: nil)
import UIKit
extension UIView {
static var nibName: String {
String(describing: self)
}
}
UITableView
🟠 R.swift
// セルの登録
tableView.register(R.nib.hogeTableViewCell)
// セルの再利用
let cell = tableView.dequeueReusableCell(withIdentifier: R.reuseIdentifier.hogeTableViewCell, for: indexPath)!
🟢 R.swift はがし
// セルの登録
tableView.register(UINib(nibName: HogeTableViewCell.nibName, bundle: nil), forCellReuseIdentifier: HogeTableViewCell.className)
// セルの再利用
guard let cell = tableView.dequeueReusableCell(withIdentifier: HogeTableViewCell.className, for: indexPath) as? HogeTableViewCell else { fatalError("cellの設定が正しくありません。") }
import Foundation
extension NSObject {
class var className: String {
return String(describing: self)
}
var className: String {
return type(of: self).className
}
}
UICollectionView
🟠 R.swift
// セルの登録
collectionView.register(R.nib.hogeCollectionViewCell)
// セルの再利用
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: R.reuseIdentifier.hogeCollectionViewCell, for: indexPath)!
🟢 R.swift はがし
// セルの登録
collectionView.register(UINib(nibName: HogeCollectionViewCell.nibName, bundle: nil), forCellReuseIdentifier: HogeCollectionViewCell.className)
// セルの再利用
guard let cell = collectionView.dequeueReusableCell(withIdentifier: HogeCollectionViewCell.className, for: indexPath) as? HogeCollectionViewCell else { fatalError("cellの設定が正しくありません。") }
import Foundation
extension NSObject {
class var className: String {
return String(describing: self)
}
var className: String {
return type(of: self).className
}
}
フォント
🟠 R.swift
R.font.hogeBold(size: 20)
🟢 R.swift はがし
UIFont(name: "hogeBold.otf", size: 20)
こういうのでも
enum FontResource {
static let hogeBold = "hogeBold.otf"
}
UIFont(name: FontResource.hogeBold, size: 10)
テキスト
R.swift をはがし、さらに Localizable.strings から String Catalogs ( Localizable.xcstrings) に移行。
コード上のテキスト
Localizalbe.strings
// en
"Hoge.title" = "Hoge";
// ja
"Hoge.title" = "ほげ";
🟠 R.swift
R.string.localizable.hogeTitle()
🟢 R.swift はがし
String(localized: "Hoge.title", defaultValue: "Hoge")
日本語は String Catalogs で追加。
xib上のテキスト
ここからは R.swift は関係なし。
🟠 Localizable.strings
これまではEasy XIB and Storyboard Localizationの記事のやり方で xib上の xibLocKeyに Localizable.stringsのキーを設定する方法を使っていた。
🟢 String Catalogs を使う
ローカライズを行いたい .xibファイルを開く。
Show the File inspector > Localization > Localize... を選択。
Use String Catalog にチェックを入れて Localize を行う。
次のようなファイルが生成される。
HomeViewController (Strings) (HomeViewController.xcstrings) を開く。
HomeViewController.xib 内のテキストが自動で抽出されるので、ローカライズを行う。
自動抽出されたラベルのローカライズを行いたくない場合には、 Mark as "Don't Translate" を選択しておいた。
必要な箇所のみローカライズを行い ✅ 状態にする。
参考
String Catalogsの紹介 - WWDC23 - ビデオ - Apple Developer
String Catalogsの概要と妥当な運用方針
