cocoa-touch How can I find selected text in UITextview to text among a NSString - cocoa-touch

How can I find selected text in UITextview among a NSString
//Declare
NSString* myText = #"This is the first row and This is the second row";
myUITextview.text = myText;
// we have the same results in here "This is the" and "row"
//Then check it when text in UITextview is selected
- (void)textViewDidChangeSelection:(UITextView *)textView
{
NSString *selectedText = [textView.text substringWithRange:[textView selectedRange]];
//selectedText "This is" from the second not from the first
}
is there anyway to recognise that? compare? check range or something?

In order to add text before and after the selected text in UITextView, you should do like this:
NSString *fullText = textView.text;
NSRange selectedRange = [textView selectedRange];
NSString *selectedText = [fullText substringWithRange:selectedRange];
NSString *addBeforeSel = #"abc";
NSString *addAfterSel = #"def";
NSString *replace = [NSString stringWithFormat:#"%#%#%#", addBeforeSel, selectedText, addAfterSel];
NSString *result = [fullText stringByReplacingCharactersInRange:selectedRange withString:replace];

Related

Search substring in a NSString

I am using a NSPopupbutton. I get the title of the selected item and put it in a NSString
NSString *selected = [mypopup titleOfSelectedItem];
at next i have a NSArray the content is loaded from an file.
Now i do a search like this:
for (NSString *line in mystringarray){
NSRange result = [line rangeOfString:selected];
if (result.location != NSNotFound){
NSLog(#"%#",not found);
}else{
NSLog(#"%#",line);
}
}
but this would not work. If i am changing the selected to #"myword" it works.

Message body text + label.text

I want to create a message that has a string text and also reference a label's text too.
I have this, but I'm not sure how to tie it together.
NSString *message = #"Lets meet here:"; _addressLabel.text;
You can use stringWithFormat:
NSString *message = [NSString stringWithFormat:#"Lets meet here: %#", _addressLabel.text];
The %# tells the method where to substitute the argument - you can even have multiple:
NSString *foo = #"foo";
NSString *bar = #"bar";
NSString *message = [NSString stringWithFormat:#"%# : %#", foo, bar];
will make message be foo : bar.

Space or tab then it should not be considered as a character

I am having text view in my app. I need to validate input of a text view. The rules are as below
Tab/Space cannot be accepted in the begining of the text entry
Tab/Space can be accepted in the middle and end of the text entry
Max characters accepted can be 256
How do I develop this, first character filteration logic ?
I have written following code in my app but it's still not giving me proper output ...Can anybody tell me where is the mistake???
-(void)textViewDidBeginEditing:(UITextView *)textView
{
NSString *rawString = [textView text];
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *trimmed = [rawString stringByTrimmingCharactersInSet:whitespace];
if ([trimmed length] == 0)
{
// Text was empty or only whitespace.
}
NSLog(#"length = %d",[trimmed length]);
[self.scrollView adjustOffsetToIdealIfNeeded];
}
Try this
NSString *string = [txtview text];;
NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(#"%d",trimmedString.length);

getting text from uilabel in objective-c

I have a project where I am trying to grab text from a uilabel that has been populated from a web service. I can grab and manipulate the text just fine but I need to send certain characters from the string to another web service call. I can not figure out how to grab the first, second and last characters in my string. I am both new to Objective-C as well as programming so any help would be much appreciated.
You can do it like this:
UILabel * l = [[UILabel alloc] init];
l.text = #"abcdef"; //set text to uilabel
[self.view addSubview:l];
NSString * text = l.text; //get text from uilabel
unichar first = [text characterAtIndex:0]; //get first char
unichar second = [text characterAtIndex:1];
unichar last = [text characterAtIndex:text.length -1];
If you need results as strings you can use:
NSString * firstAsString = [text substringWithRange:NSMakeRange(0, 1)]; //first character as string
or you can convert the unichar to string like this:
NSString * x = [NSString stringWithFormat:#"%C", last];
Per this it should be fairly easy:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UILabel_Class/Reference/UILabel.html
textLabel.text = #"Foo";
Where textLabel is instance of UILabel

Objective C - OSX NSInterger to NSString

Hi i am reading a book by aaron hillegass on cococa programming and I doing one of the mini tasks he asks us to do.
the task is to create an application that has one window open and has 1 input text field, a button and a label.
when a user inputs some text and presses the button, the label displays the text and the length of the text inputted.
Here is what I have got so far
//retrieve text from textfield
NSString *string = [textFieldInput stringValue];
//retrieve length of text and store in NSInteger called length
NSInteger length = [string length];
//store length in string format
NSString *string_length = [NSString stringWithFormat:#"%d", length];
//join strings
NSString *full_string = [string stringByAppendingString:(#"has ",string_length,#" characters")];
//set label text
[textField setStringValue:full_string];
however the actual string is shown and the characters string is shown, just not the string_length. any suggestions and am i going about this in the right way? Thanks.
NSString *fullString = [string stringByAppendingFormat:#"has %# characters", string_length];
//retrieve text from textfield
NSString *string = [textFieldInput stringValue];
NSString *fullString = [string stringByAppendingFormat:#" has %d characters", [string length]];
//set label text
[textField setStringValue:fullString];
Your usage of stringByAppendingString: is wrong.
If I understand you correctly, you are trying to pass a list of strings that should be appended to string but that method only takes a single string argument.
You can try the following:
NSString* fullString = [string stringByAppendingString:[NSString stringWithFormat:#"%# %# %#", #"has ", string_length, #" characters"]];