4

How would I elegantly achieve multiline text that includes a combination of links, plain text, and formatted words? Right now, all I can think of is breaking each word into a token and displaying/positioning it differently (UILabel, UIButton, etc), however, this is terribly inefficient. Here is an example of what I am trying to achieve from the comments feature in Instagram.

Instagram comments

Notice, formatted link to user name, indented multiline text, and possibility of inline links to hashtags.

nmock
  • 1,907
  • 2
  • 20
  • 29
  • I'm looking to the answer to this question myself and so far the most helpful thing I can find is this - but I'm wondering if this is the most recent/relevant answer. Good Luck! – truthful_ness Sep 09 '12 at 12:34

2 Answers2

3

Edit Sep 14

So I was able to implement something very similar using FTCoreTextView It was simple to import/implement. I am using the code as a static library in my ARC project.

The following are the core components:

  • I use a method to set the style for my cell's FTCoreTextView

     (NSArray *)getCoreTextStyle{
        NSMutableArray *result = [NSMutableArray array];
    
        FTCoreTextStyle *defaultStyle = [FTCoreTextStyle new];
       defaultStyle.name = FTCoreTextTagDefault;    //though the default name is already set to FTCoreTextTagDefault
       defaultStyle.font = [UIFont fontWithName:@"Helvetica Neue" size:14.f];
       defaultStyle.textAlignment = FTCoreTextAlignementLeft;
       [result addObject:defaultStyle];
    
    
           FTCoreTextStyle *linkStyle = [defaultStyle copy];
       linkStyle.name = FTCoreTextTagLink;
       linkStyle.color = [UIColor blueColor];//[del colorWithHexString:@"3aa98"];
           linkStyle.font = [UIFont fontWithName:@"Helvetica Neue" size:14.f];
        [result addObject:linkStyle];
    
            return  result;
     }
    
  • Then in your cell you would set the text for the view, assign a delegate, etc. (My FTCoreTextView overrides a plain IBOutlet for a UIView)

    [myFTTView setDelegate:self];
    [myFTTView addStyles:[self getCoreTextStyle]];
    [myFTTView setText:[self parseLinks:text]];
    [myFTTView setNeedsDisplay];
    
  • Parse links is a method i separately define to search for hashtags and usernames and then set a style when they are found (this code is rough and leaves much to be improved. I am considering switching to CoreTextHyperlink which already has code search for hashtags and @usernames.)

    -(NSString *) parseLinks: (NSString *)cap{
       NSArray *split = [cap componentsSeparatedByString:@" "];
       NSMutableArray *words = [[NSMutableArray alloc] initWithArray:split];
    
          for (NSString *word in split){
    
             if([word hasPrefix:@"#"] || [word hasPrefix:@"@"]){
    
                NSString *reformat = [NSString stringWithFormat:@"%@%@|%@%@", hashlink,word, word, endhashlink];
                [words replaceObjectAtIndex:[split indexOfObject:word] withObject:reformat];
            }
    
        }
        return [words componentsJoinedByString:@" "];
    }
    
  • Last but not least, the method called after a tag/username is clicked

    - (void)coreTextView:(FTCoreTextView *)acoreTextView receivedTouchOnData:(NSDictionary *)data {
    
        NSString *key = [data objectForKey:@"url"];
        if([key hasPrefix:@"#"]){
           //do something for tag
        }
       else{
          //do something for user.
       }
    
       return;
    
       }
    
biegleux
  • 13,179
  • 11
  • 45
  • 52
  • i used FTcoretextView , and having a problem with it .. i want to get the height of coreTextview's text dynamically (which i added as a uiview in my storyboard tableview cell), when i try to access coreTextView (UIview in cell ) in heightForRowAtIndexPath : app crashes due to EXC bad access .. can you help me in how to get height of text view – YogiAR Sep 19 '13 at 13:24
1

Two ways I can think of:

  • use CoreText with NSAttributedStrings. Difficult to achieve, especially for the links (you'll have to register a listener somewhere, compute on which "link" you "clicked", ...), but provides the best performance.
  • use a UIWebView and write the HTML on-the-fly. Less performant, but much more flexible.
Cyrille
  • 25,014
  • 12
  • 67
  • 90