textkit自定义文本属性在跨越多行时绘制大矩形

时间:2013-12-19 18:53:17

标签: ios objective-c textkit

我基本上创建了一个自定义属性,在我的文本子类化NSLayoutManager中使用drawGlyphsForGlyphRange方法绘制一个圆角矩形。下面就像一个范围跨越一行的魅力。但是,当文本范围跨越两行时,我得到一个大矩形,沿着这两行绘制属性。我想我应该在这里使用不同的方法,我尝试使用nsbackgroundattribute来绘制突出显示但不幸的是我不能使用它来突出显示圆角矩形。

我会很感激任何指示。

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

NSTextStorage *textStorage = self.textStorage;
NSRange glyphRange = glyphsToShow;
while (glyphRange.length > 0) {
   NSRange charRange = [self characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL], attributeCharRange, attributeGlyphRange;
   id attribute = [textStorage attribute:IKSpecialHighlightAttributeName atIndex:charRange.location longestEffectiveRange:&attributeCharRange inRange:charRange];

    attributeGlyphRange = [self glyphRangeForCharacterRange:attributeCharRange actualCharacterRange:NULL];

    attributeGlyphRange = NSIntersectionRange(attributeGlyphRange, glyphRange);

    if( attribute != nil ) {



        NSTextContainer *textContainer = self.textContainers[0];

        CGRect boundingRect = [self boundingRectForGlyphRange:attributeGlyphRange inTextContainer:textContainer];





        [[UIColor colorWithRed:221.0/255.0 green:255.0/255.0 blue:0.0/255.0 alpha:1] setFill]; // set rounded rect's bg color



        boundingRect.origin.x += origin.x-3.0;

        boundingRect.origin.y += origin.y+3.0;

        boundingRect.size.width += 6.0;



        UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:boundingRect cornerRadius: 3.0];



        [roundedRect fillWithBlendMode: kCGBlendModeNormal alpha:1.0f];




        [super drawGlyphsForGlyphRange:attributeGlyphRange atPoint:origin];

    }

    else {

        [super drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];

    }

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

    glyphRange.location = NSMaxRange(attributeGlyphRange);

}

}

0 个答案:

没有答案