HTMLString
is a library written in Swift that enables your app to escape and unescape HTML entities in Strings.
Main features | |
---|---|
๐ | Adds entities for ASCII and UTF-8/UTF-16 encodings |
๐ | Removes more than 2100 named entities (like & ) |
๐ข | Supports removing decimal and hexadecimal entities |
๐ฃ | Designed to support Swift Extended Grapheme Clusters (โ 100% emoji-proof) |
โ | Fully unit tested |
โก | Fast |
๐ | 100% documented |
๐ค | Compatible with Objective-C |
- iOS 8.0+
- macOS 10.10+
- tvOS 9.0+
- watchOS 2.0+
- Linux
Add this line to your Package.swift
:
.Package(url: "https://github.com/alexaubry/HTMLString", majorVersion: 3, minor: 0)
Add this line to your Podfile
:
pod 'HTMLString'
Add this line to your Cartfile:
github "alexaurby/HTMLString"
Copy the Sources/HTMLString/
directory into your project.
HTMLString
allows you to add and remove HTML entities from a String.
When a character is not supported into the specified encoding, the library replaces it with a decimal entitiy, such as &
<=> &
(compatible with HTML 4+).
You can choose between ASCII and Unicode escaping.
๐ก Pro Tip: When your content supports UTF-8 or UTF-16, use Unicode escaping as it is faster and yields a less bloated output.
import HTMLString
let emoji = "My favorite emoji is ๐"
let escapedEmoji = emoji.addingASCIIEntities // "My favorite emoji is 🙃"
let noNeedToEscapeThatEmoji = emoji.addingUnicodeEntities // "My favorite emoji is ๐"
let snack = "Fish & Chips"
let escapedSnack = snack.addingUnicodeEntities // "Fish & Chips"
To remove HTML entities from a String, use the removingHTMLEntities
property.
import HTMLString
let escapedEmoji = "My favorite emoji is 🙃"
let emoji = escapedEmoji.removingHTMLEntities // "My favorite emoji is ๐"
let escapedSnack = "Fish & Chips"
let snack = escapedSnack.removingHTMLEntities // "Fish & Chips"
With Obj-C Mix and Match, you can import and use the HTMLString
module from in Objective-C code.
The library introduces a set of Objective-C specific APIs as categories on the NSString
type:
[aString stringByAddingUnicodeEntities];
: Replaces every character incompatible with HTML Unicode encoding by a decimal HTML entitiy.[aString stringByAddingASCIIEntities];
: Replaces every character incompatible with HTML ASCII encoding by a decimal HTML entitiy.[aString stringByRemovingHTMLEntities];
: Replaces every HTML entity with the matching Unicode character.
@import HTMLString;
NSString *emoji = @"My favorite emoji is ๐";
NSString *escapedEmoji = [emoji stringByAddingASCIIEntities]; // "My favorite emoji is 🙃"
NSString *snack = @"Fish & Chips";
NSString *escapedSnack = [snack stringByAddingUnicodeEntities]; // "Fish & Chips"
@import HTMLString;
NSString *escapedEmoji = @"My favorite emoji is 🙃";
NSString *emoji = [escapedEmoji stringByRemovingHTMLEntities]; // "My favorite emoji is ๐"
NSString *escapedSnack = @"Fish & Chips";
NSString *snack = [escapedSnack stringByRemovingHTMLEntities]; // "Fish & Chips"
This library was inspired by @google's Toolbox for Mac.