0

I'm trying to add syntax highlighting to my program.
I'm using this code to add highlights:

-(void)highlightWord:(NSString *)word: (UIColor *)color {
    int amount = textDisplay.text.length;
    NSString *newString = textDisplay.text;
    NSUInteger count = 0, length = amount;
    NSRange range = NSMakeRange(0, length); 
    while(range.location != NSNotFound)
    {
        range = [textDisplay.text rangeOfString: word options:NSLiteralSearch range:range];
        if(range.location != NSNotFound)
        {
            range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
            count++; 
        }
    }
    while (count != 0) {
        count--;
        NSRange highlight = [newString rangeOfString:word];
        newString = [textDisplay.text stringByReplacingCharactersInRange:highlight withString:@" "];
        UILabel *view1 = [[UILabel alloc] initWithFrame:[self frameOfTextRange:highlight inTextView:textDisplay]];
        view1.text = word;
        view1.textColor = color;
        view1.font = [UIFont fontWithName:@"System" size: 14.0];
        view1.backgroundColor = [UIColor clearColor];
        view1.textAlignment = UITextAlignmentLeft;
        [textDisplay addSubview:view1];
    }

}

- (IBAction)highlighString:(id)sender {

    for (UIView *subview in [textDisplay subviews]){
        if ([subview isKindOfClass:[UILabel class]]){
            [subview removeFromSuperview];
        }
    }

    [self highlightWord:@"test" :[UIColor blueColor]];
    [self highlightWord:@"this" :[UIColor redColor]];
    [self highlightWord:@"is" :[UIColor grayColor]];
    [self highlightWord:@"a" :[UIColor greenColor]];
}
@end

But this seems to cause a weird results:
...


The desired result would be that it overlays to colored label seamlessly.

Allison
  • 2,213
  • 4
  • 32
  • 56
  • Throwing a label (which may have a different font or attributes on that font) over a UITextView is always going to be clunky and/or risky. Why not use [a UITextView replacement that allows for attributed strings](http://stackoverflow.com/a/8961839/981049)? – Michael Dautermann Jul 22 '12 at 00:25
  • I'de go with the except I *need* copy and paste, but if all else fails I can use this. – Allison Jul 22 '12 at 00:29
  • Your method of doing this is too complex. Let me direct you to --> http://stackoverflow.com/questions/2218415/nstextview-syntax-highlighting Using Attributed Strings – Just a coder Jul 22 '12 at 00:30
  • And If you are worried about copying and pasting --> http://stackoverflow.com/questions/2581407/copy-nsattributedstring-to-pasteboard – Just a coder Jul 22 '12 at 00:33
  • So I could use your method and be able to cold text in **3** lines? Could you put how to do that in a answer? – Allison Jul 22 '12 at 00:37

1 Answers1

0

Mixing UILabel and UITextView is unlikely to give you easy copy and paste. It's an interesting idea, but I'd be surprised if it worked seamlessly. The usual Apple solution for this problem in all non-beta versions of iOS is to use a UIWebView, horrible as that is. Cocoanetics has one called DTRichTextEditor which I haven't reviewed, but based on the quality of their work on DTCoreText (which I'm very impressed with), I have high hopes for it for iOS 5. Betas are under NDA, but you may be interested in watching Session 220 from WWDC 2012 for more future-looking information.

Your specific bug is that you're adding the labels as subviews to the text view. You should put them on top of the text view as peers. Putting them inside the textview makes you subject the text view's drawing system, which is biting you. Of course when you put them on top of the text view, that's going to make it hard to handle copy and paste. You'll need to pass the touches through somehow, and then draw the selection correctly. See the previous paragraph.

If a fully editable rich-text view were easy to hack up with some labels, then you'd find several implementations on GitHub. Unfortunately it's actually pretty tough to do well. I bow to Cocoanetics for taking it on; I gave up pretty quickly. If you can simplify your problem to a non-general text view, then it may be possible this way, but I'd recommend investigating DTRichTextEditor or a UIWebView. See Session 511 "Rich Text Editing in Safari on iOS" from WWDC 2011.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610