stackiphone
stackiphone

Reputation: 1245

Is it possible to highlight a NSString or draw some color to a string

i am having this code to get the text between "." for example i am having lots of text like .1 this is first.2 this is second.3 this is fourth etc etc.when i tap the first ext it displays the first text in log .the code is

- (void)textViewDidBeginEditing:(UITextView *)textView
{

    [NSTimer scheduledTimerWithTimeInterval:0.001 target:maintextview   selector:@selector(resignFirstResponder) userInfo:nil repeats:NO];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    NSRange selectedRange = [textView selectedRange];
    NSString *backString = [maintextview.text substringToIndex:selectedRange.location];
    NSRange backRange = [backString rangeOfString:@"." options:NSBackwardsSearch];
    NSRange backRangee = [backString rangeOfString:@"." options:NSBackwardsSearch];
    int  myRangeLenght = backRangee.location - backRange.location;
    NSRange myStringRange = NSMakeRange (backRange.location, myRangeLenght);
    NSString *forwardString  = [maintextview.text substringFromIndex:backRange.location];
    NSLog(@"%@",[[forwardString componentsSeparatedByString:@"."] objectAtIndex:1]);

}

forwadString contains the tapped text,i just want to highlight this string or draw a color above this text using core graphics or something like that.is out possible? thanks in advance

Upvotes: 0

Views: 613

Answers (2)

Robotnik
Robotnik

Reputation: 3820

It's impossible to 'colour' an NSString, a string is just a representation of characters, it holds no font, colour or style properties. Instead you need to colour the UI element that draws the text to the screen.

If forwardString is in a UILabel or UITextView you can colour the text inside these by setting the textColor property. For example if you had a UILabel called lbl you could set the colour by using:

lbl.textColor = [UIColor redColor];

Upvotes: 0

CodaFi
CodaFi

Reputation: 43330

Much to my and many other's disappointment, Apple chose not to implement NSAttributedString until iOS 3.2, and even then, all standard UI elements are incapable of rendering them!

Luckily, the few, the proud, and the brave have answered the call and DTCoreText was born.

As for an actual selection, because UITextView conforms to UITextInput as of iOS 3.2, you can use and set the selectedTextRange.

Upvotes: 2

Related Questions