How to make UITableViewCell two labels of different colors?

I would like to have a UITableViewCell as follows:

enter image description here

The color of the text "Tel:" must be different from the color of the text of the number. Right now I am setting the text in the cell as usual:

cell.textLabel.text=@ "Something"; 

Is it possible to have 1 label and change the color of some of its parts?

How can I make a table cell like in my picture?

Thanks.

+6
source share
5 answers

You will need to take two labels in the cell ... and add this label to the UITableViewCell subtitle

Write this to the cellForRowAtIndexPath method.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; for(UIView *eachView in [cell subviews]) [eachView removeFromSuperview]; //Initialize Label UILabel *lbl1 = [[UILabel alloc]initWithFrame:YOURFRAME]; [lbl1 setFont:[UIFont fontWithName:@"FontName" size:12.0]]; [lbl1 setTextColor:[UIColor grayColor]]; lbl1.text = YOUR CELL TEXT; [cell addSubview:lbl1]; [lbl1 release]; UILabel *lbl2 = [[UILabel alloc]initWithFrame:YOURFRAME]; [lbl2 setFont:[UIFont fontWithName:@"FontName" size:12.0]]; [lbl2 setTextColor:[UIColor blackColor]]; lbl2.text = YOUR CELL TEXT; [cel2 addSubview:lbl2]; [lbl2 release]; return cell; } 

UPDATE:

Along with this, you can also create a custom cell as a definition here

Happy coding.

+8
source

many use the tagging technique as cell subzones, and you may encounter unexpected results when reusing cells. Apple already offers templates that can be customized in every aspect. In your case, without using custom cells and without adding labels, I would use the UITableViewCellStyleValue2 template, you can play with existing labels and an accessory, for example cell.textLabel , cell.detailTextLabel and cell.accessoryView , see This snippet that emulates more or less yours interface:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellID = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease]; } cell.textLabel.contentMode=UIViewContentModeScaleToFill; cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; cell.textLabel.baselineAdjustment=UIBaselineAdjustmentAlignCenters; cell.textLabel.textAlignment=UITextAlignmentCenter; cell.textLabel.font=[UIFont boldSystemFontOfSize:22]; cell.textLabel.textColor=[UIColor lightGrayColor]; cell.detailTextLabel.contentMode=UIViewContentModeScaleToFill; cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap; cell.detailTextLabel.baselineAdjustment=UIBaselineAdjustmentAlignCenters; cell.detailTextLabel.textAlignment=UITextAlignmentLeft; cell.detailTextLabel.font=[UIFont boldSystemFontOfSize:23]; cell.detailTextLabel.textColor=[UIColor blackColor]; cell.textLabel.text=@ "Tel.:"; cell.detailTextLabel.text=@ "+3912345678"; UIImageView *imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"phone.png"]]; cell.accessoryView = imageView; [imageView release]; return cell; } 

Hope this helps.

+5
source

the only way is to add new labels as a preview with different colors as the background.

+4
source

Go to the custom cell and initialize as required, or you can use UITableviewCell and add several UILabels to suit your desired formats.

For instance...,

 static NSString *MyIdentifier =@ "MyIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier]; } UILabel *lblPhone = [[UILabel alloc] initwithFrame:CGRectMake(5, 5, 150, 30)]; lblPhone.text = @"Tel."; [cell addSubView: lblPhone]; 
0
source

Cannot change font or color for parts of UILabel. The easiest way is to create a subclass of cells and add two shortcuts to either the interface constructor or the code. This post shows you how to calculate the size of text in a label so that you can dynamically change the label size if you want the text of the two labels to be a "hangout".

0
source

Source: https://habr.com/ru/post/899591/


All Articles