2

我基本上是在使用下面的 drawGlyphsForGlyphRange 方法创建一个自定义属性来在我的文本子类 NSLayoutManager 中绘制一个圆角矩形。下面的工作就像一个跨越一条线的魅力。但是,当文本范围跨越两行时,我得到一个大矩形,它沿着这两行绘制属性。我想我应该在这里使用不同的方法,我尝试 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);

}

}

4

0 回答 0