Typographizer Β· Smart Quotes for Swift Apps
Typographizer turns those pesky dumb quotes (""
/''
) and apostrophes ('
) into their beautiful, curly, localized counterparts. Because good typography uses smart quotes, not dumb quotes and we should not let the internet kill smart quotes. Speaking of smartness: Typographizer is smart enough to skip HTML tags and everything between certain tags (like <code>
and <pre>
).
Typographizer has a small footprint, was written in pure Swift, and has been tested on macOS, iOS, watchOS, and tvOS.
I started building Typographizer to typographize Wikipedia articles in my VΒ forΒ Wiki app.
Installation
Put the .swift
files into your appβs Xcode project.
How to Use
The easiest way to use Typographizer is the String extension (String+Typographizer.swift
):
var s = "This is a string with \"dumb\" quotes."
s = s.typographized(language: "en")
print(s) // This is a string with βdumbβ quotes.
Ignoring HTML Tags
If your string may contain HTML, set the isHTML
parameter to true
. Typographizer will then ignore the quotes inside tags and anything between <pre>
, <code>
, <var>
, <samp>
, <kbd>
, <math>
, <script>
, and <style>
tags:
var s = "This is a \"string\" with HTML. <code class="">print(\"hello world\")</code>"
s = s.typographized(language: "en", isHTML: true)
print(s) // This is a βstringβ with HTML. <code class="">print("hello world")</code>
Debug Mode
Activate the debug mode to highlight the characters that have been changedβTypographizer will add this tag around them: <span class="typographizer-debug typographizer-debug--XXX">
</span>
var s = "This is a string with \"dumb\" quotes."
s = s.typographized(language: "en", isHTML: true, debug: true)
print(s) // This is a string with <span class="typographizer-debug typographizer-debug--opening-double">β</span>dumb<span class="typographizer-debug typographizer-debug--closing-double">β</span> quotes.
(Yes, the class names are a little wordy, but thatβs on purpose.)
Use CSS to visualize the changes:
.typographizer-debug {
font-weight: bold;
}
.typographizer-debug--apostrophe {
color: red;
}
/* β¦ */
Measuring Performance
Pass measurePerformance: true
to log performance stats:
s = s.typographized(language: "en", isHTML: true, debug: false, measurePerformance: true)
Youβll see something like this in the Xcode console:
Typographizing took 0.00582303 seconds
Features
- Fixes double quotes:
""
βββ
(localized) - Fixes single quotes:
''
βββ
(localized) - Fixes apostrophes:
'
ββ
- Fixes hyphens that are used as en dashes:
β¦ - β¦
ββ¦ β β¦
- Demo app project for macOS
Supported Languages
Language Code | Double Quotes | Single Quotes | Comment |
---|---|---|---|
bs |
β β |
β β |
|
cs |
β β |
β β |
|
da |
β β |
β β |
|
de |
β β |
β β |
|
de_CH |
Β« Β» |
βΉ βΊ |
Swiss Standard German |
en |
β β |
β β |
|
et |
β β |
β β |
|
fi |
β β |
β β |
|
fr |
Β«\u{00A0} \u{00A0}Β» |
βΉ\u{00A0} \u{00A0}βΊ |
French Quotes are set with a non-breaking space (\u{00A0} ). A thin non-breaking space would be better, but itβs not supported in most browsers. |
hu |
β β |
β β |
|
is |
β β |
β β |
|
ja |
γ γ |
γ γ |
|
lt |
β β |
β β |
|
lv |
β β |
β β |
|
nl |
β β |
β β |
|
nn |
Β« Β» |
β β |
|
no |
Β« Β» |
β β |
|
pl |
β β |
β β |
|
ro |
β β |
β β |
|
ru |
Β« Β» |
β β |
|
sk |
β β |
β β |
|
sl |
β β |
β β |
|
sv |
β β |
β β |
Demo App
To get started, try the included demo app for macOS.
To Do
- Handle special cases like β80s, βTwas, Rock βnβ Roll, etc.
- Handle primes in coordinates properly:
52° 27'Β 20"Β N, 13° 19'Β 11"Β E β 52° 27β²Β 20β³Β N, 13° 19β²Β 11β³Β E - Add support for Hebrew
- If there is only one dumb single quote in a string, itβs probably an apostrophe
- Analyze HTML tags to verify correct quotes (opening and closing
<p>
tags make a good indicator for opening and closing quotation marks) - Track open/closed state while iterating over the text to make more informed decisions
- Add more typographic refinements (e.g. prime symbols, thin spaces)
Credits
Typographizer was created by Frank Rausch (@frankrausch).
Thanks to Tony Allevato for the great article on Strings, characters, and performance in Swiftβa deep dive.
Contribution Guidelines
Please always use self
when accessing methods and properties.
Preferred:
self.coolMethod()
Not preferred:
coolMethod()
License
The Typographizer source code is released under the MIT License. Please view the LICENSE file for details.
The Typographizer logo is Β© 2017 Frank Rausch; all rights reserved.