0

New to Xcode. I have a textfield which has a black outline on, but I want it white. The font colour needs to be white also. And if it's possible to be able to center the text that will be inputted.

Thanks

Here is my code so far:

self.poundsTxt=[[UITextField alloc] initWithFrame:CGRectMake(17, 210, 120, 25)];
self.poundsTxt.borderStyle = UITextBorderStyleLine;
self.poundsTxt.font = [UIFont systemFontOfSize:15];
self.poundsTxt.autocorrectionType = UITextAutocorrectionTypeNo;
self.poundsTxt.keyboardType = UIKeyboardTypeNumberPad;
self.poundsTxt.tag=1;
self.poundsTxt.userInteractionEnabled=YES;
self.poundsTxt.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.poundsTxt.delegate = self;
self.poundsTxt.returnKeyType=UIReturnKeyDone;
[self.view addSubview:self.poundsTxt];
John Kee
  • 45
  • 1
  • 3
  • 10

3 Answers3

1

To change font color to white and make text align center add this

self.poundsTxt.textColor = [UIColor whiteColor];
self.poundsTxt.textAlignment=UITextAlignmentCenter;

To change border color to white and to give a width to border add this

self.poundsTxt.layer.borderColor = [[UIColor whiteColor]CGColor];
self.poundsTxt.layer.borderWidth=2.0;

import Quartz Core Framework Reference into your project and add

# import < QuartzCore/QuartzCore.h>

to your ViewController.m to work borderColor and borderWidth.

George
  • 191
  • 4
0

Unless you really needs to create your UI in code, create a view controller with an xib and use InterfaceBuilder to lay out your UI and tweak options. Many of the things you wish do to are in Interface Builder. The project probably already has a pair set up for you to start with.

Apple has a tutorial on making a simple app this way.

You are actually creating a UITextField not a button which would be a UIButton. UITextField has the following properties that you would be interested in:

@property(nonatomic, retain) UIColor *textColor;
@property(nonatomic) NSTextAlignment textAlignment;

You would access these in the same manner as the other properties you have used:

self.poundsTxt.textColor = [UIColor whiteColor];
self.poundsTxt.textAlignment = NSTextAlignmentCenter;

For the boarder color, see the answer to Border around UITextView

Also, be sure to read the documentation for UITextView, UIButton and UIView.

Community
  • 1
  • 1
BergQuester
  • 6,167
  • 27
  • 39
0

To set a border color for UITextField, you can set it's layer properties' Eg:-

myTextField.layer.borderColor = [UIColor whileColor];

Make sure you import the QuartzCore framework into your project and #import <QuartzCore/QuartzCore.h> in your implementation

rajagp
  • 1,443
  • 8
  • 10