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"
Related
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;
}
below are some other properties of the label. obviously nstextalignmentleft is not what i'm going for. having trouble understanding where to enter coordinates.
self.lblTimer = [[UILabel alloc] init];
self.lblTimer.translatesAutoresizingMaskIntoConstraints = NO;
self.lblTimer.textAlignment = NSTextAlignmentLeft;
self.lblTimer.font = [UIFont systemFontOfSize:10];
self.lblTimer.textColor = [UIColor redColor];`self.lblTimer = [[UILabel alloc] init];
I think you're misunderstanding what textAlignment does. This simply controls the alignment within the label, not on the screen. If you want to position the label on screen you must change its frame property:
self.lblTimer.frame = CGRectMake(x,y,width,height);
As you've set self.lblTimer.translatesAutoresizingMaskIntoConstraints to NO I am guessing you want to use Auto Layout and if so, I highly recommend taking a look at Masonry.
An alternative is to set the frame property on the label with requires a CGRect where you give it the x and y coordinates as well as the width and height.
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;
}
Hello, i want to create relative constraints between 3 elements.
And when resized from 4 to 3.5inch those constraints resizing to the new size while objects keep their size;
There is a way to create flexible spacing between elements with the help of constraints. The way is to use a view for the spacing not a constraint. There is event a sample in the official documentation.
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/AutolayoutPG/AutoLayoutbyExample/AutoLayoutbyExample.html
Look at the section named "Spacing and Wrapping".
Don't use static height and width. use following code for calculating height and width.
int width = self.view.frame.size.width;
int height = self.view.frame.size.height;
In this way set X and Y co-ordinate for your element.
Refer this Code -
int imageX = 2,imageY = 2;
int count1 = 0;
for (int i = 0; i < [mainMenuColumn1Array count]; i++) {
count1++;
MenuClass *menuClass = [[MenuClass alloc] init];
menuClass = [mainMenuColumn1Array objectAtIndex:(count1 - 1)];
UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
menuBtn.frame = CGRectMake(imageX, imageY, (width/2)-4, (height/3)-4);
menuBtn.tag = count1;
[menuBtn addTarget:self action:#selector(mainMenu1Action:) forControlEvents:UIControlEventTouchUpInside];
menuBtn.backgroundColor = [UIColor colorWithRed:17.0/255.0 green:116.0/255.0 blue:239.0/255.0 alpha:1.0];
[mainView1 addSubview:menuBtn];
imageY = imageY + height/3;
imageX = 2;
}
Here I have add UIButtons dynamically. And I set XY co-ordinates dynamically. This is a generic code for all size devices.
You can't do this in interface builder, as far as I am aware, because you can't specify multipliers on constraints via interface builder. However, you can do it quite easily in code, particularly using a nice auto layout helper category available via Github or cocoapods. (Disclaimer - I wrote the category!).
The category contains a method to distribute an array of views evenly along a specified axis, and under the hood it creates constraints using multipliers of the containing view's dimensions - so for two views, the centres would be 0.33 and 0.66 of the way along the relevant axis, for example.
To use this for a view primarily built in interface builder, you'd use placeholder constraints (removed at run time) then add the category constraints after viewDidLoad.
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]}];