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 03-04-2009, 05:54 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 29
bint is on a distinguished road
Default UITextView - scroll while editing?

Hello,

I've got a big UITextView, nearly full screen. If I tap it, the keyboard pops up and I can edit the text. However, the longer I type, eventually the text runs down the view, behind the keyboard. I can no longer see what I'm typing.

How do you deal with this? Do you have to track the cursor position and scroll the view manually?

Thanks!
B
bint is offline   Reply With Quote
Old 03-31-2009, 09:30 AM   #2 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 2
Tedeks is on a distinguished road
Default

Quote:
Originally Posted by bint View Post
Hello,

I've got a big UITextView, nearly full screen. If I tap it, the keyboard pops up and I can edit the text. However, the longer I type, eventually the text runs down the view, behind the keyboard. I can no longer see what I'm typing.

How do you deal with this? Do you have to track the cursor position and scroll the view manually?

Thanks!
B
Has anyone solved this? Running into the same problem.
Tedeks is offline   Reply With Quote
Old 03-31-2009, 09:38 AM   #3 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

Search the forum for keyboard handling. Basically, you want to shift your textview up so it's not hidden by the keyboard at all. No point in it remaining full screen behind the keyboard.

Here's some code I used to accomplish this:

Code:
// the amount of vertical shift upwards keep the Notes text view visible as the keyboard appears
#define kOFFSET_FOR_KEYBOARD					140.0

// the duration of the animation for the view shift
#define kVerticalOffsetAnimationDuration		0.50

- (IBAction)textFieldDoneEditing:(id)sender
{
	[sender resignFirstResponder];
}

- (IBAction)backgroundClick:(id)sender
{
	[latitudeField resignFirstResponder];
	[longitudeField resignFirstResponder];
	[notesField resignFirstResponder];
	
	if (viewShifted)
	{
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:kVerticalOffsetAnimationDuration];

		CGRect rect = self.view.frame;
		rect.origin.y += kOFFSET_FOR_KEYBOARD;
		rect.size.height -= kOFFSET_FOR_KEYBOARD;
		self.view.frame = rect;

		[UIView commitAnimations];
		
		viewShifted = FALSE;
	}		
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
	if (!viewShifted) {		// don't shift if it's already shifted
		
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:kVerticalOffsetAnimationDuration];

		CGRect rect = self.view.frame;		
		rect.origin.y -= kOFFSET_FOR_KEYBOARD;
		rect.size.height += kOFFSET_FOR_KEYBOARD;
		self.view.frame = rect;

		[UIView commitAnimations];
		
		viewShifted = TRUE;
	}
	return YES;
}
It's not really the best way to do it, but it works. One of the big problems you'll see in a lot of the published solutions (to keyboard handling) is that they don't account for multiline text fields, and assume that you can hook the return button to hide the keyboard and restore the text view.

joe
FlyingDiver is offline   Reply With Quote
Old 03-31-2009, 09:41 AM   #4 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 2
Tedeks is on a distinguished road
Default

I'm looking to scroll a text view when there is a link break. I've got the code to scroll, just don't have a way to capture the line breaks.
Tedeks is offline   Reply With Quote
Old 07-07-2009, 07:01 PM   #5 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 1
Vengeance is on a distinguished road
Default

Quote:
Originally Posted by Tedeks View Post
I'm looking to scroll a text view when there is a link break. I've got the code to scroll, just don't have a way to capture the line breaks.
I found this code on another site and it looks like it would do what you need:

Code:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
 replacementText:(NSString *)text
{
    // Any new character added is passed in as the "text" parameter
    if ([text isEqualToString:@"\n"]) {
        // Do your scrolling stuff here in response to the "Enter" pressed
		
        // Return FALSE so that the final '\n' character doesn't get added
        return TRUE;
    }
    // For any other character return TRUE so that the text gets added to the view
    return TRUE;
}
Hope it helps.
Vengeance is offline   Reply With Quote
Old 07-17-2009, 12:48 PM   #6 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 50
rnieves is on a distinguished road
Default

Resize your UITextViews frame when editing begins so that it will automatically scroll when you start approaching the keyboard.

Do this on:
- (BOOL)textViewShouldBeginEditingUITextView *)textView


This is what I do and it is pretty simple and straightforward.

Resize it back to the normal on:
- (BOOL)textViewShouldEndEditingUITextView *)textView;
rnieves is offline   Reply With Quote
Old 08-07-2010, 08:17 PM   #7 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 1
sd-0 is on a distinguished road
Default View shifting and Keyboard handling

Go to this developer article provided by Apple and it will tell exactly what to do.

Last edited by sd-0; 08-07-2010 at 08:18 PM. Reason: Forgot to post link
sd-0 is offline   Reply With Quote
Reply

Bookmarks

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: 338
2 members and 336 guests
guusleijsten, LEARN2MAKE
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

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