How to display Swift code in your Medium article with syntax highlighting

Mark van Wijnen
2 min readAug 27

I like to write articles about Swift and the Apple frameworks and I’ve been doing so since 2020. The thing that annoyed me the most when writing those articles was that there was no easy way to insert code with syntax highlighting on Medium. So you had to use third-party solutions, such as Github gists, to accomplish that.

I choose another approach and used Carbon to create source code images. It allowed me to use my own color palette and made sure the source code was always fully visible on screen on mobile and desktop. The drawback, however, was that reader couldn’t easily copy the code.

Now we are at the end of 2023. I’ve been ignoring the plain Medium code block the whole time as it had no use to me and continued using source code images. Little did I know that as of November 10th, 2022 Medium introduced syntax highlighting in code blocks (Code blocks with syntax highlighting by Alex Benzer).

To help create more awareness of this great feature I decided to dedicated this article to it in the hopes more technical writers here on Medium will notice and hopefully are just as surprised and excited as I am right now.

How to add code blocks with highlighted syntax

Click on the + icon on the left hand side and then select the code symbol button {} to insert a new code block with highlighted syntax.

It does not know we want to present Swift code. So we need to click on Auto (Typescript) in the top left corner of the code block and choose Swift explicitly.

Alternatively, you can use shortcuts:

  • Mac: cmd+option+6
  • Windows : ctrl+alt+6
  • Linux : ctrl+alt+6

Thank you for reading!

  • If you enjoyed this, don’t forget to clap and please follow me on Medium
  • Support me and other Medium writers by signing up here
    It won’t cost you anything extra, but instead of Medium getting all the money I will receive a portion of the membership fees.
  • And if you really like to show your appreciation buy me a coffee it is a small amount but it’s a big gesture and you made someone really happy.
Mark van Wijnen

macOS/iPadOS/iOS/watchOS/visionOS developer and SwiftUI enthousiast. “Stay Hungry, Stay Foolish!” — Steve Jobs