Clang Format
1,最近项目代码要求规范化,在网上找了个Xcode插件:Clang Format ,下载地址:https://github.com/travisjeffery/ClangFormat-Xcode
2,设置快捷键:系统偏好设置 -》键盘-》快捷键-》应用快捷键-》+
应用程序里选择:Xcode,菜单标题:Format Selected Files , 键盘快捷键选择:ctr + \ 应用程序里选择:Xcode,菜单标题:Format File in Focus, 键盘快捷键选择:car + cmd + \
3, 选择的格式不是我们想要的,和iOS的编码规范还是有一定差别的,自定义我们自己的格式吧
- 创建 _clang-format 文件,内容如下:
# 基础样式 BasedOnStyle: LLVM # 缩进宽度 IndentWidth: 4 # 圆括号的换行方式 BreakBeforeBraces: Attach # 支持一行的if AllowShortIfStatementsOnASingleLine: true # switch的case缩进 IndentCaseLabels: true # 针对OC的block的缩进宽度 ObjCBlockIndentWidth: 4 # 针对OC,属性名后加空格 ObjCSpaceAfterProperty: true # 每行字符的长度 ColumnLimit: 0 # 注释对齐 AlignTrailingComments: true # 括号后加空格 SpaceAfterCStyleCast: true # 不在小括号里加空格 SpacesInParentheses: false # 不在中括号里加空格 SpacesInSquareBrackets: false
- 将这个文件加到 .xcodeproj 包内容里,就OK了,或者是放到主目录也是OK的
附:常用的设置命令
AccessModifierOffset: 0 AlignEscapedNewlinesLeft: false AlignTrailingComments: true AllowAllParametersOfDeclarationOnNextLine: false AllowShortFunctionsOnASingleLine: false AllowShortIfStatementsOnASingleLine: false AllowShortLoopsOnASingleLine: false AlwaysBreakBeforeMultilineStrings: false AlwaysBreakTemplateDeclarations: false BinPackParameters: false BreakBeforeBinaryOperators: false BreakBeforeTernaryOperators: false BreakConstructorInitializersBeforeComma: false ColumnLimit: 128 BreakBeforeBraces: Attach CommentPragmas: '' ConstructorInitializerAllOnOneLineOrOnePerLine: false ConstructorInitializerIndentWidth: 0 ContinuationIndentWidth: 0 Cpp11BracedListStyle: false DerivePointerBinding: false IndentCaseLabels: true IndentFunctionDeclarationAfterType: false IndentWidth: 4 Language: Cpp MaxEmptyLinesToKeep: 1 NamespaceIndentation: None ObjCSpaceAfterProperty: true ObjCSpaceBeforeProtocolList: true ObjCBlockIndentWidth: 4 PenaltyBreakBeforeFirstCallParameter: 100 PenaltyBreakComment: 100 PenaltyBreakFirstLessLess: 0 PenaltyBreakString: 100 PenaltyExcessCharacter: 1 PenaltyReturnTypeOnItsOwnLine: 20 SpaceBeforeAssignmentOperators: true SpaceBeforeParens: ControlStatements SpaceInEmptyParentheses: false SpacesBeforeTrailingComments: 1 SpacesInAngles: false SpacesInCStyleCastParentheses: false SpacesInContainerLiterals: false SpacesInParentheses: false TabWidth: 4 UseTab: Never
如此设置过一次就可以了,别的项目就不需要如此设置