UITextField horizontal text alignment - text not centered - ios

I have a very strange bug trying to horizontally center text in UITextField object in iOS 7+ app.
I have set up a fresh project to isolate the bug and to make sure I'm not doing something wrong.
In viewDidAppear: method I alloc/init a new UITextField instance like this:
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(60.0f, 100.0f, 200.0f, 200.0f)];
tf.backgroundColor = [UIColor orangeColor];
tf.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:tf];
That's all. When I run this sample app on iPhone Retina (4-inch 64-bit) simulator I get following result:
After typing in some letters I get this:
Do anyone know what's is happening? The text should be centered while typing.
I didn't come across this bug till now. It is iOS related or am I doing something wrong?
P.s.: It is happening on iDevices also.
Thanks in advance.
EDIT:
Adding:
tf.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
doesn't help either.
EDIT 2:
Like Inder Kumar Rathore suggested I tried to set a height to 20.0f like so:
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(60.0f, 100.0f, 200.0f, 20.0f)];
and it surprisingly works. But it doens't solve the issue. I discovered that increasing height from 20.0f up makes issue only worse. I think it has something to do with line-height/text field height ratio. I don't know..

I think UITextField expects the vertical size of the font to be equivalent to the height of the text field. If you imagine a font at a point size that will fill the vertical height of your text field at 200 points, you will only be able to type a few characters before it fills the horizontal width and switches from the centered behavior to the right-aligned behavior. This seems to be the behavior in your "bug" above, where you can only type a few characters before it switches behaviors.
I think the best thing to do is to make your text field just high enough to fit your font, and vertically center it in a containing UIView.

Related

UISearchBar placeholder text color/location

However, I've applied different patches still I'm not able to fix placeholder text position and text color for UISearchbar.
But when I type something, it's showing up at a proper place.
What's the reason?
This is how you can change the colour of your placeholder text:
UITextField *searchField = [self.searchBar valueForKey:#"searchField"];
searchField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Some Text"];
UILabel *placeholderLabel = [searchField valueForKey:#"placeholderLabel"];
placeholderLabel.textColor = [UIColor blueColor];
As for the position, by default, placeholder text is vertically centre positioned in the UISearchBar. From your screenshot, it appears to me that there are few new line characters in the end of the text.
there's a category created on UITextField, inside it, the placeholder text is drawing. I just corrected the frame and now everything works fine, even that solve the color issue as well.

iOS7 app and how to do app-wide font adjustments from within app (not in Settings)

I have a relatively small native app that and would like to have a 3-button control that can set the font size application wide. I'd like it to be able to support clicking the different unfilled-in size and have it automatically rerender the current view with the new font size and all subsequent views. Similar to having a control where the filled-in size is the current size and the clear versions would be buttons that could be selected:
It would seem like I want to pogramaticaslly have Dynamic Type but I'm not sure if there is an easier way. Reading through the different Text technologies really hasn't provided much help.
My views use a combination of UILabels and UITextViews with NSAttributedString's and NSString's. What would be the best option to manage font size programatically?
thx for any help
edit 1
Here's a label that I would like to add; can I just update the _mainFont variable which is used in the subviews and tell those subviews to update their rendering?
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 100.0f, 20.0f)];
myLabel.backgroundColor = [UIColor blueColor];
[myLabel setUserInteractionEnabled:YES];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction)];
[recognizer setNumberOfTapsRequired:1];
//lblName.userInteractionEnabled = true; (setting this in Interface Builder)
[myLabel addGestureRecognizer:recognizer];
[tempScrollView addSubview:myLabel];
}
- (void)tapAction {
NSLog(#"Tap action");
_mainFont = MYLargerMainFont();
[self.view setNeedsDisplay];
//[self.view.subviews setNeedsDisplay];
}
The answers in the questions below might give you some ideas:
How to set a custom font for entire iOS app without specifying size
Set a default font for whole iOS app?
Is there a way to change default font for your application
How to set custom font family as system font in ios application
Just like Dynamic Type, it seems the best approach would be to fire a notification when the change happens, and have view controllers listen to this change and set the fonts. If you want something more robust, you could create subclasses of the UI elements you have and have them listen to font size change notifications, and update themselves accordingly. I like the former approach, because it is the view controller that should ultimately have control over the views.

How to create and add UIButton with multiline label from code using autolayout?

There's are a lot of links how to size a label, however there's a lack of info how to resize UIButton according to its label size. Currently, I have screen view set with autolayout (all the system constraints are added from IB) and everything works fine, but need to add dynamic content at the bottom of the screen. I need to create and add random number of buttons with random length titles. So, here's code fragment for creating and adding the buttons:
// Loop
UIButton *myButton = [UIButton new];
UIButton.frame = CGRectMake(0, previousButtonOriginY, self.view.bounds.size.width, 20);
myButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
myButton.titleLabel.textAlignment = NSTextAlignmentLeft;
myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[myButton setTitle:#"very long title..." forState:UIControlStateNormal];
myButton.titleLabel.numberOfLines = 0;
[self.footerView addSubview:myButton];
The problem is the frame height of the button stays 20px but the label string is shown on three lines and so overlaps with the other button title. If I add [myButton sizeToFit] then button width is resized to fit all the text into one line and so the title goes beyond the screen.
If I add [myButton sizeThatFits:CGSizeMake(320, 100)] then resize is not working at all. I know sizeToFit should not be called at all because it's not the part of autolayout, however need suggestions on that how easily to make button fit its label to screen 320 width.
I feel I need to add system constraints from code, but haven't done that before, so not sure how it should look like. I probably need a ton of constraints to be added from code in order to get this simple thing working :)
Instead of using Autolayout, you could just use a collection view which better options for you to lay out elements such as buttons.
It is better able to handle layouts under rotation as well.
Or you can use this code, it worked for me...
For example, substitute your desired padding values here:
UIButton* myButton = [[UIButton alloc] init];
// setup some autolayout constraints here
myButton.titleEdgeInsets = UIEdgeInsetsMake(-desiredBottomPadding,
-desiredRightPadding,
-desiredTopPadding,
-desiredLeftPadding);
Combined with the right autolayout constraints, you end up with an auto-resizing button which contains an image and text!
For this kind of customizations you'll need to create a subclass of UIButton. In the subclass you can add a customized label which benefits your needs. You can also override to setTitle:forState method to automatically update your customized label.

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;

iOS: Text is not displayed inside of a rotated UITextField

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.

Resources