[XCode] 見づらいコードをショートカットキー一発で綺麗に整形する方法

xcode

コードの整形は手間がかかる

代入文って10行とか20行とかになると、一気に見づらくなりますよね。
特に、Interface Builder使わないでコードでUIを組み立てたりしてると、プロパティの設定処理で平気で100行になります。
例えばこんなコード・・

#!objectivec
cell.backgroundView.layer.shadowColor = [UIColor blackColor].CGColor;
cell.backgroundView.layer.shadowOffset = CGSizeMake(0, 4);
cell.backgroundView.layer.shadowOpacity = 0.8;
cell.backgroundView.layer.shadowRadius = 10;
cell.backgroundView.layer.masksToBounds = NO;
cell.backgroundView.layer.shouldRasterize = YES;
cell.backgroundView.layer.rasterizationScale = [UIScreen mainScreen].scale;
cell.backgroundView.layer.contentsScale = [[UIScreen mainScreen] scale];
cell.selectedBackgroundView.layer.shadowColor = [UIColor blackColor].CGColor;
cell.selectedBackgroundView.layer.shadowOffset = CGSizeMake(0, 4);
cell.selectedBackgroundView.layer.shadowOpacity = 0.8;
cell.selectedBackgroundView.layer.shadowRadius = 10;
cell.selectedBackgroundView.layer.masksToBounds = NO;
cell.selectedBackgroundView.layer.shouldRasterize = YES;
cell.selectedBackgroundView.layer.rasterizationScale = [UIScreen mainScreen].scale;
cell.selectedBackgroundView.layer.contentsScale = [[UIScreen mainScreen] scale];

はい、とっても汚いですね。理想は・・

#!objectivec
cell.backgroundView.layer.shadowColor                = [UIColor blackColor].CGColor;
cell.backgroundView.layer.shadowOffset               = CGSizeMake(0, 4);
cell.backgroundView.layer.shadowOpacity              = 0.8;
cell.backgroundView.layer.shadowRadius               = 10;
cell.backgroundView.layer.masksToBounds              = NO;
cell.backgroundView.layer.shouldRasterize            = YES;
cell.backgroundView.layer.rasterizationScale         = [UIScreen mainScreen].scale;
cell.backgroundView.layer.contentsScale              = [[UIScreen mainScreen] scale];
cell.selectedBackgroundView.layer.shadowColor        = [UIColor blackColor].CGColor;
cell.selectedBackgroundView.layer.shadowOffset       = CGSizeMake(0, 4);
cell.selectedBackgroundView.layer.shadowOpacity      = 0.8;
cell.selectedBackgroundView.layer.shadowRadius       = 10;
cell.selectedBackgroundView.layer.masksToBounds      = NO;
cell.selectedBackgroundView.layer.shouldRasterize    = YES;
cell.selectedBackgroundView.layer.rasterizationScale = [UIScreen mainScreen].scale;
cell.selectedBackgroundView.layer.contentsScale      = [[UIScreen mainScreen] scale];

非常に綺麗です。
でも、スペース打つの面倒くさい。。

そこで、上記のようなコードの整形をショートカットキー一発で出来るXCodeプラグインをご紹介します!

XCodeプラグイン「XAlign」が便利

XAlignは、文字通りAligningを手伝ってくれるプラグインです。

インストール方法

コマンド一発で即完了。

#!bash
$ curl github.so/XAlign/build/install.sh | sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   896  100   896    0     0   1805      0 --:--:-- --:--:-- --:--:--  1851
Password:
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 34311  100 34311    0     0  52104      0 --:--:-- --:--:-- --:--:-- 52543

XAlign is installed. Please Restart Your Xcode.

More info: https://github.com/qfish/XAlign/

To uninstall XAlign, `curl github.so/XAlign/build/uninstall.sh | sh`
.

XCodeを再起動しましょう。

整形の実行

以下のデモンストレーションのように、「シフト+?+X」で自動整形を実行します。

XAlign

すごいですねww 今までの苦労は一体ww
#defineプリプロセッサ文や、@property構文などにも有効なようです。

みなさんもぜひ試してみてください!

投稿者:

Takuya

Digital crafts(man|dog). Love photography. Always making otherwise sleeping. born in 1984.

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト /  変更 )

Google フォト

Google アカウントを使ってコメントしています。 ログアウト /  変更 )

Twitter 画像

Twitter アカウントを使ってコメントしています。 ログアウト /  変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト /  変更 )

%s と連携中