0

i am novice in iPhone.

I have a textView. I am changing the background Color of selected texts in text View.

But problem is that when I am selecting more than 1 line in textView ,only first line color is being changed , not other lines color.

so can anyone tell me about this how can I change background color of all texts which i m selecting.??

 tagValue = textView.tag;

    NSRange r = textView.selectedRange;
 UITextRange     *selectedRange = [textView selectedTextRange];

    if (!selectedRange)
        return;

    CGRect result1 = [textView firstRectForRange:selectedRange];
    frame_selectedText = result1;


self.str_selected =[NSString stringWithFormat:@"%@", [textView.text substringWithRange:NSMakeRange(r.location, r.length)]];
 UIButton  *btnView = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnView setFrame:result1];

 [btnView addTarget:self action:@selector(buttonColorClicked:) forControlEvents:UIControlEventTouchUpInside];
        btnView.backgroundColor = [UIColor colorWithRed:220.0f/255.0f green:248.0f/255.0f blue:188.0f/255.0f alpha:0.5];

    [textView addSubview:btnView];
Harikrishnan
  • 7,765
  • 13
  • 62
  • 113
Arun Kumar
  • 788
  • 5
  • 15

1 Answers1

1

The reason this is happening is that firstRectForRange will give you a rectangle that will not cover more than 1 line. As long as the text/selected text remains in one line, the rect will cover that.

Reason being: Imagine you select text that spans to a line and a half. So when you select the text, the selection color will show you that the selection boundary is not a rectangle. It is more like an inverted L. Hence, a single rect cannot cover it.

  • If you want to highlight just the selected text, you will have to use multiple rects. See my code here. I have covered multiple lines, and words with different rectangles. You can set a color and transparency (alpha) to give a feeling of highlighting. But the drawback here would be, you will not be able to interact with that text.

  • If you want to create a single rectangle that covers all of your selected text, then it will cover text succeeding the selected text, but you can work with a single rectangle. For this you will have to use firstRectForRange twice. Once on the first word selected, and second on the first word selected in the last line of selected text. The use MAX and MIN to create a single rect that covers all your text.

Alternate method UITextViews support AttributedTexts. With this you can set text of UITextView with a string with multiple attributes (bold, italic, colored text, colored background etc). Use NSMutableAttributedStrings to store your text. Add attributes like this:

[myAttriButedText addAttribute:NSBackgroundColorAttributeName value:UIColorFromRGB(0x333333) range:NSMakeRange(0, [myAttriButedText length])];

And set the text of UITextView using setAttributedText, or textView.attributedText =. This way, you easily add a background color on your text, without the hassles of all the above mentioned. But if you want more that just the attributes supported by NSAttributedStrings, you will have to use above mentioned methods.

Community
  • 1
  • 1
n00bProgrammer
  • 4,261
  • 3
  • 32
  • 60
  • hmm.. i try your code.. beacause i'v used NSMutableAttributedString , and problem is that , i dont know y, its nt calling its instance methods – Arun Kumar Oct 07 '13 at 13:15
  • Instance methods of `UITextView`? What happens? Does it crash, or you cannot call the instance methods of textView.text? – n00bProgrammer Oct 07 '13 at 13:21
  • And can u please tell me ??in your code what is 'words' in this line for (int i=0;i – Arun Kumar Oct 07 '13 at 13:26
  • No,instance method of NSMutableAttributedString , when i m creating its object and try to call its instance method , like addAttribute:NSBackgroundColorAttributeName value:UIColorFromRGB(0x333333) range:NSMakeRange(0, [myAttriButedText length])] , i get a warning about "this instance method is not existed .. " something like that – Arun Kumar Oct 07 '13 at 13:33
  • Yes. This is because UIColorFromRGB is a macro i defined in my code. My mistake not pointing it out before. Either use : `#define UIColorFromRGB(rgbValue) [UIColor \ colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \ green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \ blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]` or use a simple `UIColor` like `[UIColor blueColor]` – n00bProgrammer Oct 07 '13 at 18:40
  • Bro your code also not working. :( anyway thanks for helping. – Arun Kumar Oct 11 '13 at 08:29