Theming the Code Editor

How’s your eyesight? How big is your screen? What’s the lighting like in your room? All of these factors will play into what you want source code to look like when you’re editing it.

Xcode lets you customize fonts, sizes, and colors, packaging up related settings as themes. Visit the themes interface by bringing up Xcode’s Preferences and selecting the “Fonts & Colors” tab. The theme editor is shown in the figure.

The left column lists the available themes, with the current theme highlighted. By default, you’ll get “Default”, which uses a white background, primary colors, and the font SF Mono Regular.

The right side of the editor shows the styling for every kind of syntax that Xcode knows how to style. You can choose one line and change the font, size, or color for just that kind of syntax, or multi-select with shift-clicking, command-clicking, or select-all (A) and change all lines at once.

images/editing/xcode-preferences-fonts-and-colors-basic.png

The right side of the pane also lets you set colors for non-text elements, like the background, cursor, selection, current line, and invisibles. It also lets you change the cursor appearance and spacing, and tabs at the top let you edit the appearance of the debugging console.

Joe asks:
Joe asks:
Can You Use a Variable-Width Font?

Since the theme editor uses the normal macOS font picker, you could choose a variable-width font for your code, rather than the traditional monospaced. Yes, even Comic Sans. Should you?

I ran an informal Twitter poll[4] and responses were overwhelmingly against the idea, but a handful of people say they already use a variable-width font for code.

The advantage is legibility: display fonts are designed to be easily read, after all. The disadvantage is all the places where fixed-width is assumed, such as formatting. In particular, multi-line statements won’t line up the way you expect: they don’t indent far enough in C or Swift, and are all over the place with Obj-C.

Also, if you share code with colleagues, having it look so different on your screen than everyone else’s could cause problems; a font with tight spacing like Gill Sans might tempt you to cram more on a line than you should.

Still, it’s not an inherently unreasonable practice. Maybe try it and see if you like it. After all, a tasteful font like Verdana, shown here, looks pretty good in Xcode:

images/editing/editor-verdana-font.png

Of the built-in themes, the primary choice is between a dark-on-white theme (Basic, Default, Low Key, Sunset) or a bright-on-black theme (Civic, Midnight). Beyond this, there are a few special-purpose themes. The most important is “Presentation”, which is basically Default at a much larger size, appropriate for showing on a projection system if you’re teaching or presenting to a room and want people in the back to be able to read your code. Presentation is also useful for screencasting or livestreaming, where the noise from video compression can make smaller type unreadable.

It’s easy to make your own themes. Duplicate one of the existing themes by clicking the plus (+) button at the bottom of the list on the left, give your new theme a name, and then edit its settings on the right. The “Inconsolable” theme in the earlier screenshot is one I made by duplicating the Default theme and simply changing the font to Inconsolata for every syntax type.

Of course, not all of us developers have a good design eye for this kind of thing, or the patience to make lots of fiddly little edits. Fortunately, the Xcode community has our back. Themes can be easily packaged and redistributed. Hugo Doria has a big collection of third-party Xcode themes, with screenshots, in a GitHub project.[5]

There are scripts on this page to install all the themes he has collected, and it’s also easy to just do it yourself. If it doesn’t already exist, create the directory ~/Library/Developer/Xcode/UserData/FontAndColorThemes/. Then download dvtcolortheme files and copy them to this folder. xccolortheme files work too, and if you edit an existing third-party theme, the edited version will be created as a new xccolortheme file. For example, here’s my themes folder:

 Ashe:~ cadamson$ ls -ltr ~/Library/Developer/Xcode/UserData/FontAndColorThemes/
 total 112
 -rw-r--r--@ 1 cadamson admin 5617 Mar 10 2013 Raspberry Sorbet.dvtcolortheme
 -rw-r--r--@ 1 cadamson admin 5675 Apr 22 2013 Rearden Steel.dvtcolortheme
 -rw-r--r-- 1 cadamson admin 5447 Oct 30 2013 Inconsolable.dvtcolortheme
 -rw-r--r-- 1 cadamson admin 5387 Mar 5 2014 Basic smaller.dvtcolortheme
 -rw-r--r-- 1 cadamson admin 6748 Jul 21 2015 Default Larger.dvtcolortheme
 -rw-r--r-- 1 cadamson admin 7395 Jul 25 21:00 Rearden Steel.xccolortheme
 -rw-r--r-- 1 cadamson admin 7209 Jul 30 14:56 Raspberry Sorbet.xccolortheme

You can see my home-made Inconsolable theme there from 2013, along with my edits to the third-party themes Raspberry Sorbet and Rearden Steel.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.116.20