I am getting a sigabrt error when I try to set the delegate of my UITextField to my UIView class. I have used the UITextFieldDelgate Protocal. here is my code
nameField = [[UILabel alloc] initWithFrame:CGRectMake(130, 40, 200, 30)];
[nameField setDelegate:self];
nameField.placeholder = #"<Game Name>";
nameField.textAlignment = UITextAlignmentCenter;
[self addSubview:nameField];
You are about to use UITextField in constructor, not UILabel. Replace your first string with the following:
nameField = [[UITextField alloc] initWithFrame:CGRectMake(130, 40, 200, 30)];
Related
Below is an image of the screen, I just want to when the user enters the amount on this textfield then the user can't able to move cursor before $ sign and can't able to write amount before $ sign.
Please have a look on my problem, and give me idea about it,
You can simply add a textfield inside a view and before textfield add a lable with text "$". Remove the textfield border and add a border layer for view. It will work perfectly as per your requirement.
Try to set currency image as leftview of textfield
For Imageview
UIImageView *imageArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"currency.png"] ];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[paddingView addSubview:imageArrow];
imageArrow.center = paddingView.center;
textfield.leftView = paddingView;
textfield.leftViewMode = UITextFieldViewModeAlways;
For Label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
label.text = #"$";
textField.leftViewMode = UITextFieldViewModeAlways;
textField.rightView = label;
You can use left view property of UITextField
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text = #"$";
label.backgroundColor = [UIColor white];
textField.leftViewMode = UITextFieldViewModeAlways;
textField.rightView = label;
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.
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)];
I have a simple but custom section header in which I am showing dynamic content in a label. However, when I pop a VC and in the active VC I run [self.quickDialogTableView reloadData] in viewWillAppear, the data is not being updated to the correct "self.sectionHeaderTitle. Any idea what I am doing wrong?
(void)sectionHeaderWillAppearForSection:(QSection *)section atIndex:(NSInteger)indexPath {
if (indexPath == 0) {
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 110)];
header.backgroundColor = [UIColor clearColor];
UIView *header_background = [[UIView alloc] initWithFrame:CGRectMake(25, 10, 270, 100)];
header_background.backgroundColor = [UIColor whiteColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 255, 25)];
title.text = #"A sample static title";
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(55, 40, 200, 40)];
label.text = self.sectionHeaderTitle;
[header addSubview:header_background];
[header_background addSubview:title];
[header_background addSubview:label];
[section setHeaderView:header];
}
Try reload the section, like this:
[self.quickDialogTableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationBottom];
myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
myscrollview.delegate = self;
[self.view addSubview:myscrollview];
CGSize scrollViewContentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+500);
[myscrollview setContentSize:scrollViewContentSize];
lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
lblfirstmsg = [[UILabel alloc] initWithFrame:CGRectMake(15, 50, 295, 800)];
But I don't know My Firstmasg length.... after that lblfirstmsg I want to add second lblsecongmgs.... So how can I add?
After you set first label and set it content you can call [lblfirstmsg sizeToFit]; to fit frame to its content. Then you can add your second UIlabel next to the first one [[UILabel alloc] initWithFrame:CGRectMake(lblfirstmsg.frame.origin.x + lblfirstmsg.frame.size.width, lblfirstmsg.frame.origin.y, 200, 50)];
[myscrollview addSUbview:lblluckyno];
and same way add 2nd label.
This is your scroll view code.
myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
myscrollview.delegate = self;
[self.view addSubview:myscrollview];
[myscrollview setContentSize:CGSizeMake(self.view.frame.size.width,self.view.frame.size.height+500);];
After you set two label With CGRect and add in Scroll View
[myScrollView addSubView:lblluckyno];
......
Hello Mayur P Bhansali,
You can find the position and dimension of your first label like so:
lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
UILabel *secondLabel = [[UILabel alloc] initWithFrame: CGRectMake(lblluckyno.frame.origin.x + lblluckyno.frame.size.width, 200, 100)];
Each UIView has a "frame" object and this frame object has a "origin" C structure and "size" C structure. Using labelName.frame.size.width gives you the width of a frame and labelName.frame.origin.x gives you the X coordinate of the frame.
You could access these properties like this too:
[labelName frame].size.width
[labelName frame].size.origin.x
lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
lblfirstmsg = [[UILabel alloc] initWithFrame:CGRectMake(15, 50, 295, 800)];
[myscrollview addSUbview:lblluckyno];
[myscrollview addSUbview:lblfirstmsg];