Add UILabel as a constant field to left of UITextField - ios

I am facing on problem, I want to have a static text in UITextField.
Text like "UserName:" should appear constantly to the left side of UITextField. Editing the UITextField should start from where the "UserName:" text ends.
For example, in iPhone settings, if we go to Twitter app and try to add new account. The way the user name and password textfield looks like. I want to develop same like that.

I don't understand why everyone has has posted such off the track answers to such a simple issue.
You simply do this:
Create a UILabel, with UI complimenting you textField.
You set it's text (Username:, etc).
Assign it to your textField's leftView property.
That's it. You will have to check frames, but that is basic, and I believe you can do that without much effort.
UPDATE: Sample code
UITextField* aField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 300, 40)];
aField.placeholder = #"Please enter a username";
aField.layer.borderWidth = 1.0f;
aField.backgroundColor = [UIColor whiteColor];
aField.layer.borderColor = [UIColor lightGrayColor].CGColor;
aField.layer.cornerRadius = 3.0f;
UILabel* aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 90, 38)];
aLabel.text = #"Username:";
aLabel.font = aField.font;
aLabel.textColor = [UIColor grayColor];
aField.leftView = aLabel;
aField.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:aField];
Only needs minor adjustments.

Try to use two text fields (one partially overlap with other) or Implement the below code.
-(void)viewDidAppear:(BOOL)animated{
yourTextField.text = #"UserName:";
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//handle back spaces and error conditions
NSString *editedText = [textField.text stringByReplacingCharactersInRange:range withString:string];
if([editedText isEqualToString: #"UserName"])
{
return NO;
}
else
{
yourTextField.text = editedText;
return YES;
}
}

Below code may cater your need.
UIView *vis = [[UIView alloc]initWithFrame:CGRectMake(0, 200, 320, 50)];
UILabel *lblUserName = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 120, 40)];
UITextField *txtUserName =[[UITextField alloc]initWithFrame:CGRectMake(125, 5, 180, 40)];
lblUserName.text= #"User Name";
txtUserName.placeholder = #"#User Name";
[vis addSubview:lblUserName];
[vis addSubview:txtUserName];
[vis setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:vis];
Regards,
Amit

Please try this code. I am sure this will help you.
UILabel *lblLeft = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
lblLeft.backgroundColor = [UIColor clearColor];
lblLeft.text = #"Name: ";
lblLeft.textColor = [UIColor lightGrayColor];
lblLeft.font = [UIFont systemFontOfSize:14.0];
txtYourTextField.leftView = lblLeft;
Happy Coding :)
Deepak

For quick solution, do that with two overlapping textfields like this:
Then change the textfield border style to No border style and disable the user interaction of the below textfield:

Related

How to change the UITextfield placeholder textcolor using subclass in ios

I have a UITextField with placeholder text set. In the UITextField I want the placeholder text to red and also wants to change font family and fantasize.
So creating UITextField subclass. I am writing like this.
-(void)drawPlaceholderInRect:(CGRect)rect
{
[[UIColor redColor]setFill];
[[self placeholder]drawInRect:rect withFont:[UIFont fontWithName:#"verdana" size:15]];
}
next i am importing this class to my view controller class.
#import "SubViewController.h"
#import "CustomUITextFieldPlaceholderAppearance.h"
- (void)viewDidLoad
{
[super viewDidLoad];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"enter text";
textField.delegate = self;
[self.view addSubview:textField];
}
Then how to change the placeholder text color.
Assume that your UITextField subclass is CustomUITextFieldPlaceholderAppearance
Only replace your line:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
with:
CustomUITextFieldPlaceholderAppearance *textField = [[CustomUITextFieldPlaceholderAppearance alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
You can set User Define Run time attributes to change textfield placeholder color.

SetLeftView In Label Objective-C

it has been asked before (using textfield, and I'm asking how to include a character not a small icon), and yes I have already tried using this SetLeftView to put a dollar sign '$' or whatever character I want beside the TEXTFIELD.
However, I do not want a textfield, but a label, and when I apply the same code to do what I want, it returns me an error, I mean Xcode fails to build the code.
Here is my code:
// We add a label to the top that will display the results
self.resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 80, TEXTAREA_WIDTH, TEXTAREA_HEIGHT)];
[resultLabel setBackgroundColor:[UIColor clearColor]];
[resultLabel setText:#"01234"];
[resultLabel setFont:[UIFont fontWithName:#"AppleGothic" size:30.0f]];
resultLabel.textColor = RGB(255,255,255);
resultLabel.textAlignment = NSTextAlignmentCenter;
// Add it to the view
[self.view addSubview:resultLabel];
UILabel *dollarSignLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, 25, 40)];
dollarSignLabel.text = #"$";
dollarSignLabel.textColor = RGB(255,255,255);
[dollarSignLabel setFont:[UIFont fontWithName:#"AppleGothic" size:30.0f]];
[resultLabel setLeftView:dollarSignLabel];
[resultLabel setLeftViewMode:UITextFieldViewModeAlways];
Error: No visible #interface for 'UILabel' declares the selector
'setLeftView'. Same error in the line of setLeftViewMode.
Again, this works if I use a textfield.
My working code (using textfield)
// adding a textField for input
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(viewHalf-30, 100, 200, 40)];
[myTextField setBackgroundColor:[UIColor clearColor]];
[myTextField setText:#"0"];
[myTextField setFont:[UIFont fontWithName:#"AppleGothic" size:30.0f]];
myTextField.textColor = RGB(255,255,255);
[[self view] addSubview:myTextField];
UILabel *dollarSignLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 25, 40)];
dollarSignLabel.text = #"$";
dollarSignLabel.textColor = RGB(255,255,255);
[dollarSignLabel setFont:[UIFont fontWithName:#"AppleGothic" size:30.0f]];
[myTextField setLeftView:dollarSignLabel];
[myTextField setLeftViewMode:UITextFieldViewModeAlways];
The reason you can't apply that because UILabel doesn't have a method named setLeftView as UITextField do.
What you can do is :
Create two labels next to each other.
Set left labels trailingSpace to right label to 0. Arrange other constraints whatever you want.
Set left label's textAlingment property to NSTextAlignmentRight and right label's to NSTextAlignmentLeft.
Set dolar sign on a left label and numbers to another.
Since a label isn't editable by the user anyway, there is no reason not just to add your $ sign to the label itself.
label.text = [#"$" stringByAppedingString:yourText];
if the special symbol should be an image instead, then look at NSTextAttachment & draw attributed Text
hope this will help u out.
add a dollar image on your label.
override following method of UILabel
-(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
bounds.origin.x =+leftMargin;
return bounds;
}
- (void)drawTextInRect:(CGRect)rect
{
[super drawTextInRect: CGRectInset(self.bounds, leftMargin , 0)];
}

UITextView input cursor issue in iOSsimulator

I am trying to create dynamic UITextViewController for my iOS application.So from my ApplicationViewController I am trying to get the TextView.
Textview is coming properly but the input cursor for my UITextView in coming down.How to get the blue input cursor from the top?
TextViewController.m
+(UITextField *)prepareUITextView
{
UITextView *uiTextView= [[UITextView alloc] initWithFrame:CGRectMake(
10, 80, 300, 100)];
uiTextView.layer.borderWidth = 1.0;
uiTextView.layer.cornerRadius = 8;
uiTextView.font = [UIFont systemFontOfSize:15];
uiTextView.autocorrectionType = UITextAutocorrectionTypeNo;
uiTextView.keyboardType = UIKeyboardTypeDefault;
uiTextView.returnKeyType = UIReturnKeyDone;
[uiTextView becomeFirstResponder];
uiTextView.selectedRange = NSMakeRange(0, 0);
return uiTextView;
}
ApplicationViewController.m
[self.view addSubview:[TextViewController prepareUITextView]];
Try this code
uiTextView.contentInset = UIEdgeInsetsMake(-7.0,0.0,0,0.0);
Adjust the Top value the way you want. this will solve your issue.

UITextFields aren't allowing me to edit text

I've programmatically created two UITextFields in my iOS app, and set their text to the _minPrice and _minPrice variables, respectively.
The _minPrice and _maxPrice values appear correctly in the two fields, but tapping on them doesn't allow the user to edit them, they just remain those static values, backspacing doesn't work. Is there anything about my code thats preventing the text fields from being edited?
// Min Price
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(-25, -76, 70, 30)];
tf.textColor = [UIColor blackColor];
tf.font = [UIFont fontWithName:#"Helvetica-Neue" size:14];
tf.backgroundColor=[UIColor whiteColor];
tf.text= _minPrice;
tf.textAlignment = NSTextAlignmentCenter;
tf.layer.cornerRadius=8.0f;
tf.layer.masksToBounds=YES;
tf.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf.layer.borderWidth= 1.0f;
// Max Price
UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(100, -76, 70, 30)];
tf1.textColor = [UIColor blackColor];
tf1.font = [UIFont fontWithName:#"Helvetica-Neue" size:14];
tf1.backgroundColor=[UIColor whiteColor];
tf1.text= _maxPrice;
tf1.textAlignment = NSTextAlignmentCenter;
tf1.layer.cornerRadius=8.0f;
tf1.layer.masksToBounds=YES;
tf1.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf1.layer.borderWidth= 1.0f;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 400, 400)];
[view addSubview:tf];
[view addSubview:tf1];
[self.view addSubview:view];
Your issue is clearly the frames you're setting...
Setting the color of the view you add the labels to to blue reveals your problem:
If you ensure that the labels are actually within the view you add them to (i.e. not negative), editing will be fine. All I did was change the negative x and y values in tf to positive, and they were editable:
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(25, 76, 70, 30)];
Try this! Maybe there is another view at the top of the textField
your_textfield.userInteractionEnabled = YES;
If this doesn't work
add another line
[self.view bringSubviewToFront: your_textfield];
Try to add delegate methods on your textfields. Like
- (void) textFieldDidEndEditing:(UITextField *)textField
{
// ADD BREAKPOINT HERE.
}
Check if it goes to that line of code. If not maybe there's a view on top of it. Or you can try to bring textfield to front like .
[self.view bringSubviewToFront:yourTextfield];
But this isn't a good example of how you fix the problem. Just to test if there is a view on top of it.

How to set the texttype for custom textview in xcode

How to give the style for the custom textview as password in xcode ios4.3
I think this is what you are looking for
UITextField *textFieldPassword = [[UITextField alloc] initWithFrame:CGRectMake(120, 90, 150, 30)];
textFieldPassword.borderStyle = UITextBorderStyleRoundedRect;
textFieldPassword.textColor = [UIColor blackColor];
textFieldPassword.font = [UIFont systemFontOfSize:12.0];
[textFieldPassword setSecureTextEntry:YES];
Suppose your UITextField is password than just do this
[password setSecureTextEntry:YES];
Hope it help you.

Resources