iOS: Text is not displayed inside of a rotated UITextField - ios

I have created a UITextField with Interface Builder. In viewDidLoad, I rotate the text field to match the landscape view we need:
name.transform = CGAffineTransformMakeRotation(-(M_PI/2));
Unfortunately, this does not bring the text with it. The text sits outside of the textfield, behind the background, as seen below.
Based on other questions here at StackOverflow, I have tried:
name.textAlignment = UITextAlignmentLeft;
name.textAlignment = UITextAlignmentRight;
and this additional function:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
name.textAlignment = UITextAlignmentLeft;
name.textAlignment = UITextAlignmentRight;
}
Neither solution works.
-----UPDATE-----
I was able to just orient the nib for landscape rather than portrait, and that solved the problem. However, this seems like a bug. I'd assume a rotated UITextField should bring the text with it.

Why are you rotating the text field manually? Just let your UIViewController do its normal rotation behaviour (which transforms the view controller's main view) and all the subviews will be usable in landscape.

Related

How do I hide the suggestion bar on iOS 15?

I have on OpenGL window that is also used for text input when a text element is clicked in our engine
#interface MyGLView : UIView <UIKeyInput, UITextInput, UITextInputTraits>
Whenever this view becomes first responder, it shows the suggestion bar above the keyboard. On many devices, this covers a very large portion of the screen and makes it hard to lay out the UI.
I have read that the following code is supposed to hide this suggestion bar, but nothing I change seems to have any affect
self.autocorrectionType = UITextAutocorrectionTypeNo;
self.inputAssistantItem.leadingBarButtonGroups = #[];
self.inputAssistantItem.trailingBarButtonGroups = #[];
I have tried putting this in the init for the view as well as in becomeFirstResponder method, but both don't seem to matter. What is the proper way to do this?
I think you're missing spellCheckingType!
This works for me:
self.autocorrectionType = UITextAutocorrectionTypeNo;
self.spellCheckingType = UITextSpellCheckingTypeNo;

Scroll textview when selecting text cover whole textview's screen area

In my application there is one UIScrollView having UITextView.
if the UITextView has 200 lines and user will select the first line and drag the last end point of selected text and user will drag that point to select more lines.
then, in a screen whatever the text seen, the user can select but the scroll view isn't automatic scrolling to select another text line.
may be it's quite confusing to understand but i don't know exactly what to ask for this question.
for understanding i am putting image here. in image that blue colored last end point will drag down then UITextView should scroll. that functionality i want in my application.
any types of idea, code,link will be great help...
EDIT :
The text view is fit to 200 lines.
means the text height is that much that 200 lines of text having.
so i have to scroll the scroll view and for that need any event will call on drag down the end point.
Write this And all the problem will resolve Bro ..... Do it
UITextView *lblNews=[[UITextView alloc]init];
lblNews.textColor = [UIColor blackColor];
lblNews.userInteractionEnabled=YES;
lblNews.scrollEnabled=YES;
lblNews.editable=NO;
lblNews.dataDetectorTypes = UIDataDetectorTypeAll;
lblNews.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0);
[self.view addSubView:lblNews];
It may be that UIScrollView is intercepting the touch when you drag your finger over it.
For a test, I would try setting the UIScrollView userInteractionEnabled = NO, and see if that allows the auto scrolling of the UITextView.
If it does then I would make a delegate for the UITextView which for delegate method textViewDidBeginEditing sets UIScrollView userInteractionEnabled = NO;
and for delegate method
textViewDidEndEditing sets UIScrollView userInteractionEnabled = YES;
similarly, for the UITextView you can try setting exclusiveTouch = YES and back to NO in the delegates, and see if that helps.

Setting the UITextView Text to the Beginning of the UITextView

I am using a UITextView and assigning some text in my code but for some reason it always starts after 5-6 lines. The UITextView is an AttributedTextView or allows Attributed strings.
self.bodyTextView.selectedRange = NSMakeRange(0, 0);
self.bodyTextView.font = [UIFont fontWithName:#"Cochin" size:17];
self.bodyTextView.text = #"This is written by Joe. This is written by Moo. This is written by Qoo";
To quote this answer:
A text view is a scroll view. iOS 7 will add a content offset automatically to scroll views, as it assumes they will want to scroll up behind the nav bar and title bar.
To prevent this, override the new method on UIViewController, automaticallyAdjustsScrollViewInsets, and return NO.
It deals with the Navigation bar overlapping the UIViewController's view's frame. When that is set to no then the UIViewController does not adjust anything to avoid the top navigation bar.
Instead of subclassing and overloading, you can just set the property to no.
(UIViewController).automaticallyAdjustsScrollViewInsets = NO;

Make UITextView parent be its own inputAccessoryView

I'm trying to achieve a similar keyboard interaction that Messages has in iOS 7. I have a UIView which contains a UITextView, and when the user selects it to start typing, I want to make this UIView the inputAccessoryView. This would take care of the animation for me, as well as the new UIScrollView keyboard dismiss interaction in iOS 7.
When the UITextView begins editing, I'm trying to set its inputAccessoryView to its parent UIView (which is already in the view hierarchy). The keyboard appears but not with an accessory view.
I've read some people are using a duo of UITextFields to make this work, but that seems like a bad way to achieve this.
Any suggestions?
A much easier solution is to make your input field the input accessory view of your view controller:
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (UIView *)inputAccessoryView
{
return self.yourInputField;
}
The view will be on screen at the bottom of the screen and when it becomes first responder in response to a user tapping it, the keyboard will be presented. The view will be animated such that it remains immediately above the keyboard.
The only way to get this to work is via a second text field. The idea is to make it a subview but not visible (due to crazy rect). You then switch firstResponder back and forth between it and the real text field while its getting delegate methods. I created a some one viewController test project and did this (you can copy paste and verify behavior with about 2 minutes of time):
#implementation ViewController
{
UITextField *field;
UITextField *dummyView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
field = [[UITextField alloc] initWithFrame:CGRectMake(0, 460, 320, 20)];
field.borderStyle = UITextBorderStyleRoundedRect;
field.delegate = self;
//field.inputAccessoryView = field;
field.text = #"FOO";
[self.view addSubview:field];
dummyView = [[UITextField alloc] initWithFrame:CGRectMake(0, 40000, 320, 20)];
dummyView.delegate = self;
[self.view addSubview:dummyView];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == field && textField.superview == self.view) {
[field removeFromSuperview];
dummyView.inputAccessoryView = field;
[dummyView becomeFirstResponder];
}
return YES;
}
#end
I should add I've used this technique in shipping apps since iOS 4.
EDIT: So a couple of other ideas:
1) To make the glitch when the keyboard starts moving look a little better, you could take a snapshot of your textView, put that into a UIImageView, and when you remove the textView from the primary view, replace it with the UIImageView. Now the appearance is the same. Add an animation for the image so that noting happens for 50 ms, then the alpha goes to 0. Add a similar animation to your real textview, so that it has an alpha of 0 for 50 ms, then it goes to 1. You may be able to tweak this so the transition is good (but not great).
2) The way apple probably does this is to get the animation curve and timing from the keyboard moving notification. In this case they would add a accessory view with 0 height at first, and animate the textField so its tracking the keyboard, but above it. Both moving same distance at the same time. At the end of the animation, the textField is pulled out of self.view, the accessory view has its frame changed to have the height of the textField, and the textField is placed as a subview of the accessory container view. This should work but yeah, its a bit complex to do. If you want someone to code it for you offer a 100 pt bounty. You still need the dummy text field for when you go and move the textField at the end, since when you take it out of its containing view it will resign first responder. So at the end, you make the dummy field the first responder, move the textfield, then make the real textfield the first responder again.
This actually works best if you don't use .inputAccessoryView at all and instead just animate the position of the parent UIView as the keyboard opens and closes. Here is an answer describing the process step-by-step with all the code.

Text in UITextView not display correctly, invisible, but occupied spaces

There is a UITextView in the view, the view controller is showing with Modal style. In my viewDidLoad method, I set the text of this UITextView, but, the text is not showing. Image below showing the error. Text color is black.
The weird thing is , when I long tap in the text view or tap [return] in keyboard, the text become visible. One thing I noticed is this error only occurred when the text I set is longer than the UITextView frame width, and the last word is not broken such as a long url.
I think the problem is maybe the word wrap not work correctly.
Thanks in advance.
Code like below:
UITextView *myTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, 520, 220)];
myTextView.textColor = [UIColor blackColor];
myTextView.font = [UIFont systemFontOfSize:20];
myTextView.layer.cornerRadius = 5;
myTextView.delegate = self;
myTextView.text = #"long text should some like http://stackoverflow.com/posts/11200726/edit";
[self.view addSubview:myTextView];
[myTextView release];
RESOVLED. In viewDidLoad method, add code below:
CGRect tempFrame = myTextView.frame;
[myTextView setFrame:CGRectZero];
[myTextView setFrame:tempFrame];
Subclass UITextView, to a class of your own, let's say MyUITextView:UITextView.
Initialize it offscreen with
[[MyUITextView alloc] initWithFrame:CGRectZero]
In MyUITextView override the method
-(void)willMoveToWindow:(UIWindow *)newWindow
and in it, set self.frame to the proper value.
this worked for me!
Are you sure the text color is black? Also make sure that the image is behind the textView in the view hierarchy. TextView handles Word wrap automatically.
I think in this case, the viewDidLoad is getting called before modelViewController is presented.
Set the text after modalViewController is presented.
If that also not working, then after presenting model, call a method to set the text explicitly.
Check your view hierarchy. You might have added another textView above that view.
Try changing the background color of textView. It must appear is UITextView is added there. And make sure you are not adding another view as a subview above your UITextView. If the textColor is black it must be visible, wordwrap is done by default automatically unless you change the property.Also try to check the attributes settings in XIB if added and plz show the code if adding programmatically.
I think problem in:
self.view
Try add this code line at top
self.view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
And if not will help, try remove this code line
myTextView.layer.cornerRadius = 5;

Resources