Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
I assume you mean that you want the text to be centered in the textfield.

Just click the Center Alignment button in IB. Text Field/Alignment. It's the second one from the left. Leave that Control/Alignment stuff alone.
 
All set.

I have already set it to centre, when it's empty the place holder is centred but as soon as you enter content it goes left.
 
Last edited by a moderator:
Pleas post your code as text.
Please use CODE tags.

It's easier to examine the code itself, rather than pictures of code.


The picture of code you linked to shows the IBAction method forceCenter: using NSTextAlignmentRight. It was impractical to search the picture for the code that calls forceCenter:, so I have no idea if that code is ever executed or not. If it is, then where's the code that sets alignment to center?
 
Removed.

All that code has since been removed, theres nothing but the text box, with the "middle" align highlighted.

The only thing I have is this;
Code:
#import "RegisterViewController.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController
{
    int passwordRequired;
}

@synthesize businessName;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)viewWillAppear:(BOOL)animated
{
    
}

-(void)ReceiveNotification:(NSNotification *)notification
{
}

-(void)viewWillDisappear:(BOOL)animated
{

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)toLogin:(id)sender {
    [self performSegueWithIdentifier:@"toLoginWindow" sender:self];
}


@end
 
Your code has now been stripped down to the bare essentials. I see nothing obvious that would be affecting alignment.

If the view is still misbehaving, then it's not in the code you posted. That leaves two logical possibilities where the problem could be:
1. In some code you haven't posted.
2. In the nib/xib itself.

If there's no other code at all, then you'll probably have to post the complete compilable project that demonstrates the problem, so someone else can look at it. This means all the xibs, storyboards, or whatever resources are needed to build the project and run it.

If you don't want to post the actual project, you'll need to make a simplified test case. Read this:
https://forums.macrumors.com/posts/19529649/

It's not uncommon that the process of making a simplified test case and systematically verifying behavior after every single change leads you to discover what's wrong with the original project. If it doesn't, then you at least have a postable test case that others can look at, edit, compile, and run to see the behavior.
 
Just to be clear. No code is needed to center text in a UITextField. All you need to do is set the Alignment to center in IB and it should work as expected.

If you've messed with other things to try to center the text you might want to remove that UITextField and start over with a new one.
 
I have tried creating a new field with no luck I even created a new view controller and set it as the root. The new view controller didn't have a class so there was no code attached to it. It's really strange the "text field" aligns correctly in xCode its in the simulator when it forces left.

However if it's blank the placeholder will respect the align wether it be left right or centre. As soon as you enter anything and you click another text field or close the keyboard (lose focus). It forces left?

If you set the field as *secure* so password field. That actually aligns correctly. Posting the code is a difficultly as its for a clien. I don't normally ask for help but I thought another programmer might see something I'm missing.

Please help!
 
What version of Xcode are you using?

Here's what I did.

Create a new project, choose Single View Application.
To the Main.storyboard I added two UITextFields and adjusted their width to 170.
I set the Text Field/Alignment to Center
I added some text to each text field 'Hello' and 'There'
I set the Placeholder text for each text field to 'Placeholder'
I then ran the app in one of the iPad Simulators

The text and placeholder text is always centered. If I close the keyboard the text remains centered.

What are you doing differently than that?
 
That works.

That works if I create a new document and do it, so it means theres something in my app stopping it. I have checked app delegate, but nothing suppressing my fields or controlling them. I am using cocca pods as I have BrainTree payments in there, but this issue was from before then. I have fixed everything else apart from this, but not it needs fixing, its not an option to "re-create" the entire project. Theres too much to transfer.

Just wondering if theres any code which can "force" this to happen so I can search for it see if it still occurs.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.