UITextField placeholderRectForBounds - iphone

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.
}

Related

How to dismiss a keyboard from textview programmatically

Is there any way to hide or do not show the keyboard without calling resignFirstResponder in iOS? I know different method to hide keyboard, first one is to make the textview non editable, or using
- (BOOL)textFieldShouldEndEditing:(UITextView *)textview
{
[textview resignFirstResponder];
}
but in my case these method will not be works out, becz I just want to dismiss or hide the keyboard only, but need the editing in textview. I have a textview which contains some text, when I tap the sentence it need to be selected, I have done that selection part nicely, but when I write above keyboard dismissal method in my project, the selection of the tapped sentence doesn't appear,.is there any method to o this.
Textfield's selection will remain only when you are in editing mode. When you resign your first responder your selection disappears too.
To avoid bringing keypad in editing mode, you can change the input view. For example , look at the code,
textview.inputView = [[UIView alloc] init];
This brings a custom input view and it is not visible.
Add
self.textView.editable = NO;
Try this
NSRange selection = [yourTextView.text rangeOfString:yourTextView.text];
if( selection.location != NSNotFound ){ yourTextView.selectedRange = selection; }
It will remove the keyboard from the textview. Hope it helps.

UItextView arabic text aligned to right

Using my custom arabic keyboard on UItextView inputView, I m filling my textView with the arabic text but cannot get the written text align to right....Need help to align text to right.
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
if(showCustomKeyboard==NO){
[textView resignFirstResponder];
textView.inputView=nil;
[textView becomeFirstResponder];
return YES;
}
else{
[textView resignFirstResponder];
if(customKeyboard==nil){
customKeyboard=[[CustomKeyboard alloc] initWithFrame:CGRectMake(0, 264, 320, 216)];
[customKeyboard setDelegate:self];
}
if([[UIApplication sharedApplication] respondsToSelector:#selector(inputView)]){
if (textView.inputView == nil) {
textView.inputView = customKeyboard;
[textView becomeFirstResponder];
}
}
self.customKeyboard.currentField=textView;
[textView becomeFirstResponder];
}
return YES;
}
You can set the writing direction of a UITextView using the setBaseWritingDirection selector:
UITextView *someTextView = [[UITextView] alloc] init];
[someTextView setBaseWritingDirection:UITextWritingDirectionLeftToRight forRange:[someTextView textRangeFromPosition:[someTextView beginningOfDocument] toPosition:[someTextView endOfDocument]]];
The code is a little tricky because UITextView supports having different parts of the text with different writing directions. In my case, I used [someTextView textRangeFromPosition:[someTextView beginningOfDocument] toPosition:[someTextView endOfDocument]] to select the full text range of the UITextView. You can adjust that part if your needs are different.
You may also want to check whether the text in your UITextView is LTR to RTL. You can do that with this:
if ([someTextView baseWritingDirectionForPosition:[someTextView beginningOfDocument] inDirection:UITextStorageDirectionForward] == UITextWritingDirectionLeftToRight) {
// do something...
}
Note that I specified the start of the text using [someTextView beginningOfDocument] and searched forward using UITextStorageDirectionForward. Your needs might differ.
If you subclass UITextView replace all these code samples with "self" and not "someTextView", of course.
I recommend reading about the UITextInput protocol, to which UITextView conforms, at http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextInput_Protocol/Reference/Reference.html.
Warning about using the textAlignment property in iOS 5.1 or earlier: if you use it with this approach together with setting the base writing direction, you will have issues because RTL text when aligned left in a UITextView actually aligns to the right visually. Setting text with an RTL writing direction to align right will align it to the left of the UITextView.
Try textAlignment property.
textView.textAlignment = UITextAlignmentRight;
Take a look at UITextView Class Reference.
EDIT: Maybe CATextLayer can help you, someone suggests to use this class to customize text, but I've never used it personally...
Otherwise, you can force your textView to reverse your input in UITextFieldDelegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
The text field calls this method whenever the user types a new character in the text field or deletes an existing character.
Here you can replace your input with a new NSString where you put the characters from right to left.
Hope this makes sense...
Ah... Do not forget to set
textView.textAlignment = UITextAlignmentRight;
to move your prompt on the right.
Try this code:
yourtextview.textAlignment = UITextAlignmentRight;
Hope this helps you.
Something which no one mentioned here or on any other post is that make sure you have not called sizeToFit for TextView. It simple aligns the textView (not text) to the left which gives the illusion that text is left to right instead of right to left.
If you are creating UI from Storyboard, the set constraint to Lead or Trailing space and value of First Item will be Respect Language Direction
in swift you can use this code
textView.makeTextWritingDirectionRightToLeft(true)

Text shifted up in the textView while i focus on Textview second time

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.

How to pull up a keyboard - iphone app

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

disable keypad after entering text in textfield

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;
}