在NSTextLayout上隐藏字形

时间:2014-03-19 10:26:07

标签: list ios7 uitextview glyph nslayoutmanager

我有一个带有自定义NSTextLayout的UITextView:

VPLayoutManager.h

#import <UIKit/UIKit.h>

@interface VPLayoutManager : NSLayoutManager <NSLayoutManagerDelegate>

@end

VPLayoutManager.m

#import "VPLayoutManager.h"

@implementation VPLayoutManager

- (id)init
{
    self = [super init];
    if (self) {
        self.delegate = self;
    }
    return self;
}

- (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin
{
    //[super drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];
    [self enumerateLineFragmentsForGlyphRange:glyphsToShow usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer *textContainer, NSRange glyphRange, BOOL *stop) {
        NSRange characterRange = [self characterRangeForGlyphRange:glyphRange actualGlyphRange:nil];
        NSRange paragraphRange = [self.textStorage.string paragraphRangeForRange:characterRange];
        if (characterRange.location == paragraphRange.location) {
            NSString *listCharacter = [self.textStorage.string substringWithRange:NSMakeRange(glyphRange.location, 1)];
            if ([listCharacter isEqualToString:@"\u2063"]) {
                CGRect listRect = CGRectOffset(CGRectMake(usedRect.origin.x - 10.0f, usedRect.origin.y, 40.0f, usedRect.size.height), origin.x, origin.y);
                NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:20.0f],
                                  NSForegroundColorAttributeName : [UIColor blackColor]};
                [@"-" drawInRect:listRect withAttributes:attributes];
                //[super drawGlyphsForGlyphRange:glyphRange atPoint:origin];
            }
            else {
                //[super drawGlyphsForGlyphRange:glyphRange atPoint:origin];
            }
        }
    }];
    [super drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];
}

- (NSUInteger)layoutManager:(NSLayoutManager *)layoutManager shouldGenerateGlyphs:(const CGGlyph *)glyphs properties:(const NSGlyphProperty *)props characterIndexes:(const NSUInteger *)charIndexes font:(UIFont *)aFont forGlyphRange:(NSRange)glyphRange
{
    if (glyphRange.length > 1) {
        NSGlyphProperty *properties = malloc(sizeof(NSGlyphProperty) * glyphRange.length);
        NSString *substring = [self.textStorage.string substringWithRange:glyphRange];
        for (NSUInteger i = 0; i < glyphRange.length; i++) {
            properties[i] = props[i];
        }

        NSRange regex = [substring rangeOfString:@"\u2063" options:NSRegularExpressionSearch];
        if (regex.location != NSNotFound) {
            for (NSUInteger i = regex.location; i < regex.length; i++) {
#warning crash on new-line if not at the end of the list
                properties[i] = NSGlyphPropertyControlCharacter;
            }
        }
        [layoutManager setGlyphs:glyphs properties:properties characterIndexes:charIndexes font:aFont forGlyphRange:glyphRange];
        return glyphRange.length;
    }
    [layoutManager setGlyphs:glyphs properties:props characterIndexes:charIndexes font:aFont forGlyphRange:glyphRange];
    return glyphRange.length;
}

- (NSControlCharacterAction)layoutManager:(NSLayoutManager *)layoutManager shouldUseAction:(NSControlCharacterAction)action forControlCharacterAtIndex:(NSUInteger)charIndex
{
    NSString *substring = [layoutManager.textStorage.string substringWithRange:NSMakeRange(charIndex, 1)];
    if ([substring isEqualToString:@"\u2063"]) {
        return NSControlCharacterZeroAdvancementAction;
    }
    return action;
}

@end

这段代码隐藏了字形,但是UITextView仍然看到字形(光标仍然是#34;显示&#34;即使现在看不见,该位置也有一个字形)...所以问题是:如何我是否完全隐藏了一个字形?

我试图根据段落开头的字符创建列表,想法是添加一个unicode来识别段落是否是列表的一部分,并在NSTextLayout中隐藏此unicode并执行正确的操作&#34;画&#34;清单。

0 个答案:

没有答案