UITextView 绘制不可见/空白字符 [英] UITextView Draw Invisible/Whitespace Characters

查看:40
本文介绍了UITextView 绘制不可见/空白字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让 UITextView 为制表符、空格和换行符绘制不可见的字符?

How would I make a UITextView draw invisible characters for tabs, spaces, and newline endings?

我认为它必须在 drawRect(CGRect) 方法或 Text Kit 中的布局管理器中处理.任何简单和/或直观的解决方案?

I figure it would have to be handled either in the drawRect(CGRect) method or by the layout manager in Text Kit. Any simple and/or intuitive solutions?

我只需要知道如何获取每个空白字符的 CGRect 以及哪种覆盖方法可以为每个空白字符无缝绘制图形?

I just need to know how to get the CGRect of each whitespace character and which method of override to seamlessly draw a graphic for each whitespace character?

提前感谢您的任何建议/帮助.
另外,我不介意你的答案是否在 Objective-c 中,尽管两种语言都是首选.

Thanks in advance for any advice/help.
Also, I don't mind if your answer in objective-c although both languages would be preferred.

推荐答案

我找到了比将 NSLayoutManagershowsInvisibleCharacters 属性设置为 true,通过子类化 NSLayoutManager 并覆盖方法 drawBackgroundForGlyphRange(NSRange, CGPoint),它允许为每个空白字符自定义绘图,例如:

I found a better solution than setting the showsInvisibleCharacters property of NSLayoutManager to true, by subclassing NSLayoutManager and overriding the method drawBackgroundForGlyphRange(NSRange, CGPoint), which allows for custom drawings for each whitespace character, for example:

class LayoutManager : NSLayoutManager {

    var text: String? { return textStorage?.string }

    var font: UIFont = UIFont.systemFontOfSize(UIFont.systemFontSize()) { 
        didSet { 
            guard let text = self.text else { return }
            let textRange = NSMakeRange(0, (text as NSString).length)                
            invalidateGlyphsForCharactersInRange(textRange, actualCharacterRange: nil)
            invalidateCharacterAttributesForCharactersInRange(textRange, actualCharacterRange: nil)
        }
    }

    override func drawBackgroundForGlyphRange(glyphsToShow: NSRange, atPoint origin: CGPoint) {

        super.drawBackgroundForGlyphRange(glyphsToShow, atPoint:origin)

        guard let text = self.text else { return }

        enumerateLineFragmentsForGlyphRange(glyphsToShow)
        { (rect: CGRect, usedRect: CGRect, textContainer: NSTextContainer, glyphRange: NSRange, stop: UnsafeMutablePointer<ObjCBool>) -> Void in

            let characterRange = self.characterRangeForGlyphRange(glyphRange, actualGlyphRange: nil)

            // Draw invisible tab space characters

            let line = (self.text as NSString).substringWithRange(characterRange)

            do {

                let expr = try NSRegularExpression(pattern: "\t", options: [])

                expr.enumerateMatchesInString(line, options: [.ReportProgress], range: line.range) 
                { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in

                    if let result = result {

                        let range = NSMakeRange(result.range.location + characterRange.location, result.range.length)
                        let characterRect = self.boundingRectForGlyphRange(range, inTextContainer: textContainer)

                        let symbol = "\u{21E5}"
                        let attrs = [NSFontAttributeName : Font]
                        let height = (symbol as NSString).sizeWithAttributes(attrs).height
                        symbol.drawInRect(CGRectOffset(characterRect, 1.0, height * 0.5, withAttributes: attrs)

                    }

                }

            } catch let error as NSError {
                print(error.localizedDescription)
            }

        }

    }

}

这篇关于UITextView 绘制不可见/空白字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆