Create Two UILabels dynamically using Objective C - ios

In one array I have one string and one more array I want to display this dynamically in custom cell. My array structure is given below. It will change according to data coming from web service.
{
subDescription = (
"Worship Hours 5.00am to 12.30pm & 4.00pm to 10.00pm",
"The temple is open all days of the week"
);
subHeading = Hours;
}
And i tried like this using this format i can display subHeading Value only how to display subDescription array
float xCoordinate=10.0,yCoordinate=30.0,width=320,height=40;
float ver_space=20.0;
for (int i = 0; i <test.count; i++)
{
NSDictionary *lanuage=[test objectAtIndex:i];
NSString *work=[lanuage objectForKey:#"subHeading"];
NSArray *er=[lanuage objectForKey:#"subDescription"];
UILabel *label = [[UILabel alloc] initWithFrame:
CGRectMake(xCoordinate,yCoordinate,width,height)];
label.text = work;
[events.contentView addSubview:label];
yTCoordinate=yTCoordinate+heightT+ver_space;
}

I am not sure that I understand, but why dont you use CollectionView instead of a label. There you can define header cell and sub cells. I think this fits your problem.
In case that you do not want to use collection view you can adjust your code and iterate with loop through your sub items array.
for (NSString *item in er) {
UILabel *label = [[UILabel alloc] initWithFrame:
CGRectMake(xCoordinate,yCoordinate,width,height)];
label.text = item;
[events.contentView addSubview:label];
yCoordinate=yCoordinate+height+ver_space;
}

Related

How to set UILabel height dynamically in Objective c?

I am new in iOS and I am facing problem regarding to create UILabel dynamically. I need to change its hight and the Y position according to data from webservice.
My code is like this
NSMutableArray *remarkarray =[[NSMutableArray alloc] init];
remarkarray=[responsedict valueForKey:#"Key"];
NSString *RemarkString=[NSString stringWithFormat:#"%#",[remarkarray objectAtIndex:0]];
remarklbl.text=RemarkString;
remarklbl.numberOfLines=0;
[remarklbl sizeToFit];
I have create UILabel like this
But its Y Position is fix. I need to create a UILabel in which I can change the height and the Y position according to text in it. Like this
Thanks in Advance!
You can do it very easily by declaring two labels in Storyboard.
Here are steps.
Drag two UILabels in your viewController and select the first one and set constraints like below image.
Select the second label and add following constraints.
Select both labels and set numberOfLines to zero.
Now u can set text dynamically from code.
All you have to do is:
float x=10.0,y=10.0,height=40;
float verticalSpace=20.0;
for (int i = 0; i < 10; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(x, y,50,height)];
label.text = #"dummy text";// It could be from array as well if you want to add different text for each label. Eg: label.text = [arr objectAtIndex:i];
[self.view addSubview:label];
y = y +height+ verticalSpace;
}
You could search around with key word "auto layout dynamic label height"

iPhone - how to select from a collection of UILabels?

I have a few UILabels, any one of which will update according to the index of an NSArray index they represent. I thought of selecting them by their tag
self.displayLabel.tag = myArray[index];
but that changes the tag value to whatever my array is holding at the moment
Using a dictionary for whatever tricks it offers instead of an NSArray doesn't help because i still have to select the correct matching label. This is the effect i want to achieve.
self.|mySelectedLabel|.text = myArray[index];
what should i put in |mySelectedLabel| to get the one i'm looking for?
I'm almost ashamed to ask at my reputation level, but this is stymie-ing me
every search only turns up how to set Labels and change, not the process of selecting
Assuming you have set the tags to the appropriate index to match your
array indices you can use [self.view viewWithTag:index];
Why are you not setting the tag with:
self.displayLabel.tag = index;
Also, you could just loop though an array of labels and find the right one:
for (UILabel *label : labelArray) {
if (label.tag == index) {
label.text = #"I found you!";
}
}
Rather than using tags you can refer to your specific textfields by reference:
// Create an array to hold your textfields
NSMutableArray *textFields = [NSMutableArray array]
// Create your textfields and add them to the array
UITextField *textField;
for (NSUInteger idx = 0: idx++; idx < numberOfTextFieldsYouWant) {
textField = [UITextField alloc] initWithFrame:<whateverYouWant>];
[textFields addObject:textField];
}
Since you are adding the objects to an array, rather than using the tag value 0, 1, 2... you can just access it by it's index in the array
So, for what you want to do you can just do:
textfields[index].text = myArray[index];
It's a lot cleaner, doesn't rely on magic tags, and you have an array of all your dynamic textfields that you can remove, or change in one place.
I think tags are vastly overused, and they aren't necessary in most cases.
Just letting you know I reframed the problem and this eventually worked for me without having to use an array
( with endless experimenting, I sort of bumped into it so I don't know if it constitutes good technique )
the desired label corresponding to the bag weight ( one of a number possible ) displays the right update
- (IBAction)acceptWeight:(UIButton *)sender {
int tempValue = (int) currentWeight;
// current weight comes from a UISegementedController
for (UILabel *labels in self.view.subviews)
{
if (labels.tag == currentWeight)
{
bags[tempValue]++;
labels.text = [NSString stringWithFormat:#"%i",bags[tempValue]];
}
}
totalKilo = totalKilo + (int)currentWeight;
self.totalKilo.text = [NSString stringWithFormat:#"%d",totalKilo];
}

Find iOS UIView by tag dynamically

I'm developing an app for iPhone and I have a strange problem. I tried to solve this by myself but after 3 days I didn't found a solution anyway.
I have a scrollview in which I dynamically create other views and subviews, this is the code:
for (int i=0; i<dim; i++) {
UITextView *posted_nick= [[UITextView alloc] initWithFrame:CGRectMake(paddWidth, heightUpdateImageScrollview+paddHeight/2, screenWidth-2*paddWidth, 37)];
//textview customization...
[imagesScrollView addSubview:posted_nick];
row_images_like = [[UIView alloc] initWithFrame:CGRectMake(paddWidth,heightUpdateImageScrollview+paddHeight+37+heightImageInScrollView,screenWidth-2*paddWidth,80)];
//set the tag = id
row_images_like.tag = [id_image intValue];
UIImageView *like_mini = [[UIImageView alloc] initWithFrame:CGRectMake(0,15,25,25)];
//imageview customization...
//tag = id+1..
NSInteger x = [id_image intValue] + 1;
number_like.tag = x;
[row_images_like addSubview:like_mini];
UITextView *number_like = [[UITextView alloc] initWithFrame:CGRectMake(paddWidth*5/2,10,55,37)];
//textview customization...
//tag = id+2..
NSInteger x = [id_image intValue] + 2;
number_like.tag = x;
[row_images_like addSubview:number_like];
[imagesScrollView addSubview:row_images_like];
}
Now, all works great and when I click on the image view "like_mini", I can find the other views in the same row with the appropriate tag
(UIView *thisView = (UIView*)[imagesScrollView viewWithTag:ID_IMAGE];)
The problem is where I update my scrollview. When the user scrolls to the top, if there are new images to show, I call the same function that creates the views, and all the other views (that already exist) are moved some down. Why, when I try to find a view by tag in my scrollview, all works at the first time, but don't work for the new images created with the same code?
If i remove all the views in the scrollview, before adding the new views, it works. But i don't want to remove the oldest view.
When it works, I have in my console the view (row_images_like) with tag.
When it doesn't work, I receive a _UITextContainerView. What is this?
Hope I explained myself.
Hi there the only reason the images moves down is because you are not assigning the proper tags, please give appropriate value of tag to uiview, uiimageview and uitextview.
row_images_like.tag = [id_image intValue] + 1000;
For fetching the view get it done similarly what you did before only add thousand to it.
UIView *thisView = (UIView*)[imagesScrollView viewWithTag:ID_IMAGE+1000];
Also one error :
number_like.tag = x;
How does the above line object i.e "Number_like" comes before initialising it and change the tag value of other objects to "+2000" and "+3000"
Try removing all views added to scrollview, before loading scrollview again
Write this line above for loop
for (UIView *v in [imagesScrollView subviews])
{
[v removeFromSuperview];
v = nil;
}

Identify UIImageView and UITextView created in loop iOS

I'm working in Xcode and i have a loop cycle in which i dynamically create UIImageViews and UITextviews and insert them in a UIScrollView. In every row, i create 4 UIImageViews and 2 UITextViews, so the code looks like this:
for (int i=0; i<dim; i++){
UITextView *textView1= [[UITextView alloc] initWithFrame:CGRectMake(0, 0, screenWidth/2, 37)];
UITextView *textView2= [[UITextView alloc] initWithFrame:CGRectMake(screenWidth/2, 0, screenWidth/2, 37)];
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0,50,screenWidth/4,100)];
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(screenWidth/4,50,screenWidth/4,100)];
UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(screenWidth/2,50,screenWidth/4,100)];
UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(screenWidth*3/4,50,screenWidth/4,100)];
}
The code to set images in UIImageViews, text in UITextViews and show them in the UIScrollView works great and i don't post it because it's too long.
My problem is: when i click on an image, i call methods in which i want to make change to the ImageViews and TextViews of the same row, how can i identify them?
Detecting the image clicked is not a problem using the recognizer.view, to detect other images of the same row i tried to set a tag to the views and then call "viewWithTag" to find them, but i have problems because the elements of the same row have the same tag value and i make confusion.
My target is for example:
When i click on imageView1 in the first row should change image in imageView3 of the same row.
When i click on imageView3 in the fourth row should change text in textView2 of the same row.
And so on..
Is there any alternative method without using the tag attribute?
I hope I explained myself
You can use the tag property, but without using the same tag more then once. Some little math should do the trick to identify views in the same row by their tags...
BUT:
I wouldn't recommend using tags. Make yourself a custom view class, e.g. MyRow, containing the 6 views belonging together as subviews. From there you can go anywhere.
Got a problem, invent an object. Always works.
BIGGER BUT: I'd strongly recommend to go with UICollectionView or UITableView.
UIView has a "tag" property, which you can use for anything you want. The default value is 0 and it doesn't do anything at all, except store the value.
Typically you would set the tag to be an array index.
So you could do:
textView1.tag = i;
textView2.tag = i;
imageView1.tag = i;
imageView2.tag = i;
imageView3.tag = i;
imageView4.tag = i;
Or perhaps something more clever, like:
textView1.tag = 1000000 + i;
textView2.tag = 2000000 + i;
imageView1.tag = 3000000 + i;
imageView2.tag = 4000000 + i;
imageView3.tag = 5000000 + i;
imageView4.tag = 6000000 + i;
Or maybe you could have an array property mapping them:
#property NSMutableArray tagMap;
textView1.tag = self.tagMap.count;
[self.tagMap addObject:#{#"var": #"textView1", #"index":[NSNumber numberWithInteger:i]}];

Loop, generating buttons with functions from an array

For my iOS app I have an array of text, and I want to generate inside of my UIScrollview a list of text on the right hand side and a list of buttons on the left hand side.
Whenever a user presses a UIButton from the right hand side the text from the left hand side will be copied to the user's device's clipboard...
I figured out how to generate the list of text in UILabels using a for loop and an NSArray. I have no clue how to programmatically generate UIButtons though and I also don't know how to make each button copy the respective string from the array (i.e. button #3 copies string #3 (objectAtIndex #2) from the array)
Here is my code so far:
NSArray *copyText;
copyText = [NSArray arrayWithObjects: #"text to copy 1", "text to copy 2", "text to copy 3", "text to copy 4", nil];
int i = 0;
while (i < [copyText count]) {
UILabel *copyLabels = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (ScrollView1.frame.size.width*2/3), 25)];
copyLabels.center = CGPointMake(ScrollView1.frame.size.width*2/3, ((i*25)+15));
copyLabels.textAlignment = UITextAlignmentCenter;
copyLabels.textColor = [UIColor blackColor];
copyLabels.backgroundColor = [UIColor clearColor];
copyLabels.font = [UIFont fontWithName:#"System" size:(16.0)];
[ScrollView1 addSubview:copyLabels];
copyLabels.text = [NSString stringWithString:[copyLabels objectAtIndex:i]];
i ++;
}
Found my answer here: Add a multiple buttons to a view programmatically, call the same method, determine which button it was
The UIButton has a "tag" field in which numeric values can be assigned so my loop can keep track of which button is which. Genius.

Resources