Add fixed UILabel in UIPickerView - iphone

I saw a this question and answer and I tried a few options but non worked.
I would like to create a UIPickerView like the one below, (fixed labels inches and feet) but those wouldn't appear:
I create the UIImagePicker like this:
- (void)viewDidLoad
{
_picker = [[UIPickerView alloc] init];
CGRect pickerFrame = CGRectMake(0, 0, 200, 216);
pickerView.frame = pickerFrame;
pickerView.userInteractionEnabled = YES;
pickerView.dataSource = self;
pickerView.hidden = YES;
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[self.view addSubview:pickerView];
[textField setInputView:pickerView];
textField.delegate = self;
[pickerView removeFromSuperview];
_picker.hidden = YES;
}
- (BOOL) textFieldShouldBeginEditing:(UITextView *)textView
{
if (textView.tag==1){ //field for the uipickerview
_picker.hidden = NO;
[self addPickerLabel:#"Feet" rightX:114 top:342 height:21];
[self addPickerLabel:#"Inches" rightX:241 top:342 height:21];
}
return YES;
}
- (void)addPickerLabel:(NSString *)labelString rightX:(CGFloat)rightX top:(CGFloat)top height:(CGFloat)height {
#define PICKER_LABEL_FONT_SIZE 18
#define PICKER_LABEL_ALPHA 0.7
UIFont *font = [UIFont boldSystemFontOfSize:PICKER_LABEL_FONT_SIZE];
CGFloat x = rightX - [labelString sizeWithFont:font].width;
// White label 1 pixel below, to simulate embossing.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, top + 1, rightX, height)];
label.text = labelString;
label.font = font;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
label.alpha = PICKER_LABEL_ALPHA;
[_picker addSubview:label];
// Actual label.
label = [[UILabel alloc] initWithFrame:CGRectMake(x, top, rightX, height)];
label.text = labelString;
label.font = font;
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
label.alpha = PICKER_LABEL_ALPHA;
[_picker addSubview:label];
}
The picker appears, but without the fixed labels of inches and feet.
What is wrong?

Move this lines to viewDidLoad and try it.Labels need to be added
once.Not always when textfield did begin editing
[self addPickerLabel:#"Feet" rightX:114 top:342 height:21];
[self addPickerLabel:#"Inches" rightX:241 top:342 height:21];
Log the frame of both the label and set it correct if it appears wrong
NSLog(#"%#",NSStringFromCGRect(label.frame));

Your UIPickerView has height of 216, but you put the labels at height 342. This might be the reason you can't see them.
Edit:
Try replacing the lines where you make the labels to
[self addPickerLabel:#"Feet" rightX:114 top:98 height:21];
[self addPickerLabel:#"Inches" rightX:241 top:98 height:21];

I used the already implemented great component:
https://github.com/brunow/ActionSheetPicker2
Which provides a multicolumn picker view and I simply changed the text and the amount of columns

Related

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;

Display vertical pages for UITextView in UIScrollView

How i can display vertical pages for UITextView in UIScrollview
self.textView = [[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 410)]autorelease];
self.textView.textColor = [UIColor blackColor];
self.textView.font = [UIFont fontWithName:#"Georgia-Bold" size:14];
self.textView.textAlignment = UITextAlignmentCenter;
//self.textView.backgroundColor = [UIColor colorWithHue:2.0/12 saturation:2.0 brightness:4.0/10 alpha:1.0];
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.editable = NO;
self.textView.delegate = self;
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.layer.borderWidth = 1;
self.textView.layer.borderColor = [[UIColor darkGrayColor] CGColor];
self.textView.layer.cornerRadius = 1;
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = #"Juice fasting makes every other natural method work faster...............";
[self.view addSubview:self.textView];
scrollView.contentSize = CGSizeMake(textView.frame.size.width, textView.frame.size.height);
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 0.75;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.delegate = self;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
[self.scrollView addSubview:self.textView];
[button release];
[super viewDidLoad];
}
I have a long text in UITextView. How i can display this UITextView in vertical pages when user scrolls.
Appreciate help.
Did you try paging?
textView.pagingEnabled = YES;
Is that what you want? Otherwise I probably would recommend using a default UILabel in a default UIScrollView, to set this up. (Adding one UILabel per page to the scrollView)
Make the text view large enough so that there is no scrolling within it (or change it to a UILabel), and put that inside a separate scrollView.
Then set
scrollView.pagingEnabled = YES;
and make sure the height of the content size is larger than the height of the actual scroll view. So if the text view is 320x960, make the scroll view 320x480.

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

increasing width of uilabel dynamically

I want to dynamically assign width to a label depending upon the text length to be displayed. The labels are it self being added on uiview. I am using following code but still i am getting label with shorter width.
- (id)initWithFrame:(CGRect)frame OrangeText:(NSString*)orange WhiteText:(NSString*)white {
if ((self = [super initWithFrame:frame])) {
CGSize textSize = [orange sizeWithFont:[UIFont systemFontOfSize:14]];
OrangeLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, textSize.width, textSize.height+2)];
OrangeLabel.text = orange;
OrangeLabel.backgroundColor = [UIColor clearColor];
OrangeLabel.textColor = [UIColor orangeColor];
[self addSubview:OrangeLabel];
WhiteLabel = [[UILabel alloc] init];
CGSize whiteTextSize = [white sizeWithFont:[UIFont systemFontOfSize:14]];
WhiteLabel.frame = CGRectMake(OrangeLabel.frame.size.width+35, 5, whiteTextSize.width, whiteTextSize.height);
WhiteLabel.text = white;
WhiteLabel.backgroundColor = [UIColor clearColor];
WhiteLabel.textColor = [UIColor whiteColor];
[self addSubview:WhiteLabel]; // Initialization code
}
return self;
}
I think you are looking for this method
[myLabel sizeToFit];
This should resize the label frame to fit its contents.

UINavigationItem titleView position

I'm using UINavigationItem's titleView property to set a custom UILabel with my desired font size/color. Here's my code:
self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 400.0, 44.0)];
self.headerLabel.textAlignment = UITextAlignmentCenter;
self.headerLabel.backgroundColor = [UIColor clearColor];
self.headerLabel.font = [UIFont boldSystemFontOfSize:20.0];
self.headerLabel.textColor = [UIColor colorWithRed:0.259 green:0.280 blue:0.312 alpha:1.0];
self.navigationItem.titleView = self.headerLabel;
In the navigation bar I also have a left bar button. The result is: the text isn't properly centered. I've tried setting the x origin of the label, but this has no effect.
In stead of initWithFrame just use init and put [self.headerLabel sizeToFit] after your last line of code.
If you make the headerLabel a subview of the titleView, you can then set headerLabel's frame to control where it goes within the titleView.
The way you are doing it now, you don't have that control. I think the OS chooses the titleView's frame for you based on the space available.
Hope this helps!
I've used custom title labels for my nav bars in every app I have in the app store. I've tested many different ways of doing so and by far the easiest way to use a custom label in a navigation bar is to completely ignore titleView and insert your label directly into navigationController.view.
With this approach, it's easy to have the title label's frame always match the navigationBar's frame -- even if you are using a custom navBar with a non-standard size.
- (void)viewDidLoad {
[self.navigationController.view addSubview:self.titleLabel];
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didRotateFromInterfaceOrientation:
(UIInterfaceOrientation)fromInterfaceOrientation {
[self frameTitleLabel];
}
- (UILabel *) titleLabel {
if (!titleLabel) {
titleLabel = [[UILabel alloc]
initWithFrame:self.navigationController.navigationBar.frame];
titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:18];
titleLabel.text = NSLocalizedString(#"Custom Title", nil);
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
}
return titleLabel;
}
- (void) frameTitleLabel {
self.titleLabel.frame = self.navigationController.navigationBar.frame;
}
The one caveat to this approach is that your title can flow over the top of any buttons you have in the navBar if you aren't careful and set the title to be too long. But, IMO, that is a lot less problematical to deal with than 1) The title not centering correctly when you have a rightBarButton or 2) The title not appearing if you have a leftBarButton.
I have a same problem; I just somehow solved this issue by calculating the title length and set the label frame width accordingly. Although this is not a perfect one but can be manageable. Here is the code.
label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.font = [ UIFont fontWithName: #"XXII DIRTY-ARMY" size: 32.0 ];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.0f];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor orangeColor];
//label.text=categoryTitle;
CGFloat verticalOffset = 2;
NSString *reqSysVer = #"5.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
{
if (categoryTitle.length > 8)
{
label.frame = CGRectMake(0, 0, 300, 44);
}else {
label.frame = CGRectMake(0, 0, 80, 44);
}
self.navigationItem.titleView = label;
self.navigationItem.title=label.text;
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setTintColor:[UIColor newBrownLight]];
}
Just calculate exact frame size needed and align to left:
UIFont* font = [UIFont fontWithName:#"Bitsumishi" size:20];
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [title sizeWithFont:font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeCharacterWrap];
CGRect frame = CGRectMake(0, 0, expectedLabelSize.width, expectedLabelSize.height);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.font = font;
label.textAlignment = UITextAlignmentLeft;
label.text = title;
self.titleView = label;
UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
lbl.text = #"Home";
lbl.textAlignment = UITextAlignmentCenter;
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:20];
lbl.shadowColor = [UIColor blackColor];
lbl.shadowOffset = CGSizeMake(0,1);
self.navigationItem.titleView = vw;
[self.navigationItem.titleView addSubview:lbl];
What worked for me was to update the titleView frame in the viewDidAppear method.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIView *titleView = self.navigationItem.titleView;
CGRect navBarFrame = self.navigationController.navigationBar.frame;
[titleView setFrame:CGRectMake((CGRectGetWidth(navBarFrame) - TitleWidth) / 2, (CGRectGetHeight(navBarFrame) - TitleHeight) / 2, TitleWidth, TitleHeight)];
}