Skip to content

frootloops/ReversedFont

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ReversedFont

That code renders any font as reversed font.

How to use:

  1. 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)
    }
    
}
  1. 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)
    }()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages