This question already has answers here:
Set padding for UITextField with UITextBorderStyleNone
(32 answers)
Closed 3 years ago.
textSearchField.hidden = NO;
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 50, 20)];
textSearchField.leftView = paddingView;
textSearchField.leftViewMode = UITextFieldViewModeAlways;
[search_view addSubview:textSearchField];
I want to add left padding - 10 px for UITextfield using Objective-C. If any other solution for this?
You can create programmatically by using below code:
CGRect someRect = CGRectMake(0.0, 0.0, 100.0, 30.0);
DKTextField* textField = [[DKTextField alloc] initWithFrame:someRect];
textField.padding = 1.0
[self.view addSubview:textField];
Use Below Classes for it
DKTextField.h
#import <UIKit/UIKit.h>
#interface DKTextField : UITextField
#property (nonatomic) IBInspectable CGFloat padding;
#end
DKTextField.m
#import "DKTextField.h"
IB_DESIGNABLE
#implementation DKTextField
#synthesize padding;
-(CGRect)textRectForBounds:(CGRect)bounds{
return CGRectInset(bounds, padding, padding);
}
-(CGRect)editingRectForBounds:(CGRect)bounds{
return [self textRectForBounds:bounds];
}
#end
You just have to give your selected UITextField to DKTextField Class
Whatever the padding you want just add it like below image.
//TRY this
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.placeholder = #"enter text";
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:textField];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 50, 20)];
paddingView.backgroundColor = [UIColor darkGrayColor];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
Check IMAGE
textSearchField = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, screenWidth - 10 - 40, search_height- 10)];
textSearchField.borderStyle = UITextBorderStyleRoundedRect;
textSearchField.font = [UIFont systemFontOfSize:15];
textSearchField.placeholder = #"Search...";
textSearchField.autocorrectionType = UITextAutocorrectionTypeNo;
textSearchField.keyboardType = UIKeyboardTypeDefault;
textSearchField.returnKeyType = UIReturnKeySearch;
textSearchField.clearButtonMode = UITextFieldViewModeWhileEditing;
textSearchField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textSearchField.delegate = self;
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 50, 20)];
paddingView.backgroundColor = [UIColor darkGrayColor];
textSearchField.leftView = paddingView;
textSearchField.leftViewMode = UITextFieldViewModeAlways;[![enter image description here][1]][1]
Related
I have Multiple UITextView's that I have created with a for-loop programmatically. I'm trying to add a UILabel to the upper left corner of each UITextView also.
How can I do that?
My UITextView code:
for (int i = 0; i < 10; i++){
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, yPos, 375,height)];
[textView setBackgroundColor:[UIColor lightGrayColor]]; //set different property like this
UIColor *borderColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1.0];
textView.layer.borderColor = borderColor.CGColor;
textView.layer.borderWidth = 1.0;
textView.layer.cornerRadius = 5.0;
textView.textAlignment=NSTextAlignmentRight;
textView.editable=NO;
[CommentScrooll addSubview:textView ];
// CommentScroll Is the name of my viewcontroller
yPos += (height + padding);
}
Objective-c code:-
UILabel *cust_Label = [[UILabel alloc] initWithFrame:CGRectMake(Your_X, Your_Y, Your_Width,Your_Height)];
cust_Label.text=#"Your Text";
[textView addSubview: cust_Label];
I've never added a UILabel to a UITextView before but I just tested this and it worked. This is the general idea:
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
label.text = "My Label"
textView.addSubview(label)
Hi i am beginner in iOS and in my project i need to insert "titles" at "left side center" and "right side center" on UInavigationBar as like below image please help me how should i do below my requirement
See below code and try :
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = [[UIScreen mainScreen] bounds];
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, 44)];
lbl1.backgroundColor = [UIColor redColor];
lbl1.textAlignment = NSTextAlignmentCenter;
lbl1.text = #"Dashboard";
[self.navigationController.navigationBar addSubview:lbl1];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, 44)];
lbl2.backgroundColor = [UIColor redColor];
lbl2.textAlignment = NSTextAlignmentCenter;
lbl2.text = #"Profile";
[self.navigationController.navigationBar addSubview:lbl2];
}
You can customize the titleView of navigation bar. Read the official documentation here.
As per your question, I tried the below working code:-
-(UIView *)getTitleView
{
CGRect frame = [[UIScreen mainScreen] bounds];
UIView *viewTitle = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, self.navigationController.navigationBar.frame.size.height)];
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, self.navigationController.navigationBar.frame.size.height)];
lbl1.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f];
lbl1.textAlignment = NSTextAlignmentCenter;
[lbl1 setTextColor:[UIColor whiteColor]];
lbl1.text = #"Dashboard";
[viewTitle addSubview:lbl1];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, viewTitle.frame.size.height)];
lbl2.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f];
[lbl2 setTextColor:[UIColor whiteColor]];
lbl2.textAlignment = NSTextAlignmentCenter;
lbl2.text = #"Profile";
[viewTitle addSubview:lbl2];
return viewTitle;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.titleView = [self getTitleView];
}
I divide the code in separate method to follow modular approach. For any part of code, which is of separate task, write that piece of code in different method. This will lead to more readability.
I'm attempting to create a sign in screen for an app I'm working on. Right now I have two UITextFields with an image view behind them. When I try to click on a text field to enter text, nothing happens. I've spent a lot of time trying to figure out this problem but can't find anyone who has had a similar problem. In my LoginViewController's viewDidLoad method I have the following code:
`
[super viewDidLoad];
UIImageView *splashView = [[UIImageView alloc] initWithFrame:self.view.frame];
splashView.image = [UIImage imageNamed:#"Default-568h#2x.png"];
UITextField *username = [[UITextField alloc] initWithFrame:CGRectMake(90, 320, 140, 30)];
username.placeholder = #"username";
username.borderStyle = UITextBorderStyleRoundedRect;
username.returnKeyType = UIReturnKeyDone;
username.textAlignment = NSTextAlignmentLeft;
username.userInteractionEnabled = YES;
[splashView addSubview:username];
UITextField *password = [[UITextField alloc] initWithFrame:CGRectMake(90, 365, 140, 30)];
password.placeholder = #"password";
password.borderStyle = UITextBorderStyleRoundedRect;
password.textAlignment = NSTextAlignmentLeft;
[splashView addSubview:password];
[self.view addSubview:splashView];
`
any help or advice would be appreciated thanks.
Change the place to add Image:
[super viewDidLoad];
UIImageView *splashView = [[UIImageView alloc] initWithFrame:self.view.frame];
splashView.image = [UIImage imageNamed:#"Default-568h#2x.png"];
// The image behind
[self.view addSubview:splashView];
UITextField *username = [[UITextField alloc] initWithFrame:CGRectMake(90, 320, 140, 30)];
username.placeholder = #"username";
username.borderStyle = UITextBorderStyleRoundedRect;
username.returnKeyType = UIReturnKeyDone;
username.textAlignment = NSTextAlignmentLeft;
username.userInteractionEnabled = YES;
// Change this
[self.view addSubview:username];
UITextField *password = [[UITextField alloc] initWithFrame:CGRectMake(90, 365, 140, 30)];
password.placeholder = #"password";
password.borderStyle = UITextBorderStyleRoundedRect;
password.textAlignment = NSTextAlignmentLeft;
//And change this
[self.view addSubview:password];
If you are another image for the other textField add to view first than this.
I have a UISearchbar in xib and the placeholder not located in perfect design.
See image
UITextField *txtField = [UITextField appearanceWhenContainedIn:[UISearchBar class], nil];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 20)];
paddingView.backgroundColor = [UIColor grayColor];
[txtField setLeftViewMode:UITextFieldViewModeAlways];
[txtField setLeftView:paddingView];
txtField.delegate = self;
[txtField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
txtField.textAlignment = UITextAlignmentLeft;
I'm having trouble in an iOS app I'm building with UILabels: despite initializing them the text I programmed won't show up and I'm at wits' end about what I should do.
For example, this is one of the app's screens in which I have this problem:
Welcome.m
#interface Welcome()
#end
#implementation Welcome
-(void) viewdidLoad{
[super viewDidLoad];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 47)];
[self.view addSubview: label];
label.text = #"Benvinguts a la pràctica de iOS I";
label.numberOfLines = 4;
}
-(IBAction) buttonPressed: (id) sender
{
[self performSegueWithIdentifier:#"Tutorial1" sender:self];
}
#end
Could you help me out in fixing this?
You need to add a textColor:
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 47)];
[self.view addSubview: label];
label.text = #"Benvinguts a la pràctica de iOS I";
label.numberOfLines = 4;
label.textColor = [UIColor blackColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 47)];
[self.view addSubview: label];
label.text = #"Benvinguts a la pràctica de iOS I";
label.numberOfLines = 0;
[label SizeToFit];
You may use like this (label size manage by itself.)
Is your Viewcontroller a child? I had the same problem in my code, it would display the box but not ant text. I found a solution by adding a scrollview or something of the sort and adding the label to that got it working.
try this
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 47)];
label.text = #"Benvinguts a la pràctica de iOS I";
label.numberOfLines = 4;
[self.view addSubview: label];
or maybe your label appears under the NavigationController. Try dropping below
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 140, 47)];