setFrame of UILabel property of .xib doesn´t work - ios

i make the next method in a viewController
-(void)setupDocsLabel:(NSMutableArray *)documents{
self.lDocumentos.frame = CGRectMake(self.lDocumentos.frame.origin.x, kFirstLabelYPosition+actualLabelYPos,self.lDocumentos.frame.size.width,lDocumentos.frame.size.height);
self.Documentos.frame = CGRectMake(self.Documentos.frame.origin.x, kFirstLabelYPosition+actualLabelYPos,self.Documentos.frame.size.width,self.Documentos.frame.size.height);
actualLabelYPos +=20.0;
for (DocInformation *doc in documents) {
NSString *textLabel = [doc.documentDescription stringByAppendingString:#" :"];
UIFont *lblFont = lDocumentos.font;
CGSize sizeFont = [textLabel sizeWithFont:lblFont forWidth:120.0 lineBreakMode:NSLineBreakByTruncatingTail];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(lDocumentos.frame.origin.x+20, kFirstLabelYPosition+actualLabelYPos,sizeFont.width,sizeFont.height)] retain];
label.text = textLabel;
[label setFont:lblFont];
[label setTextColor:lDocumentos.textColor];
[label setBackgroundColor:[UIColor clearColor]];
//[label setLineBreakMode:NSLineBreakByTruncatingTail];
NSString *textDoc = doc.cdgoDocum;
UIFont *lblFontDoc = Documentos.font;
CGSize sizeFontDoc = [textDoc sizeWithFont:lblFontDoc];
UILabel *labelDoc = [[[UILabel alloc] initWithFrame:CGRectMake(label.frame.origin.x+label.frame.size.width+20, kFirstLabelYPosition+actualLabelYPos,sizeFontDoc.width,lDocumentos.frame.size.height)] retain];
labelDoc.text = textDoc;
[labelDoc setFont:lblFontDoc];
[labelDoc setTextColor:lDocumentos.textColor];
[labelDoc setBackgroundColor:[UIColor clearColor]];
[self.scrollView addSubview:label];
[self.scrollView addSubview:labelDoc];
[label release];
[labelDoc release];
actualLabelYPos+=20.0;
}
//[self.view setNeedsDisplay];
//[self.view setNeedsLayout];
}
New Labels add to the scrollView well, but self.lDocumentos (a IBOutlet of the .xib) don´t change his position.
Thanks for the help!!

I've solved it, the problem was that the .xib had enabled Use AutoLayout in the File Inspector.

Related

Generated subview is always behind image view

viewController.m
-(void) viewDidLoad
{
[super viewDidLoad];
[self addScoreRowViewWithIndexLeft:(++i) andName:[scoreDict objectForKey:#"name"] andScore:[[scoreDict objectForKey:#"points"] integerValue]];
}
-(void) addScoreRowViewWithIndexLeft:(NSInteger)index andName: (NSString *)name andScore: (NSInteger)score {
UIView *scoreRowView = [[UIView alloc] initWithFrame:CGRectMake(128, 112 + index * 80, 280, 20)];
UILabel *indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 20, 20)];
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 20, 180, 20)];
UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(330, 20, 50, 20)];
UIColor *color = [UIColor darkGrayColor];
[indexLabel setText:[#((int)index) stringValue]];
[indexLabel setFont:[UIFont fontWithName:#"DINAlternate-Bold" size:18]];
[indexLabel setTextColor:color];
[nameLabel setText:name];
[nameLabel setFont:[UIFont fontWithName:#"DINAlternate-Bold" size:18]];
[nameLabel setTextColor:color];
[scoreLabel setText:[#((int)score) stringValue]];
[scoreLabel setFont:[UIFont fontWithName:#"DINAlternate-Bold" size:18]];
[scoreLabel setTextColor:color];
[scoreRowView addSubview:indexLabel];
[scoreRowView addSubview:nameLabel];
[scoreRowView addSubview:scoreLabel];
[self.view addSubview:scoreRowView];
}
I added my ImageView in my Storyboard as a subview of my View.
When I delete the ImageView, everything is ok. I get displayed my other code generated subviews. But when I add the ImageView in my Storyboard, it's on top of my other subviews. If I give my ImageView an alpha of 0.3 I can see my subviews shining through.
What's wrong with my code? I also tried;
[self.view bringSubviewToFront:scoreRowView];
but it doesn't work.
What can I do?
Try to change zPosition of your ImageView and scoreRowView. You can do this this way:
Connect your ImageView with your ViewController
In bottom of ViewDidLoad add something like that:
yourImageView.layer.zPosition = 1;
scoreRowView.layer.zPosition = 2;
I'm suspecting that [self.view bringSubviewToFront:scoreRowView]; doesn't work because you invoke it in ViewDidLoad where view in not fully work. You can try to invoke it in e.g. ViewDidAppear and check if that will work.

getting adjustsFontSizeToFitWidth to work with constraints for snug content

This is a little view control I've written to test out the issue I'm having with constraints and adjustsFontSizeToFitWidth. Its a little thing that add's and remove's words from a label to see how various content sizes in label_title effect the layout. The desired layout is snug around the label_title (with the label_count taking up the rest of the room). Unfortunately its not quite snug yet.
Here are 3 view states I've screen captured to illustrate the lack of snugness on the blue title which I'm trying to fix.
Following that is the full source of a view controller that will run this view.
#import "MainViewController.h"
#interface MainViewController ()
// stuff to test our problem with
#property(nonatomic,strong) UIButton * addButton;
#property(nonatomic,strong) UIButton * removeButton;
// our problem
#property(nonatomic,strong) UIView * view_labels;
#property(nonatomic,strong) UILabel * label_title;
#property(nonatomic,strong) UILabel * label_count;
#end
#implementation MainViewController
// synthesize for the views constraint mapping
#synthesize label_title;
#synthesize label_count;
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor grayColor]];
self.addButton = [self buttonWithTitle:#"Add Word"
selector:#selector(addButtonPressed) color:[UIColor greenColor]];
self.removeButton = [self buttonWithTitle:#"Remove Word"
selector:#selector(removeButtonPressed) color:[UIColor redColor]];
// view containing our troublesome labels
self.view_labels = [[UIView alloc] init];
[self.view addSubview:self.view_labels];
//[self.view_labels setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view_labels setBackgroundColor:[UIColor lightGrayColor]];
// label row 1
self.label_title = [self labelWithText:#"word" defaultFontSize:30.0 bgcolor:[UIColor blueColor]];
[self.view_labels addSubview:self.label_title];
// label row 2
self.label_count = [self labelWithText:#"77" defaultFontSize:40.0 bgcolor:[UIColor purpleColor]];
[self.view_labels addSubview:self.label_count];
NSDictionary *views = NSDictionaryOfVariableBindings(label_title, label_count);
NSDictionary *metrics = #{#"pad":#2};
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"V:|-(==pad)-[label_title(<=44)]-(==pad)-[label_count]-(==pad)-|"
options:0
metrics:metrics
views:views]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"|-(==pad)-[label_title]-(==pad)-|"
options:0
metrics:metrics
views:views]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"|-(==pad)-[label_count]-(==pad)-|"
options:0
metrics:metrics
views:views]];
}
#pragma mark - boiler plate stuff
-(UILabel*) labelWithText:(NSString*) text defaultFontSize:(float) fontSize bgcolor:(UIColor*) color
{
UILabel * label = [[UILabel alloc] init];
[label setTranslatesAutoresizingMaskIntoConstraints:NO];
[label setTextAlignment:NSTextAlignmentCenter];
[label setTextColor:[UIColor whiteColor]];
[label setAdjustsFontSizeToFitWidth:YES];
[label setNumberOfLines:0];
[label setMinimumScaleFactor:0.6];
[label setFont:[UIFont boldSystemFontOfSize:fontSize]];
[label setBackgroundColor:color];
[label setText:text];
return label;
}
-(UIButton*) buttonWithTitle:(NSString*) title selector:(SEL) selector color:(UIColor*) color
{
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:button];
[button setTitle:title forState:UIControlStateNormal];
[button setBackgroundColor:color];
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
return button;
}
-(void) viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
float SQUARE_SIZE = 100;
[self.addButton setFrame:CGRectMake(0, self.view.frame.size.height-44.0, self.view.frame.size.width, 44.0)];
[self.removeButton setFrame:CGRectMake(0, self.view.frame.size.height-(44.0*2), self.view.frame.size.width, 44.0)];
[self.view_labels setFrame:CGRectMake(SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE)];
}
-(void) addButtonPressed
{
NSString * text = self.label_title.text;
if (text == nil){
text = #"";
}
text = [NSString stringWithFormat:#"%#%#",text,#" word"];
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[self.label_title setText:text];
}
-(void) removeButtonPressed
{
NSString * text = self.label_title.text;
if (text == nil || [text isEqualToString:#""]){
return;
}
NSArray * array = [text componentsSeparatedByString:#" "];
text = #"";
for (int i = 0; i < [array count]-1; i++){
NSString * token = [array objectAtIndex:i];
text = [NSString stringWithFormat:#"%# %#",text,token];
}
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[self.label_title setText:text];
}
#end
If you are trying to fit the text in UILabel replace the your method by the following.
-(UILabel*)labelWithText:(NSString*) text defaultFontSize:(float) fontSize bgcolor:(UIColor*) color
{
#autoreleasepool {
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50,100,100)];
[label setTextAlignment:NSTextAlignmentCenter];
[label setTextColor:[UIColor whiteColor]];
[label setAdjustsFontSizeToFitWidth:YES];
[label setFont:[UIFont boldSystemFontOfSize:fontSize]];
[label setBackgroundColor:color];
[label setText:text];
[label setNumberOfLines:0];
while (fontSize > 0.0)
{
CGSize size = [text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.frame.size.width, label.frame.size.height) lineBreakMode:NSLineBreakByWordWrapping];
if (size.height <= label.frame.size.height)
break;
fontSize -= 1.0;
}
[label setFont:[UIFont boldSystemFontOfSize:fontSize]];
return label;
}
}

How to adjust/set UIView size according to UILabel content

i used loop to create dynamic UIView. But i could not create UIView according the size of UILabel kindly help me as i could do further proceed.. need your help.. i will appreciate
for(NSString *item in myArray)
{
length = item.length;
self.custom=[[CustomView alloc]init];
self.custom.Label = [[UILabel alloc]initWithFrame:CGRectMake(40, yAxis, 100+item.length, 44)];
[self.custom setFrame:self.custom.Label.frame];
[self.custom sizeToFit];
yAxis=yAxis+50;
self.custom.tag=200+a;
a++;
[newViews addObject:self.custom];
length = item.length;
self.custom.Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5,length+20,40)];
self.custom.button=[[UIButton alloc]initWithFrame:CGRectMake(85,10,12,10)];
[self.custom.Label setText:item];
// Tell the label to use an unlimited number of lines
[self.custom.Label setNumberOfLines:1];
[self.custom.Label sizeToFit];
self.custom.Label.textAlignment = UITextAlignmentCenter;
![this is image i have created uiview according the array values where uilabel more width than uiview so i need to set uiview according the uilabel][1]
UIImage *btnImage = [UIImage imageNamed:#"button_droparrow.png"];
[self.custom.button setImage:btnImage forState:UIControlStateNormal];
[self.custom.button addTarget:self
action:#selector(buttonPressed:)forControlEvents:UIControlEventTouchDown];
self.custom.button.tag=self.custom.button.tag+a;
self.custom.backgroundColor=[UIColor greenColor];
custom.Label.text=item;
custom.Label.backgroundColor = [UIColor yellowColor];
[self.custom addSubview:self.custom.button];
[self.custom addSubview:custom.Label];
// [self.custom addSubview:cv];
[self.view addSubview:self.custom];
}
You can get the size based of the UILabel content using the following code.
NSString * countString = aLbl.text;
CGSize s = [countString sizeWithFont:[UIFont fontWithName:#"Helvetica-Light" size:12] constrainedToSize:CGSizeMake(100, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
And alter the Size of any views using following code
CGRect rect = aView.frame;
rect.size.width = s.width;
rect.size.height = s.height;
aView.frame = rect;
try size to fit for label or view
[yourlabel sizeToFit];
or
[yourview sizeToFit];
Change setNumberOfLines:0 and run your app. I hope it helps you. And I request to you, if this is help you please vote me.
[self.custom.Label setNumberOfLines:0];
self.custom.Label.text = #"xyz.................";
[self.custom.Label sizeToFit];

How can I know which view I am clicking? And why the button no reaction?

I am designing a viewcontroller which have several button bar, each bar canbe clicked and show a content view.like:
http://i.stack.imgur.com/m5V4Q.png
When I click the buttonbar, it's frame will expand bigger, and you can see the content in it(which is a button).
First, a bar button(320*30 size) and a contentView is a set, they are subviews of listview. And several listview makes the whole view.
Second, when I click it, the list view will expand from 320*30 to 320*180, the contentview in it will expand from 300*0 to 300 * 130(20 pixel of margin). and I set the clipsToBounds of contentview to Yes to make sure the content in contentview won't show when the frame height is 0.
Now,the click can show the contentview exactly as I want. But here is a problem: I can't click the button in it, and I tried to set their userInteractionEnabled to yes . And even I set the contentview as user-defined,which I set the view's userInteractionEnabled to yes and overwrite the touchbegin function to show a alertView. Those test are failed, no reaction found. And I checked and sure that there shouldn't be any view covered it.
What's the reason might be?
The code of setup the list view is:
NSArray *array = [titleArray objectAtIndex:i];
NSString *ShenSuoTitle = [array objectAtIndex:0];
NSString *ShenSuoContent = [array objectAtIndex:1];
UIView *listView = [[UIView alloc] initWithFrame:CGRectMake(0, totalHeight, 320, ShenSuoViewHeight)];
[listView setBackgroundColor:[UIColor blackColor]];
[listView setTag:[[NSString stringWithFormat:#"%d",(i+1)] intValue]];
[self.scrollView addSubview:listView];
totalHeight = totalHeight + 1 + ShenSuoViewHeight;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[titleView setBackgroundColor:[UIColor whiteColor]];
[titleView setTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
[listView addSubview:titleView];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:[[NSString stringWithFormat:#"%d2",i+1] intValue]];
[btn setFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[btn addTarget:self action:#selector(reSetFrame:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:btn];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(5, 12, 30, 30)];
img.image = [UIImage imageNamed:#"list_ico.png"];
[img setTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
[titleView addSubview:img];
NSLog(#"img:%f,%f",img.frame.origin.y,img.frame.size.height);
UILabel *labTitle = [[UILabel alloc] initWithFrame:CGRectMake(35, 15, 100, 25)];
labTitle.textColor = [UIColor blackColor];
labTitle.backgroundColor = [UIColor clearColor];
labTitle.font = [UIFont boldSystemFontOfSize:15];
labTitle.text = ShenSuoTitle;
[titleView addSubview:labTitle];
NSLog(#"labTitle:%f,%f",labTitle.frame.origin.y,labTitle.frame.size.height);
//add a label for selected
UILabel *selectedLabel = [[UILabel alloc] initWithFrame:CGRectMake(214, 14, 86, 21)];
selectedLabel.textColor = [UIColor grayColor];
//selectedLabel.alpha = 0.75;
selectedLabel.backgroundColor = [UIColor clearColor];
selectedLabel.text = #"All";
selectedLabel.font = [UIFont boldSystemFontOfSize:15];
[selectedLabel setTag:[[NSString stringWithFormat:#"%d5",i+1] intValue]];
[titleView addSubview:selectedLabel];
NSLog(#"selectedLabel:%f,%f",selectedLabel.frame.origin.y,selectedLabel.frame.size.height);
UIView *content = [[UIView alloc] initWithFrame:CGRectMake(10, 10 + ShenSuoViewHeight, 300, 0)];
content.backgroundColor = [UIColor grayColor];
UILabel *testLa = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
testLa.text = #"Label";
UIButton *testBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testBut.frame = CGRectMake(0, 40, 100, 30);
testBut.titleLabel.textColor = [UIColor greenColor];
testBut.titleLabel.text = #"Button";
content.tag = [[NSString stringWithFormat:#"%d4",i+1] intValue];
[content addSubview:testLa];
[content addSubview:testBut];
content.clipsToBounds = YES;
[testLa release];
[listView addSubview:content];
[content release];
[labTitle release];
[img release];
[titleView release];
[listView release];
the code handle the click to make the list view expands is:
UIView *titleView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
titleView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:0.9];
UIView *listView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d",i+1] intValue]];
listView.frame = CGRectMake(0, totalHeight, 320, ShenSuoViewHeight);
totalHeight = totalHeight + 1 + 20 + 180 + ShenSuoViewHeight;
UIView *content = [self.view viewWithTag:[[NSString stringWithFormat:#"%d4",i+1] intValue]];
content.frame = CGRectMake(10, 10 + ShenSuoViewHeight, 300, 180);
UIImageView *img = (UIImageView*)[self.view viewWithTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
img.image = [UIImage imageNamed:#"list_ico_d.png"];
I'm not sure I follow your situation exactly, and some code would be nice. To answer your question though, the reason is most likely that the touch is being intercepted elsewhere. It may also be that you just forgot to connect an action method to the button. Try using breakpoints or NSLogs to see what methods are being triggered and which aren't.
Thanks a lot. It is due to my fault. The code of expanding the bar which setting the listview's frame is wrong, it should add in the expanded height. This leads the visual is ok but the clicking not be handled.
So this problem closed.

iOS create UILabels dynamically

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

Resources