在NSLayoutManager中控制自定义文本属性周围的间距

时间:2015-12-29 03:35:35

标签: ios cocoa-touch uitextview textkit nslayoutmanager

我有一个自定义的NSLayoutManager子类我正用它来绘制药丸形状的代币。我使用自定义属性(TokenAttribute)为子字符串绘制这些标记。我没有问题。

但是,我需要在我的TokenAttribute范围内添加一些“填充”(以便令牌的圆角矩形背景不会与文本相交)。

enter image description here

在上图中,我正在用橙色绘制我的令牌背景,但我想在469周围添加额外的填充,因此背景不会正好对着文本。

我不确定该怎么做。我尝试重写-boundingRectForGlyphRange:inTextContainer:以返回带有更多水平填充的边界矩形,但看起来字形的布局实际上并未受此影响。

如何围绕某些字形/字形范围提供更多间距?

这是我用来绘制背景的代码,在我的布局管理器子类中:

- (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin {

    NSTextStorage *textStorage = self.textStorage;
    NSRange glyphRange = glyphsToShow;

    while (glyphRange.length > 0) {

        NSRange characterRange = [self characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
        NSRange attributeCharacterRange;
        NSRange attributeGlyphRange;

        id attribute = [textStorage attribute:LAYScrubbableParameterAttributeName 
                                      atIndex:characterRange.location 
                        longestEffectiveRange:&attributeCharacterRange 
                                      inRange:characterRange];

        attributeGlyphRange = [self glyphRangeForCharacterRange:attributeCharacterRange 
                                           actualCharacterRange:NULL];
        attributeGlyphRange = NSIntersectionRange(attributeGlyphRange, glyphRange);

        if (attribute != nil) {
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSaveGState(context);

            UIColor *backgroundColor = [UIColor orangeColor];
            NSTextContainer *textContainer = self.textContainers[0];
            CGRect boundingRect = [self boundingRectForGlyphRange:attributeGlyphRange inTextContainer:textContainer];

            // Offset this bounding rect by the `origin` passed in above
            // `origin` is the origin of the text container!
            // if we don't do this, then bounding rect is incorrectly placed (too high, in my case).
            boundingRect.origin.x += origin.x;
            boundingRect.origin.y += origin.y;

            [backgroundColor setFill];
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:boundingRect cornerRadius:boundingRect.size.height / 2.0];
            [path fill];

            [super drawGlyphsForGlyphRange:attributeGlyphRange atPoint:origin];
            CGContextRestoreGState(context);

        } else {
            [super drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];
        }

        glyphRange.length = NSMaxRange(glyphRange) - NSMaxRange(attributeGlyphRange);
        glyphRange.location = NSMaxRange(attributeGlyphRange);
    }
}

1 个答案:

答案 0 :(得分:1)

NSLayoutManagerDelegate中定义了一些方法,这些方法可用作基于字形的自定义点。

使用

func layoutManager(_ layoutManager: NSLayoutManager, shouldGenerateGlyphs glyphs: UnsafePointer<CGGlyph>, properties props: UnsafePointer<NSLayoutManager.GlyphProperty>, characterIndexes charIndexes: UnsafePointer<Int>, font aFont: NSFont, forGlyphRange glyphRange: NSRange) -> Int

识别与关注范围周围的空白相关联的字形,并通过将它们在 props 数组中的值更改为 NSLayoutManager.GlyphProperty.controlCharacter 来标记它们。 然后将更改后的数组传递给

NSLayoutManager.setGlyphs(_:properties:characterIndexes:font:forGlyphRange:)

之后,您可以实施

func layoutManager(_ layoutManager: NSLayoutManager, shouldUse action: NSLayoutManager.ControlCharacterAction, forControlCharacterAt charIndex: Int) -> NSLayoutManager.ControlCharacterAction

再次标识感兴趣的字形并返回预定义的操作:

NSLayoutManager.ControlCharacterAction.whitespace

最后,这使您可以实现

func layoutManager(_ layoutManager: NSLayoutManager, boundingBoxForControlGlyphAt glyphIndex: Int, for textContainer: NSTextContainer, proposedLineFragment proposedRect: NSRect, glyphPosition: NSPoint, characterIndex charIndex: Int) -> NSRect

更改用于字形的边框。只需返回适当的尺寸。这将影响以下布局机制。

祝你好运!