Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

set the navigation bar title while user enters text (like in the Notes app)

franiumfranium Posts: 68Registered Users
edited December 2010 in iPhone SDK Development
Hi,
I have a textview and I would that while the user enters text, the first line of it becomes the navigation bar title.
I have this code:
- (void)textViewDidChange:(UITextView *)textView {
	self.navigationItem.title = textView.text;
}
In this way I see the title change dynamically with the text.
Only one more thing I'd like to have, that is stopping the update if the user enters a newline.
For example, if the user enters in the textview
"It's a title\nAnd then there are other words"
I'd like to see in the title only:
"It's a title"
while now there is something like this:
"It's a title And then there are..."

Any ideas?
Thanks,
Fran
Post edited by franium on

Replies

  • JoeBlazeJoeBlaze Posts: 97Registered Users
    edited December 2010
    Try this code
    -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
    {
    	NSMutableString *newString = [NSMutableString stringWithString:textView.text];
    	[newString replaceCharactersInRange:range withString:text];
    	NSRange newLineRange = [newString rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]];
    	
    	if(newLineRange.length > 0)
    	{
    		self.title = [newString substringToIndex:newLineRange.location];
    	}
    	else {
    		self.title = textView.text;
    	}
    	
    	return YES;
    }
    
  • mickmmickm Posts: 289Registered Users
    edited December 2010
    franium wrote: »
    Hi,
    I have a textview and I would that while the user enters text, the first line of it becomes the navigation bar title.
    I have this code:
    - (void)textViewDidChange:(UITextView *)textView {
    	self.navigationItem.title = textView.text;
    }
    
    In this way I see the title change dynamically with the text.
    Only one more thing I'd like to have, that is stopping the update if the user enters a newline.
    For example, if the user enters in the textview
    "It's a title\nAnd then there are other words"
    I'd like to see in the title only:
    "It's a title"
    while now there is something like this:
    "It's a title And then there are..."

    Any ideas?
    Thanks,
    Fran
    - (void)textViewDidChange:(UITextView *)textView {
        
        NSRange range = [textView.text rangeOfCharactersInSet:[NSCharacterSet newlineCharacterSet]];
        if (range.location == NSNotFound) 
            self.navigationItem.title = textView.text;
    
    }
    

    that should do it...
  • JoeBlazeJoeBlaze Posts: 97Registered Users
    edited December 2010
    The code I posted can be copied outside of that method and placed directly in your
    - (void)textViewDidChange:(UITextView *)textView
    
    and you can skip using the newString variable and use textView.text. My intent on using
    -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
    
    is so that you could check the range early and see if they modified any text in the first line but I just did not implement that.
  • franiumfranium Posts: 68Registered Users
    edited December 2010
    mickm wrote: »
    - (void)textViewDidChange:(UITextView *)textView {
        
        NSRange range = [textView.text rangeOfCharactersInSet:[NSCharacterSet newlineCharacterSet]];
        if (range.location == NSNotFound) 
            self.navigationItem.title = textView.text;
    
    }
    

    that should do it...


    It works, but if the first line changes the title doesn't.
  • franiumfranium Posts: 68Registered Users
    edited December 2010
    JoeBlaze wrote: »
    Try this code
    -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
    {
    	NSMutableString *newString = [NSMutableString stringWithString:textView.text];
    	[newString replaceCharactersInRange:range withString:text];
    	NSRange newLineRange = [newString rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]];
    	
    	if(newLineRange.length > 0)
    	{
    		self.title = [newString substringToIndex:newLineRange.location];
    	}
    	else {
    		self.title = textView.text;
    	}
    	
    	return YES;
    }
    

    Thanks.
    The only thing is that there is one char delay. I'll do some tries to fix this.
  • franiumfranium Posts: 68Registered Users
    edited December 2010
    JoeBlaze wrote: »
    The code I posted can be copied outside of that method and placed directly in your
    - (void)textViewDidChange:(UITextView *)textView
    
    and you can skip using the newString variable and use textView.text. My intent on using
    -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
    
    is so that you could check the range early and see if they modified any text in the first line but I just did not implement that.

    Thanks, thanks, thanks
    Using
    - (void)textViewDidChange:(UITextView *)textView {
    	NSRange newLineRange = [textView.text rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]];
    	
    	if (newLineRange.length > 0) {
    		self.navigationItem.title = [textView.text substringToIndex:newLineRange.location];
    	} else {
    		self.navigationItem.title = textView.text;
    	}
    }
    
    seems perfect.
  • franiumfranium Posts: 68Registered Users
    edited December 2010
    franium wrote: »
    Thanks, thanks, thanks
    Using
    - (void)textViewDidChange:(UITextView *)textView {
    	NSRange newLineRange = [textView.text rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]];
    	
    	if (newLineRange.length > 0) {
    		self.navigationItem.title = [textView.text substringToIndex:newLineRange.location];
    	} else {
    		self.navigationItem.title = textView.text;
    	}
    }
    
    seems perfect.

    There is only a problem if the user deletes the first line, in this way the title is empty. How could I manage this?
  • JoeBlazeJoeBlaze Posts: 97Registered Users
    edited December 2010
    franium wrote: »
    There is only a problem if the user deletes the first line, in this way the title is empty. How could I manage this?

    Trim characters in the newLineSet :)

    Edit: (Adding example)
    - (void)textViewDidChange:(UITextView *)textView {
    	NSString *trimmedValue = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet newLineCharacterSet]];
    	NSRange newLineRange = [trimmedValue rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]];
    	
    	if (newLineRange.length > 0) {
    		self.navigationItem.title = [trimmedValue substringToIndex:newLineRange.location];
    	} else {
    		self.navigationItem.title = textView.text;
    	}
    }
    
  • franiumfranium Posts: 68Registered Users
    edited December 2010
    JoeBlaze wrote: »
    Trim characters in the newLineSet :)

    Edit: (Adding example)
    - (void)textViewDidChange:(UITextView *)textView {
    	NSString *trimmedValue = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet newLineCharacterSet]];
    	NSRange newLineRange = [trimmedValue rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]];
    	
    	if (newLineRange.length > 0) {
    		self.navigationItem.title = [trimmedValue substringToIndex:newLineRange.location];
    	} else {
    		self.navigationItem.title = textView.text;
    	}
    }
    

    :eek: you're great... thank you very much :)
Sign In or Register to comment.