How Would I insert an array of strings into UILabel, and have them load or count by 1?
I'm trying to load the following array into the UILabel:
self.array = [NSArray arrayWithObjects:#"Testing 1",#"Testing 2",#"Testing 3", nil];
Here I am using an UIScrollView that automatically changes from each slide to each slide:
for (int i=1; i<=3; i++) {
// create label
UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,30)];
text.text = [NSString stringWithFormat:#"%#", self.array];
text.font=[UIFont fontWithName:#"HelveticaNeueLTStd-LtCn" size:15];
text.textColor = [UIColor darkGrayColor];
text.textAlignment =NSTextAlignmentCenter;
// create imageView
UIImageView *lblView = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 325, 280, 240)];
// set label
[lblView addSubview:text];
// apply tag to access in future
lblView.tag=i+1;
// add to scrollView
[scrMain addSubview:lblView];
}
I have an example of how to do it with UIImageView:
for (int i=1; i<=3; i++) {
// create image
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"sti%02i.png",i]];
// create imageView
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width + 23, 60, 280, 260)];
// set scale to fill
imgV.contentMode=UIViewContentModeScaleToFill;
// set image
[imgV setImage:image];
// apply tag to access in future
imgV.tag=i+1;
// add to scrollView
[scrMain addSubview:imgV];
}
The images are in my project and are numbered sti01, sti02, and so on... It then counts by one for each images and then loads them into the scrollview. I hope this helps!
The y-position of the label should be adjusted according to the index.
You can use the array count instead of a fixed '3'.
You don't need an UIImageView in between.
You can try this:
for (int i = 0; i < self.array.count; i++) {
// create label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, i*30, self.view.frame.size.width,30)];
label.text = [self.array objectAtIndex:i];
label.font = [UIFont fontWithName:#"HelveticaNeueLTStd-LtCn" size:15];
label.textColor = [UIColor darkGrayColor];
label.textAlignment = NSTextAlignmentCenter;
// add to scrollView
[scrMain addSubview:label];
}
If I'm understanding you correctly, you need to make the following change:
text.text = [NSString stringWithFormat:#"%#", [self.array objectAtIndex:i]];
This will pull out the ith object from your array of strings and set that as the text of your UILabel.
Why don't you loop through the array instead of trying to assign the text to the whole array like this:
for (int i=0; i<3; i++) {
// create label
UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,30)];
text.text = [NSString stringWithFormat:#"%#", [self.array objectAtIndex:i]];
text.font=[UIFont fontWithName:#"HelveticaNeueLTStd-LtCn" size:15];
text.textColor = [UIColor darkGrayColor];
text.textAlignment =NSTextAlignmentCenter;
// create imageView
UIImageView *lblView = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 325, 280, 240)];
// set label
[lblView addSubview:text];
// apply tag to access in future
lblView.tag=i+1;
// add to scrollView
[scrMain addSubview:lblView];
}
That way you're just pulling that one string out, assigning it as the UILabel's text, then adding it as a subview to the appropriate imageView.
Related
In a for loop, I'm trying to add Labels to a ScrollView based on a number given by the user. Here is the code in Objective-C:
NSInteger numberOfViews = [self.textfieldNumViews.text intValue];
for (int i = 0; i < numberOfViews ; i++){
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 300, 12)];
label.text = [NSString stringWithFormat:#"%d",i];
label.backgroundColor = [UIColor lightGrayColor];
[self.scrollviewViewContainer addSubview:label];
}
The problem is that the labels are overlapping each other inside the ScrollView, when I really want them to just display one after the other.
Image I understand that this is due to how addSubview behaves, placing views on top of other views, but is there any way I can prevent that? Or some other function I could use? I feel like this should be an easy task but I can't seem to figure it out. Is there something like a vertical layout property I can add to the ScrollView? Or add margins to the labels?
You need to set the y lower for each subview. Check the third line:
NSInteger numberOfViews = [self.textfieldNumViews.text intValue];
for (int i = 0; i < numberOfViews ; i++){
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, (i*20), 300, 12];
label.text = [NSString stringWithFormat:#"%d",i];
label.backgroundColor = [UIColor lightGrayColor];
[self.scrollviewViewContainer addSubview:label];
}
I am continuously facing problem to create uitextfield programmatically inside a loop and make all them in editable mode?
I have created successfully text fields inside the loop but problem is that. the last one i.e. the last text field is in editable mode.
EX:
suppose an array (myarray) has 5 values.
-(void)createDynamic_textfld{
UITextField *roomratetxt;
UIScrollView *scrlsummery;
NSMutableArray *myarray = [NSMutableArray arrayWithObjects:#[#"one",#"two",#"three",#"four",#"five"], nil];
for (int i = 0; [myarray count]; i ++){
roomratetxt = [[UITextField alloc]init];
roomratetxt.frame = CGRectMake(200, 0, 90, 20);
roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lighttextcolor.png"]];
roomratetxt.backgroundColor = [UIColor clearColor];
roomratetxt.font = [UIFont systemFontOfSize:12];
roomratetxt.text = [NSString stringWithFormat:#"%#",[myarray objectAtIndex:i]];
roomratetxt.textAlignment = NSTextAlignmentRight;
[roomratetxt setTag:i];
[roomratetxt setDelegate:(id)self];
[scrlsummery addSubview:roomratetxt];
}
}
i want to make all 5 textfields in editable mode. but as of now, the last textfield is only in editing mode and the rest all are non editable mode.
Can anyone plz suggest me some ideas ?How to achieve this
Thanks in advance.
Ajeet Kumar
First of all you need to provide different frames for that you can them in different position. Your above code looks correct and it must be in editable mode. I execute below snippet on my project and it works like a charm. I am assuming you are using Scrollview and added all textfield in it. Only thing make sure you are not disabling them from
(BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
or
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string; // return NO to not change
text
for (int i = 0; [myarray count]; i ++){
roomratetxt = [[UITextField alloc]init];
roomratetxt.frame = CGRectMake(200, i*20, 90, 20);
roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lighttextcolor.png"]];
roomratetxt.backgroundColor = [UIColor clearColor];
roomratetxt.font = [UIFont systemFontOfSize:12];
roomratetxt.text = [NSString stringWithFormat:#"%#",[myarray objectAtIndex:i]];
roomratetxt.textAlignment = NSTextAlignmentRight;
[roomratetxt setTag:i];
[roomratetxt setDelegate:(id)self];
[scrlsummery addSubview:roomratetxt];
}
-(void)createDynamic_textfld{
UITextField *roomratetxt;
UIScrollView *scrlsummery;
NSMutableArray *myarray = [NSMutableArray arrayWithObjects:#[#"one",#"two",#"three",#"four",#"five"], nil];
for (int i = 0; [myarray count]; i ++){
roomratetxt = [[UITextField alloc]init];
roomratetxt.frame = CGRectMake(200, i*0, 90, 20);
roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lighttextcolor.png"]];
roomratetxt.backgroundColor = [UIColor clearColor];
roomratetxt.font = [UIFont systemFontOfSize:12];
roomratetxt.text = [NSString stringWithFormat:#"%#",[myarray objectAtIndex:i]];
roomratetxt.textAlignment = NSTextAlignmentRight;
[roomratetxt setTag:i];
[roomratetxt setDelegate:(id)self];
[scrlsummery addSubview:roomratetxt];
}
}
The frame of all 5 textField are putting in the same position. You might need to change their frame as well.
for (int i = 0; [myarray count]; i ++){
roomratetxt = [[UITextField alloc] init];
roomratetxt.frame = CGRectMake(200, i*60, 90, 20);
roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lighttextcolor.png"]];
roomratetxt.backgroundColor = [UIColor clearColor];
roomratetxt.font = [UIFont systemFontOfSize:12];
roomratetxt.text = [NSString stringWithFormat:#"%#",[myarray objectAtIndex:i]];
roomratetxt.textAlignment = NSTextAlignmentRight;
[roomratetxt setTag:i];
[roomratetxt setDelegate:(id)self];
[scrlsummery addSubview:roomratetxt];
}
I'm able to add a label and text field dynamically but the image doesn't display unless I used the actually name of the image.
This works
UIImage *individualTopImage = [UIImage imageNamed:#"photo1.png"];
I need to use the for loop and display all the images in the array.
//lets create views
NSInteger numberTopOfViews = topImages.count;
for (int i = 0; i < numberTopOfViews; i++) {
//set the origin of the sub view
CGFloat myOrigin = i * self.view.frame.size.width;
//create the sub view and allocate memory
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
//set the background to white color
myView.backgroundColor = [UIColor whiteColor];
//create a label and add to the sub view
CGRect myFrame = CGRectMake(10.0f, 10.0f, 200.0f, 25.0f);
UILabel *myLabel = [[UILabel alloc] initWithFrame:myFrame];
myLabel.text = [NSString stringWithFormat:#"This is page number %d", i];
myLabel.font = [UIFont boldSystemFontOfSize:16.0f];
myLabel.textAlignment = NSTextAlignmentLeft;
[myView addSubview:myLabel];
//create a text field and add to the sub view
myFrame.origin.y += myFrame.size.height + 10.0f;
UITextField *myTextField = [[UITextField alloc] initWithFrame:myFrame];
myTextField.borderStyle = UITextBorderStyleRoundedRect;
myTextField.placeholder = [NSString stringWithFormat:#"Enter data in field %i", i];
myTextField.tag = i+1;
[myView addSubview:myTextField];
//create a uiimage and add to sub view
myFrame.origin.y +=myFrame.size.height +50.f;
//UIImage *individualTopImage = [UIImage imageNamed:#"photo1.png"];
UIImage *individualTopImage = [UIImage imageNamed:[NSString stringWithFormat:#"%#.png",[topImages objectAtIndex:i]]];
UIImageView *topImageView = [[UIImageView alloc] initWithImage: individualTopImage];
[topImageView setFrame:CGRectMake(0, 0, myFrame.size.width,myFrame.size.height )];
[myView addSubview:topImageView];
//set the scroll view delegate to self so that we can listen for changes
self.myScrollView.delegate = self;
//add the subview to the scroll view
[self.myScrollView addSubview:myView];
}
I want to add a Label and an Image in titleView of my NavigationItem. I am adding UILabel and UIImageView to a UIView and setting it as titleView of the navigation Item. Things are being added, but I am not able to calculate the length of label and place image next to it.
My code is :-
// Title Name + Image
UIView *titView = [[UIView alloc] initWithFrame:self.navigationItem.titleView.bounds];
self.navigationItem.titleView = titView;
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(-10, -20, 150, 30)];
title.text = #"Bunny ";
[title setTextColor:[UIColor orangeColor]];
[titView addSubview:title];
float x2 = 30; //+ title.bounds.size.width;
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"return" ]];
img.frame = CGRectMake(x2, -20, img.bounds.size.width, 30);
[titView addSubview:img];
I am looking for some way to calc the width text and set accordingly the width of the label. Right now I have set the width of the label as 150. And secondly, calc the x position to place the image just next to the label.
Tried many ways, but nothing works as expected. How can I achieve this ? Can you he give some guidelines such that regardless of length of text of label, things works as expected and UIView gets placed in the center of navigation item.
Any help is highly appreciated. Thanks.
You can call
[title sizeToFit];
after setting its properties and it will size itself to match the contained string.
Set the imageViews x origin to
CGRectGetMaxX(title.frame) + desiredSpacing;
And it could help to subclass UIView to achieve the final results by overwriting layoutSubviews and placing your views there, since the titleView's frame can be adjusted by the navigationBar...basically like this:
#implementation TitleView{
UILabel *_titleLabel;
UIImageView *_img;
}
- (id)initWithTitle:(NSString *)title andImage:(UIImage *)image
{
self = [super init];
if (self) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.text = title;
[_titleLabel setTextColor:[UIColor orangeColor]];
[_titleLabel sizeToFit];
[self addSubview:_titleLabel];
_img = [[UIImageView alloc] initWithImage:image];
[self addSubview:_img];
}
return self;
}
- (void)layoutSubviews{
CGFloat spacingBetweenTextAndImage = 0;
CGFloat width = CGRectGetWidth(_titleLabel.frame)+CGRectGetWidth(_img.frame)+spacingBetweenTextAndImage;
CGFloat x = (CGRectGetWidth(self.bounds)-width)/2;
_titleLabel.frame = CGRectMake(x, (CGRectGetHeight(self.bounds)-CGRectGetHeight(_titleLabel.bounds))/2, CGRectGetWidth(_titleLabel.bounds), CGRectGetHeight(_titleLabel.bounds));
x+=CGRectGetWidth(_titleLabel.bounds)+spacingBetweenTextAndImage;
_img.frame = CGRectMake(x, (CGRectGetHeight(self.bounds)-CGRectGetHeight(_img.bounds))/2, CGRectGetWidth(_img.bounds), CGRectGetHeight(_img.bounds));
}
#end
use in your viewController:
UIView *titleView = [[TitleView alloc] initWithTitle:#"Bunny" andImage:[UIImage imageNamed:#"return" ]];
self.navigationItem.titleView = titleView;
Sometimes I want my view to contain 5 UILabels, sometimes 3 and sometimes n.
The number of UILabels depends on data that's fetched from a website.
You'll have to make them in code instead of interface builder
for (int i = 0; i < n; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(/* where you want it*/)];
label.text = #"text"; //etc...
[self.view addSubview:label];
[label release];
}
A generic answer for a generic question:
while (labelsToDisplay)
{
UILabel *label = [[UILabel alloc] initWithFrame:aFrame];
[label setText:#"someText"];
[aViewContainer addSubview:label];
[label release];
}
NSArray *dataArray;
float xCoordinate=10.0,yCoordinate=10.0,width=100,height=40;
float ver_space=20.0;
for (int i = 0; i <dataArray.count; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(xCoordinate,yCoordinate,width,height)];
label.text = [dataArray objectAtIndex:i];
[self.view addSubview:label];
yCoordinate=yCoordinate+height+ver_space;
}
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(125, 12,170,20)];
lbl.text=#"IOS";
lbl.textAlignment = NSTextAlignmentCenter;
lbl.textColor = [UIColor whiteColor];
lbl.font = [UIFont fontWithName:#"AlNile" size:10.0];
lbl.backgroundColor=[[UIColor redColor]colorWithAlphaComponent:0.5f];
lbl.layer.borderColor=[UIColor blackColor].CGColor;
lbl.layer.borderWidth=1.0f;
lbl.layer.cornerRadius = 6.0f;
[self.view addSubview:lbl];
UILabel *lblTitle=[[UILabel alloc]init];
[lblTitle setFrame:CGRectMake(0, 0, 100, 100)];
[lblTitle setText:#"MAK"];
[lblTitle setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:lblTitle];
-Here UILable will be created dynamically.
-but property will be set differently.
Create a TextView to show the text on the labels and a NSArray to contain the data.
For more information:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html
http://developer.apple.com/library/ios/documentation/uikit/reference/UITextView_Class/Reference/UITextView.html