Stuck With UIViewAutoresizingMask - iphone

Hi I am New in iOS Development I added UITableViewCell With Custom Cell Design in which I
added two labels in one Cell for which I added
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(/* where you want it*/)];
label.text = #"text"; //etc...
label.autoresizingMask=UIViewAutoresizingFlexibleWidth;
[self.contentView addSubView:label];
[label release];
where my First Label is masking but second Label is Sticking while repositioning table
I am Using iOS6

Related

Custom cell label text color

I want to change UILabel text color in initWithStyle method of UITableViewCell. But color is not getting changed.
But when color changing is done in cellForRowAtIndexPath method,color is getting changed. Why?
Depending how you are using your label you just need to set textColor property:
For creating tableViewCell in - (UITableViewCell*)tableView:(UITableView *)tableView method I think you have used like following:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
there may be possibility that you have your style is either UITableViewCellStyleDefault or UITableViewCellStyleValue1 orUITableViewCellStyleValue2orUITableViewCellStyleSubtitle`.
Now whatever style you are using if you are adding your own label than use:
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100,25)];
lbl.text = #"your Text";
[lbl setTextColor:[UIColor greenColor]];
and if you are not adding your own labels and using default label which is textLabel or detailTextLabel of UITableViewCell object:
[cell.textLabel setTextColor:[UIColor greenColor]];
[cell.detailTextLabel setTextColor:[UIColor greenColor]];
If you are adding any label in subview like:
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100,25)];
lbl.text = #"your Text";
[lbl setTextColor:[UIColor greenColor]];
you can replace greenColor with your desire color.
Hope this helps :)

iphone : Label values over write.

I am inserting label in UIButton using addSubView
Here the label's value will be change every time.
UILabel *backTopLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)];
backTopLabel.backgroundColor = [UIColor clearColor];
backTopLabel.textColor = [UIColor whiteColor];
backTopLabel.font = [UIFont fontWithName:#"Georgia" size:56];
backTopLabel.textAlignment = UITextAlignmentCenter;
backTopLabel.text = [[selectedUsers objectAtIndex:userIndex] valueForKey:#"FirstName"]; // UserIndex will be change and new data will be load in the label
[btnBackLeftCard addSubview:backTopLabel]; // btnBackLeftCard is the UIButton
[backTopLabel release];
The Problem is : It successfully changes the label value but it is over writing it.
I am getting the new label value over the old label value.
How can I solve this ?
Every time you call [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)] you are creating new memory, not accessing the existing memory.
So, you need to have something like
if(!backTopLabel)
backTopLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)];
so that you only create the memory if it doesn't exist.
However since your label isn't a property, you're essentially losing access to it anyway.
So you either need to add the label as a property of the class you're in OR tag the view so you can find it again & remove it.
Tag it & find again like this:
for(UIView* labelView in btnBackLeftCard.subviews)
{
if(labelView.tag = 100)
[labelView removeFromSuperView];
}
UILabel *backTopLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)];
backTopLabel.backgroundColor = [UIColor clearColor];
backTopLabel.textColor = [UIColor whiteColor];
backTopLabel.font = [UIFont fontWithName:#"Georgia" size:56];
backTopLabel.textAlignment = UITextAlignmentCenter;
//here is where you tag the view, so you can find it again
backTopLabel.view.tag = 100;
backTopLabel.text = [[selectedUsers objectAtIndex:userIndex] valueForKey:#"FirstName"];
[btnBackLeftCard addSubview:backTopLabel]; // btnBackLeftCard is the UIButton
[backTopLabel release];
Assuming you have only one subview to be inserted in UIButton you can use
for(UILabel *lblViews in [btn subviews]) //Remove all subviews which are labels under button first if any
{
if(lblViews.tag == sometagno) //Compare tag number and remove only if that label found.
[lblViews removeFromSuperview];
}
backTopLabel.tag = sometagno; //Assign some tag number while adding.
[btnBackLeftCard addSubview:backTopLabel]; //Than Add your subview
This basically happens when a view gets added as subview on another view.. In your case it's been added multiple times.
Create one UILabel and then simple change it's text property.
In your case you're creating it multiple times and adding on top of the previous one.
Or if you cannot reuse previously created one, try removing it from the superview then add the latest one..
I think you are adding the subview (UILabel) again.
YOu should only change the text of UILabel
I am working in COCOS2D. If you have declared labelname globally then use
[labelName removeFromParentAndCleanup:YES]

display label with UITableViewCellAccessoryDisclosureIndicator

I hope display label as below
So I used the codes below to display label with UITableViewCellAccessoryDisclosureIndicator
cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero// cellframe //SimpleTableIdentifier1
reuseIdentifier:SimpleTableIdentifier1] autorelease];
cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;//
UILabel *labelCell =[ [[UILabel alloc]init] autorelease];
[labelCell setTag:11101];
labelCell.frame = CGRectMake(180.9f,15.0f,60.0f,23.0f) ;
[labelCell setBackgroundColor:[UIColor clearColor]];
labelCell.textAlignment=UITextAlignmentRight;
[labelCell setFont:[UIFont systemFontOfSize:13.0]];
[labelCell setTextColor:[UIColor blackColor]];
[cell.contentView addSubview:labelCell];
UITableViewCellAccessoryDisclosureIndicator will display correctly
but the labelCell does not display, except I tap the row of the UITasbleViewCell
Welcome any comment
change the frame property of your UILabel,
try with changing value for x and y.
labelCell.frame = CGRectMake(180.9f,15.0f,60.0f,23.0f) ;
You could also consider to implement the accessory using accessoryView.
Implement a Custom Accessory View For UITableView in iPhone
Somehow I think doing this,
[cell.contentView bringSubviewToFront:labelCell];
will help but since there are other subviews in the table cell you will have to deal with their layout as well. Rather I suggest you create a new custom UITableViewCell subclass.

How to insert more than one line of data in a single cell of UITableView?

Can I know how can we insert more than one line of data into a single cell of a UITableView,
Just Like, I want the output in a single cell to be like :
KPK
Developer
Infosystems
INDIA
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 80)];
myLabel.text = #"KPK";
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.textColor = [UIColor yellowColor];
[cell addSubview:myLabel];
UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 201, 200, 80)];
myLabel1.text = #"Developer";
myLabel1.textAlignment = UITextAlignmentCenter;
myLabel1.textColor = [UIColor yellowColor];
[cell addSubview:myLabel1];
Use \n as you are using in your string.
Set numberOfLines to 4 to allow for any number of lines.
label.numberOfLines = 4;
Update the label frame to match the size of the text using sizeWithFont:. If you don't do this your text will be vertically centered or cut off.
UILabel *label; // set frame to largest size you want
...
CGSize labelSize = [label.text sizeWithFont:label.font
constrainedToSize:label.frame.size
lineBreakMode:label.lineBreakMode];
label.frame = CGRectMake(
label.origin.x, label.orgin.y,
label.size.width, labelSize.height);
label.text=[NSString stringWithFormat:#"KPK\nDeveloper\nInfosystems\nIndia"];
Now you just need to add the label in your cell.
[cell addSubView:label];
you can place \n in text.Not sure if that works else you can make custom cell for multiple line records.
set tablerow height and Use label in your tableview
Create a custom cell, add 4 labels in it, to display all of your required info. Checkout the following tutorial to see how you can make custom cell.
http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/
Use a uilabel and add that one as the subview if your tableview cell.you can specify the number of lines of uilabel
label.numberOfLines = 4;
I think that the best way is creating a custom UITableViewCell implementation, or programmatically adding UILabels to the cell's contentView.
I.e.:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 30)];
label.text = #"MyText";
label.textColor = [UIColor blackColor];
[cell.contentView addSubview:label];
[label release]; // Never forget this!
When adding the additional rows you should update the first line definition to correctly offset the new rows. Eg. the second label should have something like this:
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 35, 200, 30)];
When you are done, you should implement the tableView:heightForRowAtIndexPath: method in the table's delegate, because you have to define the correct height for the new cell. In your example, for 4 labels you should implement something like this:
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath)indexPath {
if (is_one_of_my_modified_cells) {
return 35*4 + 5;
}
return 44; // default height
}
Try it and let me know.

nsarray in uilabel

I want to add uilabels according to mutable array at run time.
actually i have some labels texts in nsmutablearray.and i want to create label at run time and fill the text in label.plz suggest any example or provide guidance
If you have an array of NSStrings and you want to create a UILabel for each text you can do:
for(NSString *text in myArray){
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(...)];
[label setText:text];
[aView addSubview:label];
[label removeFromSuperview];
}