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 :)
Related
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
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.
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.
I am working on a todo list application. It's however i cant seems to align my checkbox image properly with the cell.textLabel.text.
My problem for the code below is the words "test test" got cover by my checkbox image, which only left with "st test"
My code for the checkbox button image like
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5.0, 7.0, 25.0, 25.0)];
UIImage *uncheckedButtonImage = [UIImage imageNamed:#"unchecked_checkbox.png"];
[button setImage:uncheckedButtonImage forState:UIControlStateNormal];
[cell.contentView addSubview:button];
and my cell textlabel is just
cell.textLabel.text = #"test test";
Is there anyway the align the textLabel text to move a bit to the right?
I have suggested to Add UILabel because in the future if you needed to change the view cell it will be more Dynamic and easier to play around anything new to the Cell.
take a look to the following code snippet:
CGRect DeviceNameFrame = CGRectMake(101, 32, 522, 21);
UILabel *lblDeviceName = [[UILabel alloc] initWithFrame:DeviceNameFrame];
lblDeviceName.backgroundColor = [UIColor clearColor];
lblDeviceName.font = [UIFont fontWithName:#"Helvetica-Bold" size:17];
lblDeviceName.text = #"test test";
lblDeviceName.lineBreakMode = UILineBreakModeMiddleTruncation;
[cell.contentView addSubview:lblDeviceName];
[lblDeviceName release];
I hope this will help you.
The simplest solution would be to look at the "Indentation" properties of UITableViewCell. See the documentation for more info. Subclassing the cell and adding your own UILabel would give you the most control though.
A quick question, for a quick answer (since I'm not finding any):
Is there a way to change the font of the section's title (given by titleForSection) in iPhone?
Thanks a lot!
Thanks Jasarien! You were absolutely right.
I leave my code here to help someone with the same problem:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = #"Just a title";
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(0, 0, 284, 23);
label.textColor = [UIColor blackColor];
label.font = [UIFont fontWithName:#"Helvetica" size:14];
label.text = sectionTitle;
label.backgroundColor = [UIColor clearColor];
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
[view autorelease];
[view addSubview:label];
return view;
}
You'll have to use the viewForHeaderInSection: method and provide your own view. Luckily this can be a UILabel with a specified font, so you can do it quite easily.
You have to override
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
to give required height for the section title. Else it will overlap with the cell.
viewForHeaderInSection might work fine... but here's an alternative that works fine for me (Xcode 5 and IOS7 target):
// To set the background color and text color of the Table Headers
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// Background color
view.tintColor = [UIColor colorWithRed:0.329 green:0.557 blue:0.827 alpha:1.000];
// Text Color & Alignment
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor whiteColor]];
[header.textLabel setTextAlignment:NSTextAlignmentCenter];
// Text Font
UIFont *saveFont = header.textLabel.font;
[header.textLabel setFont:[UIFont fontWithName:saveFont.fontName size:18.0]];
// Another way to set the background color
// Note: does not preserve gradient effect of original heade!r
// header.contentView.backgroundColor = [UIColor blackColor];
}
Here's what it looks like