Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-21-2010, 03:26 PM   #1 (permalink)
Registered Member
 
franium's Avatar
 
Join Date: Nov 2010
Posts: 68
franium is on a distinguished road
Default set the navigation bar title while user enters text (like in the Notes app)

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:
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
franium is offline   Reply With Quote
Old 12-21-2010, 04:03 PM   #2 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 97
JoeBlaze is on a distinguished road
Default

Try this code

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;
}
JoeBlaze is offline   Reply With Quote
Old 12-21-2010, 04:06 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 285
mickm is on a distinguished road
Default

Quote:
Originally Posted by franium View Post
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:
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
Code:
- (void)textViewDidChange:(UITextView *)textView {
    
    NSRange range = [textView.text rangeOfCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    if (range.location == NSNotFound) 
        self.navigationItem.title = textView.text;

}
that should do it...
mickm is offline   Reply With Quote
Old 12-21-2010, 04:07 PM   #4 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 97
JoeBlaze is on a distinguished road
Default

The code I posted can be copied outside of that method and placed directly in your
Code:
- (void)textViewDidChange:(UITextView *)textView
and you can skip using the newString variable and use textView.text. My intent on using
Code:
-(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.
JoeBlaze is offline   Reply With Quote
Old 12-22-2010, 05:11 AM   #5 (permalink)
Registered Member
 
franium's Avatar
 
Join Date: Nov 2010
Posts: 68
franium is on a distinguished road
Default

Quote:
Originally Posted by mickm View Post
Code:
- (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.
franium is offline   Reply With Quote
Old 12-22-2010, 05:14 AM   #6 (permalink)
Registered Member
 
franium's Avatar
 
Join Date: Nov 2010
Posts: 68
franium is on a distinguished road
Default

Quote:
Originally Posted by JoeBlaze View Post
Try this code

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.
franium is offline   Reply With Quote
Old 12-22-2010, 05:36 AM   #7 (permalink)
Registered Member
 
franium's Avatar
 
Join Date: Nov 2010
Posts: 68
franium is on a distinguished road
Default

Quote:
Originally Posted by JoeBlaze View Post
The code I posted can be copied outside of that method and placed directly in your
Code:
- (void)textViewDidChange:(UITextView *)textView
and you can skip using the newString variable and use textView.text. My intent on using
Code:
-(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
Code:
- (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.
franium is offline   Reply With Quote
Old 12-22-2010, 08:23 AM   #8 (permalink)
Registered Member
 
franium's Avatar
 
Join Date: Nov 2010
Posts: 68
franium is on a distinguished road
Default

Quote:
Originally Posted by franium View Post
Thanks, thanks, thanks
Using
Code:
- (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?
franium is offline   Reply With Quote
Old 12-22-2010, 10:01 AM   #9 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 97
JoeBlaze is on a distinguished road
Default

Quote:
Originally Posted by franium View Post
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)
Code:
- (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;
	}
}

Last edited by JoeBlaze; 12-22-2010 at 10:06 AM. Reason: Adding details
JoeBlaze is offline   Reply With Quote
Old 12-22-2010, 11:17 AM   #10 (permalink)
Registered Member
 
franium's Avatar
 
Join Date: Nov 2010
Posts: 68
franium is on a distinguished road
Thumbs up

Quote:
Originally Posted by JoeBlaze View Post
Trim characters in the newLineSet

Edit: (Adding example)
Code:
- (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;
	}
}
you're great... thank you very much
franium is offline   Reply With Quote
Reply

Bookmarks

Tags
dynamically, iphone, title, uinavigationbar, uitextview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 397
9 members and 388 guests
chemistry, daudrizek, HemiMG, jeroenkeij, Kirkout, PavelMik, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:00 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0