Tuesday, December 15, 2015

How to show HashTags in Comments or Post same as FB, Twitter and Instagram in iOS


How to ADD HasTag in TextView

UITextView * myTextView = [[UITextView alloc] init];
[myTextView setAttributedText:[self attributedMessageFromMessage: myTextView.text]];

NSArray * hashTagsArray = [NSArray arrayWithObjects:@"Apple"@"Google"@"Microsoft",@"Yahoo"@"Microsoft Google"@"Google Apple",  nil];

//Add Tap Gesture in TextView for gives action to HashTags
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:selfaction:@selector(messageTapped:)];
tap.numberOfTapsRequired = 1;
[myTextView addGestureRecognizer:tap];

//This method is used for showing color in HashTags according to its Array. Its only show on color on tags which is in array.
- (NSAttributedString *)attributedMessageFromMessage:(NSString *)message
{
    NSArray* messageWords = [message componentsSeparatedByString@" "];
    NSMutableAttributedString *attributedMessage = [[NSMutableAttributedString alloc]initWithString:@""];
    
    for (NSString *word in messageWords)
    {
        NSDictionary * attributes;
        if (word.length>0) {
            
        
        if([word characterAtIndex:0] == '@')
        {
            if ([hashTagsArray containsObject:[word substringFromIndex:1]])
            {
                attributes = @{NSForegroundColorAttributeName:[UIColor blueColor],
                               wordTypeuserNameKey,
                               userNameKey:[word substringFromIndex:1]};
            }
            else
            {
                attributes = @{NSForegroundColorAttributeName:[UIColor blackColor],wordTypenormalKey};
            }
            
        }
        else if([word characterAtIndex:0] == '*')
        {
           if ([hashTagsArray containsObject:[word substringFromIndex:1]])
            {
                attributes = @{NSForegroundColorAttributeName:[UIColor blueColor],
                               wordTypehashTagKey,
                               hashTagKey:[word substringFromIndex:1]};
            }
            else
            {
                attributes = @{NSForegroundColorAttributeName:[UIColor blackColor],wordTypenormalKey};
            }
            
        }
        
        else
        {
            attributes = @{NSForegroundColorAttributeName:[UIColor blackColor], wordType:normalKey};
        }
            
        NSAttributedString * subString = [[NSAttributedString alloc]
                                          initWithString:[NSString stringWithFormat:@"%@ ",word]
                                          attributes:attributes];
        [attributedMessage appendAttributedString:subString];
        }
    }

    return attributedMessage;
}

//This method is used to find which HashTag is tapped
- (IBAction)messageTapped:(UITapGestureRecognizer *)recognizer
{
    UITextView *textView = (UITextView *)recognizer.view;
    textView.selectable = YES;
    NSLayoutManager *layoutManager = textView.layoutManager;
    CGPoint location = [recognizer locationInView:textView];
    location.x -= textView.textContainerInset.left;
    location.y -= textView.textContainerInset.top;
    
    
    NSUInteger characterIndex;
    characterIndex = [layoutManager characterIndexForPoint:location
                                           inTextContainer:textView.textContainer
                  fractionOfDistanceBetweenInsertionPoints:NULL];
    
    textView.selectable = NO;
    
    if (characterIndex < textView.textStorage.length)
    {
        NSRange range;
        id type = [textView.attributedText attribute:wordType
                                             atIndex:characterIndex
                                      effectiveRange:&range];
        
        if([type isEqualToString:userNameKey])
        {
            NSString *userName = [textView.attributedText attribute:userNameKey
                                                            atIndex:characterIndex
                                                     effectiveRange:&range];
            [self hashTagBtnAction:userName];
        }
        else if([type isEqualToString:hashTagKey])
        {
            NSString *userName = [textView.attributedText attribute:hashTagKey
                                                            atIndex:characterIndex
                                                     effectiveRange:&range];
            [self hashTagBtnAction:userName];
        }
    }
}

//This method is used for gives action on HashTags.
-(void)hashTagBtnAction:(NSString *)str
{
     NSLog(@"You tap on %@ Tag", str);
     //code here......

}

No comments:

Post a Comment