Limit number of lines in UITextView - iphone

I'm using UITextView in my application, i want user to enter maximum 5 lines of text, I've been spending a few days on google but still have no luck, can some one please help!!
PS: Not limit number of characters but number of "LINES"
I've tried finding number of lines and use this method textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text , return NO when limit is reached but the back space button doesn't work after limit reached.
Thanks!

If the lines are not automatically wrapped you could use this:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:#"\n"]) {
rows++;
if(rows >= maxNumberOfLines){
//Exit textview
return NO;
}
}
return YES;
}
This should work, but it's not the best way to deal with it.
It would probably be better to use the size of the string and compare it to the contentsize of the textview to limit it.

You could try looking at the following UITextInput Protocol (which the UITextView conforms to) methods
- (NSInteger)characterOffsetOfPosition:(UITextPosition *)position withinRange:(UITextRange *)range
- (UITextRange *)characterRangeByExtendingPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction
- (CGRect)firstRectForRange:(UITextRange *)range
#property(nonatomic, readonly) UITextPosition *endOfDocument

Related

UITextView Detect Character Removed

I created a UITextView which allows dynamic tagging when the user types #.
Is it possible to detect a # removed, in order to stop my specific tagging process that I created? If so, how can I do that? UITextView has no such delegate methods.
I would use
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
Then create an NSString in which you store the last character added to the UITextView. Within the above method, add the following snippet.
if ([text isEqualToString:#""] && [lastRecordedString isEqualToString#"#"]) {
//do whatever you want to do when an # is deleted
}
lastRecordedString = [textView.text substringFromIndex:[string length]-1];
EDIT: see the edited code above. Now lastRecordedString is the last character in the UITextView after a character is removed. This should work.
You can get the original text which will be replaced by use:
NSString *originalText = [textView.text substringWithRange:range];
in your
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
method.
Then, do
if ([originalText rangeOfString:#"tag"].length > 0) {
NSLog(#"delete tag string");
}
You will detect if user delete a specific string.
Hope that helpful!

How to set UILabel's value according to UITextField as it is typed in iPhone?

I want to set UILabel according to the UITextField as it is typed. I mean if user want to type SAMPLE and he starts typing S then the lable should be set as S, then he types A label should also be A and so on. How to achieve this?
Please share suggestions.
Thanks in advance
Simplest way to do is to make a method and connect it with UiTextfield with event UIControlEventEditingChanged which will give you the trace on every character entered in the textfield.
[self.selectedTextField addTarget:self action:#selector(enterInLabel ) forControlEvents:UIControlEventEditingChanged];
-(void)enterInLabel
{
selectedLabel.text=selectedTextField.text;
}
The delegate also works for this as #brain said. The shouldChangeCharactersInRange: method can be a little confusing but the the following I think is pretty straight forward.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// myTextField delegate has been set
if ([textField isEqual:myTextField]){
NSMutableString *txt = [NSMutableString stringWithString:textField.text];
[txt replaceCharactersInRange:range withString:string]; //this is essentially how the textfield is updated after YES is returned
previewLabel.text = txt;
}
return YES;
}
The changing of the text field is actually done after the return YES. That is the whole point of this delegate. Just for an example, if you wanted to limit a textfield to 3 characters you could do the following to stop the text field from "replacing" the text.
if (range.location > 3)
return NO;
Set your textField delegate then call its method in your viewController.m file.. like this -
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
mylabel.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
return YES;
}
This will change the text after each increment of character.
OR You can add action on your TextField
See this link - UiTextField events
Your view controller will need to implement the UITextFieldDelegate which will allow it to receive changes to the text field. In the appropriate methods of the delegate you need to set the text of the UILabel.
I have never used UITextFieldDelegate so can't provide more detail on how to use it. I would mock up a quick example and just NSLog or debug the delegate calls to see that calls you get.

how to do this operation in UITextView (iphone)

In My textView there is a default value of "First name "
I want to implement such operation in it..
1) it should be shown "first name" firstly
2) when I start editing it should became nil
3)when I am complete with my editing if the field is still nil then it should be shown as "first name" again
can anyone help me with this?
//*This is now what I am doing now**//
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if (first_name.text=#"") {
first_name.text=#"First Name(Required)";
}
// Any new character added is passed in as the "text" parameter
if ([text isEqualToString:#"\n"]) {
// Be sure to test for equality using the "isEqualToString" message
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return FALSE;
}
// For any other character return TRUE so that the text gets added to the view
return TRUE;
return TRUE;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
NSLog(#"textViewShouldBeginEditing");
textView.scrollEnabled=NO;
if (textView == first_name) {
first_name.text=nil;
}
return YES;
}
All the delegate methods you can use to implement this concept can be found in the UITextViewDelegate Protocol Reference.
You need to overlay a UILabel on top of the text view with the text you want, set its userInteractionEnabled to NO and on the text field delegate method shouldBeginEditing you hide the UILabel before returning YES and unhide it on shouldFinishEditing.

How to prevent user to editing from given range in iPhone textView

I am currently worked on simple textview application in which I need prevent user to write data in textView & after preventing he may be change other value of textview. Below is my code for prevernting user to write into textview.
- (void)textViewDidChange:(UITextView *)textView
{
NSString *textValue=textView.text;
NSInteger intLen=[textValue length];
if(intlen>280){
textView.editable=No;
}
else
{
textView.editable=YES;
if(intLen <=140)
{
intLen=140-intLen;
intSMSValue=1;
lblSmscounter.text=[[NSNumber numberWithInt:intSMSValue]stringValue];
lblCountChar.text=[[NSNumber numberWithInt:intLen] stringValue];
}
if((intLen>140)&&(intLen <=280))
{
intLen=280-intLen;
intSMSValue=2;
lblSmscounter.text=[[NSNumber numberWithInt:intSMSValue]stringValue];
lblCountChar.text=[[NSNumber numberWithInt:intLen] stringValue];
}
}
}
But in my code I am prevent user to write more than 280 bt user caneditable the previous text.
Try delegate method
(BOOL)textView:(UITextView )textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString)text{
}
If the range of editable text is ok, return yes.

How to dissallow certain characters in a UITextview

I have a uitextview that is editable but there are certain characters I would like to be disallowed from being typed.
How can I do that?
You can do this by assigning a delegate to the UITextView, and implementing the following method in the delegate:
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
In the body just write some code that scans through the input text to see if you find the characters you want to filter, if you see them return NO, otherwise return YES.
Unfortunately, this is not that simple, because textView:shouldChangeTextInRange: replacementText: is not necessarilly called with one-character strings. It is for keyboard input, but it isn't when pasting, or when using speech recognition to enter text.
So what do you want to do if the user pastes (or dictates) a string that contains forbidden characters? You might want to let all valid text go through and only delete (or replace) unwanted characters.
The incorrect idea would be to fix the text in the textViewDidChange: delegate routine. While this seems to work, it somehow prevents speech input from working in the UITextView.
The correct idea is to implement textView:shouldChangeTextInRange: replacementText: with full filtering. Here is a sample implementation that filters out newlines:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:#"\n"]) {
[textView resignFirstResponder]; // dismiss keyboard
return NO;
}
text = [text stringByReplacingOccurrencesOfString:#"\n" withString:#" "]; // replace by spaces
NSString *fullText = [textView.text stringByReplacingCharactersInRange:range withString:text];
textView.text = fullText;
return NO;
}
Note that it dismisses the keyboard when the user strikes the enter key. But not quite perfectly: it will also dismiss the keyboard when the user pastes a single newline character. This might be a problem, but this should happen only rarely.