Create custom header file comments with Xcode Text Macros

In Xcode, you can create custom headers for your Swift files using text macros. Text macros allow you to automatically insert boilerplate code or comments into your files, which can include information such as the author, date, and other custom details.
Here’s how you can create and use a custom header with Xcode text macros in Swift:

Step 1: Create IDETemplateMacros.plist file

First, open your terminal and enter cd ~/Library/Developer/Xcode/UserData this directory. If you didn’t find any .plist macros file then create it using touch IDETemplateMacros.plist this command.

Step 2: Define Your Custom Header

Now, open IDETemplateMacros.plist file with Text file so that you can edit and customize your code.
First, you need to define the custom header template that you want to use. Here’s an example of what your custom header might look like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>FILEHEADER</key>
	<string>
/**
// Copyright (c) ___YEAR___ Joynal Abedin. All rights reserved.

/// Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
/// distribute, sublicense, create a derivative work, and/or sell copies of the
/// Software in any work that is designed, intended, or marketed for pedagogical or
/// instructional purposes related to programming, coding, application development,
/// or information technology.  Permission for such use, copying, modification,
/// merger, publication, distribution, sublicensing, creation of derivative works,
/// or sale is expressly withheld.

// This `___FILENAME___` file created by `___USERNAME___` on ___DATE___ AT ___TIME___.
// Using Swift `___DEFAULTTOOLCHAINSWIFTVERSION___`
// Running on iOS `___RUNNINGMACOSVERSION___`
- `Product`: ___PRODUCTNAME___
- `project`: ___PROJECTNAME___
- `Package`: ___PACKAGENAME___
*/
	</string>
	
</dict>
</plist>

Use Xcode’s Built-in Placeholders:

Xcode provides several built-in placeholders that you can use in your custom header. These placeholders will be automatically replaced with the appropriate values when you create a new file. Common placeholders include:

  1. ___COPYRIGHT___
  2. ___DATE___
  3. ___DEFAULTTOOLCHAINSWIFTVERSION___
  4. ___FILEBASENAME___
  5. ___FILEBASENAMEASIDENTIFIER___
  6. ___FILEHEADER___
  7. ___FILENAME___
  8. ___FULLUSERNAME___
  9. ___ORGANIZATIONNAME___
  10. ___PRODUCTNAME___
  11. ___PACKAGENAME___
  12. ___RUNNINGMACOSVERSION___
  13. ___TARGETNAME___
  14. ___TIME___
  15. ___YEAR___

    For more information you can visit this site: https://help.apple.com/xcode/mac/9.0/index.html?localePath=en.lproj#/dev7fe737ce0

Step 4: Final Result:

By following these steps, you can automate the inclusion of a custom header in your Swift files using Xcode text macros. This approach helps maintain consistency and saves time, especially in large projects.

6 thoughts on “Create custom header file comments with Xcode Text Macros

Leave a Reply

Your email address will not be published. Required fields are marked *