0

I need to change the text color on tap in UITextView. I got NSRange on selected text of text view but unable to change its color using this code.

[mutableAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:10.0/255.0 green:15.0/255.0 blue:5.0/255.0 alpha:1.0] range:range1];

Is there any way to change tap color change in text view?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
user2524618
  • 237
  • 1
  • 2
  • 15

2 Answers2

0

I hope, This code may help you :) In my scenario i am changing selected textColor by Tapping uiButton, You can try this way.

// When text is tapped or selected by user we can change the

- (IBAction)applyBlueColor:(id)sender {

    // getting textRange
    NSRange textRange = [_textView selectedRange];

    NSDictionary *attributeDictionary = [_textView.textStorage attributesAtIndex:textRange.location
                                                                    effectiveRange:nil];

    if ([attributeDictionary objectForKey:NSForegroundColorAttributeName] == nil ||
        [attributeDictionary objectForKey:NSForegroundColorAttributeName] != [UIColor blackColor]) {
        // Setting blue color to my selected text.
        NSDictionary *colorDictionary = @{NSForegroundColorAttributeName: [UIColor blueColor]};
        [_textView.textStorage beginEditing];
        [_textView.textStorage setAttributes:colorDictionary range:textRange];
        [_textView.textStorage endEditing];
    }

}
DevdDexter
  • 63
  • 8
  • thank you for your solution,i have done this thing but once color change when i am tap for second word to change color first word change blue to black,so any solution for it that first tap word is blue and second tap word is also blue in textview. – user2524618 Aug 14 '14 at 05:29
  • Okay :) Nice to see finally you got your answer: – DevdDexter Sep 01 '14 at 08:32
-1

hello every one i have post my answer. i hope it will help to other,enter code here

-(void)textTapped:(UITapGestureRecognizer *)recognizer
{

NSMutableAttributedString *attributedStringText = [[NSMutableAttributedString alloc]initWithString:txtView.text];

    UITextView *textView = (UITextView *)recognizer.view;
    NSLayoutManager *layoutManager = textView.layoutManager;
    CGPoint location = [recognizer locationInView:textView];
    location.x -= textView.textContainerInset.left;
    location.y -= textView.textContainerInset.top;

    // Get character Index.
    NSInteger characterIndex = [layoutManager characterIndexForPoint:location inTextContainer:textView.textContainer fractionOfDistanceBetweenInsertionPoints:NULL];

    if (characterIndex < textView.textStorage.length)
    {
       // Enumerate string and get word from character index.
        [txtView.text enumerateSubstringsInRange:NSMakeRange(0, textView.textStorage.length)options:NSStringEnumerationByWords usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                              if (NSLocationInRange(characterIndex, enclosingRange)) {
                                                  // Do your thing with the word, at range 'enclosingRange'

                                                  //[mutableAttributedString setTextColor:[UIColor redColor] range:NSMakeRange(range.location, [word1 length])];

                                                  // Change color of text.
                                                  //[attributedStringText addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:255.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:1.0] range:NSMakeRange(enclosingRange.location,[word1 length])];


                                                  NSLog(@"%@",textView.tintColor);
                                                  NSLog(@"%@",);

                                                  [attributedStringText addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor]  range:NSMakeRange(enclosingRange.location,[word1 length])];

                                                  [textView setAttributedText:attributedStringText];
                                                  *stop = YES;
                                                }
                                          }];
}
}
user2524618
  • 237
  • 1
  • 2
  • 15