SizeToFit Navigation Bar Item (UITextField) - iphone

How do I size a UITextField to fit the whole UINavigationBar? So far I have this:
locationField = [[UITextField alloc] initWithFrame:CGRectMake(37,7,246,31)];
locationField.delegate = self;
locationField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
locationField.textColor = [UIColor colorWithRed:102.0/255 green:102.0/255 blue:102.0/255 alpha:1.0];
locationField.textAlignment = UITextAlignmentLeft;
locationField.borderStyle = UITextBorderStyleRoundedRect;
locationField.font = [UIFont fontWithName:#"Helvetica" size:15];
locationField.autocorrectionType = UITextAutocorrectionTypeNo;
locationField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
I basically want to turn locationField = [[UITextField alloc] initWithFrame:CGRectMake(37,7,246,31)]; to fit the UINavigationBar, probably using sizeToFit, just not 100% sure how to implement it.
Thanks.

You can try the
[self.navigationItem setTitleView:locationField];
See code below:
UITextField *locationField = [[UITextField alloc] initWithFrame:CGRectMake(37,7,246,31)];
locationField.delegate = self;
locationField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
locationField.textColor = [UIColor colorWithRed:102.0/255 green:102.0/255 blue:102.0/255 alpha:1.0];
locationField.textAlignment = UITextAlignmentLeft;
locationField.borderStyle = UITextBorderStyleRoundedRect;
locationField.font = [UIFont fontWithName:#"Helvetica" size:15];
locationField.autocorrectionType = UITextAutocorrectionTypeNo;
locationField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.navigationItem setTitleView:locationField];
[locationField release];

Nevermind, got it fixed by switching the CGRect from:
UITextField *locationField = [[UITextField alloc] initWithFrame:CGRectMake(37,7,246,31)];
to
UITextField *locationField = [[UITextField alloc] initWithFrame:CGRectMake(37,7,300,31)];

Related

UILabel not align center on UINavigationBar in iPhone

I need to align UILabel text center on UINavigationBar. I have implemented all the steps but some how its not center Align. Below is code.
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.backgroundColor = [UIColor clearColor];
[titleLabel setTextColor:[UIColor whiteColor]];
self.titleLabel.text = title;
self.titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
self.titleLabel.adjustsFontSizeToFitWidth = NO;
self.titleLabel.adjustsLetterSpacingToFitWidth = NO;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
[self.titleLabel sizeToFit];
float titlePadding = 60.0f;
self.titleLabel.left = titlePadding;
self.titleLabel.width = self.width - 2 * titlePadding;
self.titleLabel.top = ceilf(self.frame.size.height / 2 - self.titleLabel.frame.size.height / 2);
Please help me with this
Try This It will help
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = NSTextAlignmentCenter;
// ^-Use UITextAlignmentCenter for older SDKs.
label.textColor = [UIColor yellowColor]; // change this color
self.navigationItem.titleView = label;
label.text = NSLocalizedString(#"PageThreeTitle", #"");
[label sizeToFit];

Make UINavigationBar title be user editable

How do I make UINavigationBar title to be user editable, I've tried setting the titleView to be a textfield however it doesn't look the same.. It's missing that outline or drop shadow. I'm aiming for it to look like the default one.
This is what i'm implementing at the moment:
_titleField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 26)];
[_titleField setDelegate:self];
_titleField.text = _bugDoc.data.title;
_titleField.font = [UIFont boldSystemFontOfSize:20];
_titleField.textColor = [UIColor whiteColor];
_titleField.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = _titleField;
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadTitle];
}
- (void)loadTitle
{
txtField_navTitle = [[UITextField alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x,7,self.view.bounds.size.width,31)];
txtField_navTitle.delegate = self;
[txtField_navTitle setBackgroundColor:[UIColor clearColor]];
txtField_navTitle.textColor = [UIColor whiteColor];
txtField_navTitle.textAlignment = UITextAlignmentCenter;
txtField_navTitle.borderStyle = UITextBorderStyleNone;
txtField_navTitle.font = [UIFont boldSystemFontOfSize:20];
txtField_navTitle.autocorrectionType = UITextAutocorrectionTypeNo;
txtField_navTitle.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.navigationItem setTitleView:txtField_navTitle];
txtField_navTitle.layer.masksToBounds = NO;
txtField_navTitle.layer.shadowColor = [UIColor whiteColor].CGColor;
txtField_navTitle.layer.shadowOpacity = 0;
[txtField_navTitle release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
self.title = textField.text;
return YES;
}
Please dont forget to #import <QuartzCore/QuartzCore.h>
Try this way this works for me.
// Custom Navigation Title.
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
tlabel.text=self.title;
tlabel.textAlignment=NSTextAlignmentCenter;
tlabel.font = [UIFont boldSystemFontOfSize:17.0];
tlabel.textColor=[UIColor colorWithRed:7.0/255.0 green:26.0/255.0 blue:66.0/255.0 alpha:1.0];
tlabel.backgroundColor =[UIColor clearColor];
tlabel.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=tlabel;

centering UILabel in UINavigationBar

I have a UINavigationBar and have the leftBarButtonItem and rightBarButtonItem set. The leftbar is set as:
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
UIBarButtonItem *myButton = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
self.navigationItem.leftBarButtonItem = myButton;
and the rightbarbutton is set as:
UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *doneButtonImage = nil;
doneButtonImage = [UIImage imageNamed:#"done.png"];
[doneBtn setImage:doneButtonImage forState:UIControlStateNormal];
doneBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:doneBtn];
self.navigationItem.rightBarButtonItem = doneBarButtonItem;
the width of back is 23px and done is 72px, so they're not equal. Now the issue is I have a label that I want to be in the center all the time.. and I want to clip the text if the text in the label is too long that it interferes with the right bar button. here's how I set it up:
titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.text = self.pageTitle; // Can't change
titleLabel.adjustsFontSizeToFitWidth = YES;
titleLabel.clipsToBounds = YES;
titleLabel.numberOfLines = 1;
titleLabel.font = [UIFont fontWithName:#"PTSans-Narrow" size:30.0];
titleLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
titleLabel.backgroundColor = [UIColor yellowColor];
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
titleLabel.textAlignment = UITextAlignmentCenter;
[titleLabel sizeToFit];
titleLabel.frameHeight = self.navigationController.navigationBar.frameHeight;
self.navigationItem.titleView = titleLabel;
However this doesn't center the title.. How can I do so?
I suggest first count the width of self.pageTitle
then accordingly set the position of titleLabel.I prefer this:-
titleLabel = [[UILabel alloc] init];
titleLabel.text = self.pageTitle; // Can't change
**CGSize maximumTitleLabelSize = CGSizeMake(200,40);//Set maximum width that an self.pageTitle can have.
CGSize expectedLabelSize = [titleLabel.text titleLabel.font
constrainedToSize:maximumTitleLabelSize
lineBreakMode:UILineBreakModeWordWrap];
//adjust the label the the new width.
CGRect newFrame = titleLabel.frame;
newFrame.size.width = expectedLabelSize.width;
titleLabel.size.width = newFrame.size.width;**
if(titleLabel.size.width==100)
{
titleLabel.frame = cgRectMake(85,5,titleLabel.size.width,30);
}
else if(titleLabel.size.width==200)
{
titleLabel.frame = cgRectMake(36,5,titleLabel.size.width,30);
}
titleLabel.adjustsFontSizeToFitWidth = YES;
titleLabel.clipsToBounds = YES;
titleLabel.numberOfLines = 1;
titleLabel.font = [UIFont fontWithName:#"PTSans-Narrow" size:30.0];
titleLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
titleLabel.backgroundColor = [UIColor yellowColor];
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
titleLabel.textAlignment = UITextAlignmentCenter;
[titleLabel sizeToFit];
titleLabel.frameHeight = self.navigationController.navigationBar.frameHeight;
self.navigationItem.titleView = titleLabel;
Try this.It will help you.Thanks :)
This is the simplest solution, use the sizeToFit method before adding it to the titleView:
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = #"My Title";
titleLabel.font = [UIFont boldSystemFontOfSize:17.0f];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel sizeToFit];
self.navigationItem.titleView = titleLabel;
Don't use NavigationItem to add left and right bar button, instead use addsubview method to add custom buttons for navigation bar.
Use a tool bar to add multiple buttons to ur navigation bar.
// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]`enter code here`
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
// create a standard "refresh" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
you just use this code its work fine try it
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 190, 30)];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.text =#"My Wish List";
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont fontWithName:#"Copperplate" size:16.0];
titleLabel.minimumFontSize = 10;
titleLabel.autoresizingMask=UIViewAutoresizingFlexibleWidth;
self.navigationItem.titleView = titleLabel;
[titleLabel release];
i use this code in my project also.......
:-)

How to do parameter passing for Creating N number or labels programmatically

I am doing an application where I am creating a LABELS programmatically..What my Requirement is I need to create some 10 LABELS programmatically, so only there Position and Text label changes. Instead of Writing the same function for every Label Thought of doing it By parameter passing, where only v need pass only the Label name and Position of the label...how to implement it?
//Below code shows creation of Labels With Different Names, instead I want to have one function and do parameter passing rather than creating a function for each label.
-(void)Vendorname
{
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,80,150,20)];
[UIFont fontWithName:#"Arial" size:13];
myLabel.backgroundColor = [UIColor whiteColor];
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.textColor = UIColorFromRGB(0x124F73);
myLabel.text = #"Vendor Name";
[self.view addSubview:myLabel];
[myLabel release];
}
-(void)address
{
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,110,350,20)];
[UIFont fontWithName:#"Arial" size:13];
myLabel.backgroundColor = [UIColor whiteColor];
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.textColor = UIColorFromRGB(0x124F73);
myLabel.text = #"84-21 Gwanheon dong,jungo-gu,seoul,south korea";
[self.view addSubview:myLabel];
[myLabel release];
}
-(UILabel*)createLabel:(CGRect)frame :(NSString*)labelTitle{
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:#"Arial" size:13];
myLabel.backgroundColor = [UIColor whiteColor];
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.text = labelTitle;
return [myLabel autorelease];
}
[self.view addSubview:[self createLabel:CGRectMake(50,80,150,20):#"ur Title"]];
-(void)createLabel:(NSString *)text :(CGRect)frame{
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:#"Arial" size:13];
myLabel.backgroundColor = [UIColor whiteColor];
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.textColor = UIColorFromRGB(0x124F73);
myLabel.text = text;
[self.view addSubview:myLabel];
[myLabel release];
}
call the above method...
[self createLabel:#"label1" :frame];
try this one
int x=50;
int y=80
[self.view addSubview:[self createLabel:CGRectMake(x,y,150,20) title:#"Vendore"]];
-(UILabel*)createLabel:(CGRect)frame title:(NSString*)title{
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:#"Arial" size:13];
myLabel.backgroundColor = [UIColor whiteColor];
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.text = title;
return [myLabel autorelease];
}

How to create a passcode view iphone

I'd like to add a passcode lock in my app...
I created the view but I don't know how to get it working...
This is what I'd like it must do:
- If an user is setting his passcode, he must type it twice and the code must verify if the passcode typed the second time is the same of the first time.
- If the passcode controller is called by a setting view, for example, to set the passcode, it must have a cancel button on the navigation bar but if it is called at app launch, the cancel button mustn't be enabled.
summaryLabel is the label that show a message like "Passcode didn't match. Try again." when the passcode is not the same as the one written previously or saved.
EDIT1: How can I use textField:shouldChangeCharactersInRange:replacementString method to do this?
This is the code:#import "PasscodeController.h"
#implementation PasscodeController
#synthesize panelView;
#synthesize summaryLabel;
#synthesize titleLabel;
#synthesize textField1;
#synthesize textField2;
#synthesize textField3;
#synthesize textField4;
#synthesize hiddenTF;
-(void)viewDidLoad {
self.title = #"Passcode";
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 22, 270, 30)];
titleLabel.font = [UIFont boldSystemFontOfSize:15];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.textColor = [UIColor colorWithRed:66.0/255.0 green:85.0/255.0 blue:102.0/255.0 alpha:1.0];
titleLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:titleLabel];
[titleLabel release];
summaryLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 130, 270, 40)];
summaryLabel.font = [UIFont boldSystemFontOfSize:12];
summaryLabel.numberOfLines = 0;
summaryLabel.baselineAdjustment = UIBaselineAdjustmentNone;
summaryLabel.textAlignment = UITextAlignmentCenter;
summaryLabel.textColor = [UIColor colorWithRed:66.0/255.0 green:85.0/255.0 blue:102.0/255.0 alpha:1.0];
summaryLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:summaryLabel];
[summaryLabel release];
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(25, 60, 60, 60)];
textField1.borderStyle = UITextBorderStyleBezel;
textField1.textColor = [UIColor blackColor];
textField1.textAlignment = UITextAlignmentCenter;
textField1.font = [UIFont systemFontOfSize:41];
textField1.secureTextEntry = YES;
textField1.backgroundColor = [UIColor whiteColor];
textField1.keyboardType = UIKeyboardTypeNumberPad;
[self.view addSubview:textField1];
[textField1 release];
textField2 = [[UITextField alloc] initWithFrame:CGRectMake(95, 60, 60, 60)];
textField2.borderStyle = UITextBorderStyleBezel;
textField2.textColor = [UIColor blackColor];
textField2.textAlignment = UITextAlignmentCenter;
textField2.font = [UIFont systemFontOfSize:41];
textField2.secureTextEntry = YES;
textField2.backgroundColor = [UIColor whiteColor];
textField2.keyboardType = UIKeyboardTypeNumberPad;
[self.view addSubview:textField2];
[textField2 release];
textField3 = [[UITextField alloc] initWithFrame:CGRectMake(165, 60, 60, 60)];
textField3.borderStyle = UITextBorderStyleBezel;
textField3.textColor = [UIColor blackColor];
textField3.textAlignment = UITextAlignmentCenter;
textField3.font = [UIFont systemFontOfSize:41];
textField3.secureTextEntry = YES;
textField3.backgroundColor = [UIColor whiteColor];
textField3.keyboardType = UIKeyboardTypeNumberPad;
[self.view addSubview:textField3];
[textField3 release];
textField4 = [[UITextField alloc] initWithFrame:CGRectMake(235, 60, 60, 60)];
textField4.borderStyle = UITextBorderStyleBezel;
textField4.textColor = [UIColor blackColor];
textField4.textAlignment = UITextAlignmentCenter;
textField4.font = [UIFont systemFontOfSize:41];
textField4.secureTextEntry = YES;
textField4.backgroundColor = [UIColor whiteColor];
textField4.keyboardType = UIKeyboardTypeNumberPad;
[self.view addSubview:textField4];
[textField4 release];
hiddenTF = [[UITextField alloc] initWithFrame:CGRectZero];
hiddenTF.hidden = YES;
hiddenTF.delegate = self;
hiddenTF.keyboardType = UIKeyboardTypeNumberPad;
[self.view addSubview:hiddenTF];
[hiddenTF release];
[hiddenTF becomeFirstResponder];
}
Thanks a lot!
Another solution, KVPasscodeViewController (mine), is available here: https://github.com/Koolistov/Passcode (BSD license).
If this can be helpful for other, I solved my problem with this source code on GitHub: PTPasscodeViewController.
I changed it a little bit to adapt it to my needs and now works greatly :)
If you want to use it, there are all the information about how to use it on the project page or in a file if you've downloaded it ;)
Hope this help!
P.S.: Thanks a lot to Lasha Dolidze to provide this code!
PINCode 1.0