FK 0.1.7

FK 0.1.7

TestsTested
LangLanguage SwiftSwift
License Custom
ReleasedLast Release Nov 2015
SPMSupports SPM

Maintained by Daniel Dahan.



FK 0.1.7

FK

FocusKit

Text Matching

Easily match any regular expression pattern in a body of text. Below is an example of the default hashtag pattern matching.

FK

class NoteView: TextDelegate, TextViewDelegate {

    // ...

    lazy var text: Text = Text()
    var textView: TextView?

    // ...

    func prepareTextView() {
        let layoutManager: NSLayoutManager = NSLayoutManager()
        let textContainer = NSTextContainer(size: bounds.size)
        layoutManager.addTextContainer(textContainer)

        text.delegate = self
        text.textStorage.addLayoutManager(layoutManager)

        textView = TextView(frame: CGRectNull, textContainer: textContainer)
        textView?.delegate = self
        textView!.editable = true
        textView!.selectable = true
        textView!.font = UIFont.systemFontOfSize(16)
        textView!.text = note?["text"] as? String

        textView!.placeholderLabel = UILabel()
        textView!.placeholderLabel!.textColor = UIColor.grayColor()

        let attrText: NSMutableAttributedString = NSMutableAttributedString(string: "focus your #thoughts", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(16)])
        attrText.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(16), range: NSRange(location: 11, length: 9))
        textView!.placeholderLabel!.attributedText = attrText

        addSubview(textView!)
    }

    // ...

    func textWillProcessEdit(text: Text, textStorage: TextStorage, string: String, range: NSRange) {
        textStorage.removeAttribute(NSFontAttributeName, range: range)
        textStorage.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(16), range: range)
    }

    //...

    func textDidProcessEdit(text: Text, textStorage: TextStorage, string: String, result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) {
        textStorage.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(16), range: result!.range)
    }

    // ...

}

License

AGPL-3.0