在UITextView上使用属性字符串突出显示语法

时间:2014-06-08 08:15:11

标签: ios uitextview syntax-highlighting nsattributedstring

有一个很棒的项目可以为您的项目添加语法突出显示和行号:https://github.com/illyabusigin/CYRTextView

但它有一个关键问题:https://github.com/illyabusigin/CYRTextView/issues/16

github上的项目。任何人都可以帮忙解决这个问题吗?我试图分析代码,但还没找到。

1 个答案:

答案 0 :(得分:0)

此处描述并修复了此问题:iOS 7.1 UITextView still not scrolling to cursor/caret after new line

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define is_iOS7 SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
#define is_iOS8 SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")
@implementation CYRTextView {
    ...old code...
    BOOL settingText;
}

- (id)initWithFrame:(CGRect)frame {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextViewDidChangeNotification:) name:UITextViewTextDidChangeNotification object:self];
}

- (void)scrollToCaretInTextView:(UITextView *)textView animated:(BOOL)animated {
    CGRect rect = [textView caretRectForPosition:textView.selectedTextRange.end];
    rect.size.height += textView.textContainerInset.bottom;
    [textView scrollRectToVisible:rect animated:animated];
}

- (void)handleTextViewDidChangeNotification:(NSNotification *)notification {
    if (notification.object == self && is_iOS7 && !is_iOS8 && !settingText) {
        UITextView *textView = self;
        if ([textView.text hasSuffix:@"\n"]) {
            [CATransaction setCompletionBlock:^{
                [self scrollToCaretInTextView:textView animated:NO];
            }];
        } else {
            [self scrollToCaretInTextView:textView animated:NO];
        }
    }
}

- (void)setText:(NSString *)text {
    settingText = YES;
    ...old code...
    settingText = NO;
}