I am new to iphone;
what i did is creating a 10 labels with corresponding text fields.
when i click on text field keypad is open.
But it covers the bottom 4 textfields.
I can't able to enter text to those fields.
i need when i click textfield in front of keypad screen moves up.
now i can able to enter in those textfields.
How can i done this.
Thank u in advance.
As Jumhyn wrote, make the UIViewController the delegate of all the text fields. Then assign the text fields tags from 0 to 9, from top to bottom. Then implement the following method in the controller:
- (void)textFieldDidEndEditing:(UITextField *)textField {
self.view.bounds = CGRectOffset(self.view.bounds, 0, textField.tag * 50);
[[self.view viewWithTag: textField.tag + 1] becomeFirstResponder];
}
The idea is that the first line shifts all the text fields up, depending on which text field has been edited, in the second line you activate the next text field. You will have to modify the code to better fit your layout, but it should give you the idea.
Make the UIViewController that handles the text views a UITextFieldDelegate:
#interface TextFieldViewController : UIViewController <UITextFieldDelegate> {
then in the textFieldShouldBeginEditing: method in the .m file, add the code that moves the text fields up. For example:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
myTextField3.frame = CGRectMake(myTextField3.frame.origin.x, myTextField3.frame.origin.y - /*whatever you want to subtract from the y value*/, myTextField3.frame.size.width, myTextField3.frame.size.height);
myTextField4.frame = CGRectMake(myTextField4.frame.origin.x, myTextField4.frame.origin.y - /*whatever you want to subtract from the y value*/, myTextField4.frame.size.width, myTextField4.frame.size.height);
myTextField5.frame = CGRectMake(myTextField5.frame.origin.x, myTextField5.frame.origin.y - /*whatever you want to subtract from the y value*/, myTextField5.frame.size.width, myTextField5.frame.size.height);
myTextField6.frame = CGRectMake(myTextField6.frame.origin.x, myTextField6.frame.origin.y - /*whatever you want to subtract from the y value*/, myTextField6.frame.size.width, myTextField6.frame.size.height);
return YES;
}
Related
I was wondering is there any way to get the point of the selected text in UITextView ?
Thanks!
For iOS5 and above : Now UITextField and UITextView conform to UITextInput protocol so it is possible :)
Selecting the last 5 characters before the caret would be like this:
//Get current selected range , this example assumes is an insertion point or empty selection
UITextRange *selectedRange = [textField selectedTextRange];
NSLog("Start: %d <> End: %d", selectedRange.start, selectedRange.end);
//Calculate the new position, - for left and + for right
UITextPosition *newPosition = [textField positionFromPosition:selectedRange.start offset:-5];
//Construct a new range using the object that adopts the UITextInput, our textfield
UITextRange *newRange = [textField textRangeFromPosition:newPosition toPosition:selectedRange.start];
Yes, first get the active selection like this: myTextView.selectedRange and then pass it to this method [myTextView firstRectForRange:range] This will give you the first bounding rectangle (which is the entire selection if it is one line, and the area of the selection on the first line if it is multi-line) as a CGRect.
I want to have a text-send view like the send-email view from the email app by apple. (the default iPhone emailapp).
I tried a UIScrollView with 2 TextFields and 1 TextView, but it looks not good, the scrollview has no function and the view does not scroll when I enter a text in the TextView (the text is hiding in the third line when I type...
Here is a picture:
http://s3.imgimg.de/uploads/Bildschirmfoto20110823um1156940f792556940f791456940f79png.png
It should look like this:
http://a5.mzstatic.com/us/r1000/032/Purple/ea/4f/03/mzl.bxracfen.320x480-75.jpg
How to rebuild?
to scroll downside when you start writing the text inside UITextfield, use this :
[yourScrollView scrollRectToVisible:CGRectMake(yourTextfield.frame.origin.x,yourTextField.frame.origin.y,yourScrollView.frame.size.width,yourScrollView.frame.size.height) animated:YES];
Also, when you edit your TextView (i.e in the textViewDidBeginEditing method), make its frame smaller so as to make it fully visible. Then, when you finish editing the text (i.e in the textViewDidEndEditing method) set the frame to whatever it was, originally.
EDIT:
//add this in .h file
NSInteger originalHeight;
//in .m file
- (void) textViewDidBeginEditing:(UITextView *)textView
{
[yourScrollView scrollRectToVisible:CGRectMake(yourTextView.frame.origin.x,yourTextView.frame.origin.y,yourScrollView.frame.size.width,yourScrollView.frame.size.height) animated:YES];
//now set the frame
//you just need to change the height, rest can be kept whatever they are
//set the newHeight so as to make the textview visible
originalHeight = yourTextView.frame.size.height;
yourTextView.frame = CGRectMake(x,y,width,newHeight);
//rest of the code
}
Now when editing is completed, set the height to as it was before.
- (void) textViewDidEndEditing:(UITextView *)textView
{
yourTextView.frame = CGRectMake(x,y,width,originalHeight);
}
I use Textview for insert message text but when i focus in textview second time after insert some text then already inserted text shifted upside. so text cut in the TextView.
Any solution for this problem?
UITextView the subclass of UIScrollView. So you can discard scroll to top like this:
((UIScrollView*)someTextView).delegate = self; // or some other UIScrollViewDelegate
and implement this method:
-(void) scrollViewDidScroll:(UIScrollView*) scrollView {
scrollView.contentOffset = CGPointMake(0, 0);
}
But if you do need scrolling during edit, you can add some boolean flags to distinguish the situations when user is scrolling and when the scrolling is automatic.
I've dealt with Keyboards with a UITextField but how can I use a Keyboard in the following way:
I have a UITableView
When the user selects a specific row, a keyboard pops up
I'd like to have a method that knows everytime a letter or something is pressed on the keyboard
Place text fields on the cells using the tableView:cellForRowAtIndexPath: method, and give the text field on each cell its own tag to identify it in the table view or text field delegate methods. Optionally, have a cell's text field become first responder when the cell is tapped in tableView:didSelectRowAtIndexPath: (outside of the text field's region).
To make it look like part of the cells, ensure your text fields don't have borders and match the cell background color.
You can hide a tiny (1x1) invisible/transparent text field in the corner somewhere (temporarily make it a subview in the table view cell or on top of the entire table view), make the text field first responder, and trap any character inputs by implementing the text should change delegate and inspecting the replacement text before it's entered.
const CGRect kbottomPickerViewFrame = {{0.0f, 480.0f},{320.0f, 298.0f}};
const CGRect ktopPickerViewFrame = {{0.0f, 480-298},{320.0,298.0f}};
-(void) animateViewUp:(BOOL)up {
[UIView animateWithDuration:.5 animations:^{
if(up) {
self.view.frame = ktopPickerViewFrame;
}else {
self.view.frame = kbottomPickerViewFrame;
}
} completion:nil];
}
use this method it will animate the view . Call it when textField begin editing
I want to create a UITextField with the placeholder aligned to left and the text is aligned to center. I search a bit and I found placeholderRectForBounds: function the apple documents says override if you want to but doesnt say how? I tried categorize the UITextField however It wasnt a success. any one has an idea?
thanks
At default set the text alignment as left.
When you start typing on the textfield this delegate will be called
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//here again set the alignment value to center
textField.textAlignment=UITextAlignmentCenter;
}
This delegate will be called when you focus out of textfield
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//Here check text in the textfield. if its nill set alignment as left.
//Becoz if there is no text in textfield placeholder will be shown.
}