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)];
Related
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]
I have created a application which displays the contacts in my device in a table view and when i click a row it will display the details of that contact on another view. This is how the storyboard looks like:
In my didSelectRowAtIndexPath i have this code:
NewViewController *WVC = [self.storyboard instantiateViewControllerWithIdentifier:#"NewViewController"];
[self.navigationController pushViewController:WVC animated:YES];
When i run the application, the app looks like:
However when i click on a row the details are displayed, but like this:
Can someone let me know where i am going wrong, thanks in advance.
Code in NewViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 20)];
mylabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 125, 300 , 20)];
[nameLabel setText:#"Name :"];
[mylabel setText:_LabelText];
[self.view addSubview:nameLabel];
[self.view addSubview:mylabel];
myPhonelabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 175, 300, 20)];
[myPhonelabel setText:#"Contact Number :"];
PhoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 200, 300, 20)];
[PhoneLabel setText:_phoneNumber];
[self.view addSubview:myPhonelabel];
[self.view addSubview:PhoneLabel];
myEmailLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 240, 300, 20)];
[myEmailLabel setText:#"Email ID :"];
EmailLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 265, 300, 20)];
[EmailLabel setText:_emailId];
[self.view addSubview:myEmailLabel];
[self.view addSubview:EmailLabel];
myBdayLabel =[[UILabel alloc]initWithFrame:CGRectMake(10, 315, 300, 20)];
[myBdayLabel setText:#"Date Of Birth :"];
BdayLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 335, 300, 20)];
[BdayLabel setText:_birthDate];
[self.view addSubview:myBdayLabel];
[self.view addSubview:BdayLabel];
myButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 30, 300, 30)];
[myButton setTitle:#"Contact Details" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor colorWithRed:0xBD/255.0 green:0xBE/255.0 blue:0xC2/255.0 alpha:1];
myButton.layer.cornerRadius = 10;
myButton.clipsToBounds = YES;
[self.view addSubview:myButton];
}
EDIT: (ANSWER)
After a lot of head banging and hair pulling, the problem was solved by replacing
NewViewController *WVC = [self.storyboard instantiateViewControllerWithIdentifier:#"NewViewController"];
with
NewViewController *WVC = [[NewViewController alloc] init];
Honestly i don't know why the previous one didn't work.
If you don't need navigation bar, use to hide it
[self.navigationController setNavigationBarHidden:YES];
I am trying to make a label programmatically and for some reason it is not appearing. I have declared it on the .h file and this is what I have on the .m:
TitleLabel.textColor = [UIColor whiteColor];
TitleLabel.frame =CGRectMake(65, 30, 200, 50);
TitleLabel.textAlignment = NSTextAlignmentCenter;
TitleLabel.alpha = 1;
TitleLabel.font = [UIFont fontWithName:#"BrandonGrotesque-Black" size:23];
TitleLabel.backgroundColor = [UIColor blackColor];
TitleLabel.text = #"TEST";
Can anyone help me with this minor issue. Thanks
You're missing the init, do this:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width ,height)];
First of all you need to create label ojbect with alloc/init methods:
titleLabel = [[UILabel alloc] init];
or
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 30, 200, 50)];
And at the end you need to add it on the view:
[self.view addSubview:titleLabel];
And also, just a little advice - use CamelCase coding style:
http://en.wikipedia.org/wiki/CamelCase
It is best practice to name instances of classes from lower case, like titleLabel, not TitleLabel
I want to set name to UILabel as label1,label2,label3 and so on. For that i don't want to write code for label1,label2,... Now i am using this method.
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
.....
But it's not a good method. I only want to write a single line of code for all labels.
How can i implement this? Is it possible to use %d along with label.
Please help.
just set tag of label and get data by using tag
for (int i=0; i<4; i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
label.tag = i;
[self.view addSubview:label];
}
You can use tag of Label to differentiate from other instance. there is no such logic you are looking and it is not suggestible to differentiate objects.
for (int i = 0; i < 4; i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
label.tag = i;
[self.view addSubview:label];
}
if ([self.view viewWithTag:i]) {
[(UILabel *)[self.view viewWithTag:i] setText:#"Update Text"];
}
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];