That code renders any font as reversed font.
How to use:
- Create custom NSLayoutManager
class TransparentLayoutManager: NSLayoutManager {
override func showCGGlyphs(glyphs: UnsafePointer<CGGlyph>, positions: UnsafePointer<CGPoint>, count glyphCount: Int, font: UIFont, matrix textMatrix: CGAffineTransform, attributes: [String : AnyObject], inContext graphicsContext: CGContext) {
guard attributes[NSBackgroundColorAttributeName] != nil else {
super.showCGGlyphs(glyphs, positions: positions, count: glyphCount, font: font, matrix: textMatrix, attributes: attributes, inContext: graphicsContext)
return
}
CGContextSaveGState(graphicsContext)
CGContextSetBlendMode(graphicsContext, .DestinationOut)
super.showCGGlyphs(glyphs, positions: positions, count: glyphCount, font: font, matrix: textMatrix, attributes: attributes, inContext: graphicsContext)
CGContextRestoreGState(graphicsContext)
}
}
- Create a textview and use it
private let textView: UITextView = {
let textStorage = NSTextStorage()
let layoutManager = TransparentLayoutManager()
let container = NSTextContainer(size: CGSize.zero)
container.widthTracksTextView = true
container.heightTracksTextView = true
layoutManager.addTextContainer(container)
textStorage.addLayoutManager(layoutManager)
return UITextView(frame: CGRect.zero, textContainer: container)
}()