Pixelated UILabel in table cell - ios

To add a UILabel to a table cell I use
UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, 10, 40, 12)];
timeLabel.text = #"2s";
timeLabel.backgroundColor = [UIColor clearColor];
timeLabel.font = [UIFont systemFontOfSize:12];
timeLabel.textColor = [UIColor lightGrayColor];
timeLabel.highlightedTextColor = [UIColor whiteColor];
timeLabel.textAlignment = UITextAlignmentRight;
timeLabel.frame = CGRectIntegral(timeLabel.frame);
[cell.contentView addSubview:timeLabel];
in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.
This works fine until I scroll the table or select a cell. Then the label becomes pixelated.
At load:
after action:
I also tried to add the label by subclassing UITableViewCell and load it in
- (void) layoutSubviews.
I already found related questions here and here but nothing worked.
EDIT: It's not possible to use the standard cell labels since they're already in use. I need to add an additional label.

I finally got it working with a dirty fix.
In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I set
cell.selectionStyle = UITableViewCellSelectionStyleNone;.
In a subclass of UITableViewCell I load the timeLabel in initWithStyle as follows:
timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, 10, 40, 12)];
timeLabel.text = #"2s";
timeLabel.backgroundColor = [UIColor whiteColor];
timeLabel.font = [UIFont systemFontOfSize:12];
timeLabel.textColor = [UIColor lightGrayColor];
timeLabel.highlightedTextColor = [UIColor whiteColor];
timeLabel.textAlignment = UITextAlignmentRight;
[self.contentView addSubview:timeLabel];
then I override these two functions:
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if(highlighted == YES){
UIImage *image = [UIImage imageNamed:#"cellBg#2x.png"];
//scale custom cell background to necessary height
UIImage *scaledImage = [image scaleToSize:CGSizeMake(1,self.contentView.frame.size.height)];
//set cell background
self.backgroundColor = [UIColor colorWithPatternImage:scaledImage];
//set textcolor for default labels
self.textLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.textColor = [UIColor whiteColor];
//set textcolor for custom label
timeLabel.textColor = [UIColor whiteColor];
//cope background for custom label background since timeLabel.backgroundColor = [UIColor clearColor] doesnt work
CGImageRef ref = CGImageCreateWithImageInRect(scaledImage.CGImage, CGRectMake(0, 10, 12, 20));
UIImage *img = [UIImage imageWithCGImage:ref];
//set custom label background
timeLabel.backgroundColor = [UIColor colorWithPatternImage:img];
} else {
//set unselected colors
self.backgroundColor = [UIColor whiteColor];
self.textLabel.textColor = [UIColor darkGrayColor];
self.detailTextLabel.textColor = UIColorFromRGB(0x808080);
timeLabel.textColor = UIColorFromRGB(0x808080);
//white background works without the label pixelates
timeLabel.backgroundColor = [UIColor whiteColor];
}
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if(selected == YES){
UIImage *image = [UIImage imageNamed:#"cellBg#2x.png"];
UIImage *scaledImage = [image scaleToSize:CGSizeMake(1,self.contentView.frame.size.height)];
self.backgroundColor = [UIColor colorWithPatternImage:scaledImage];
self.textLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.textColor = [UIColor whiteColor];
timeLabel.textColor = [UIColor whiteColor];
CGImageRef ref = CGImageCreateWithImageInRect(scaledImage.CGImage, CGRectMake(0, 10, 12, 20));
UIImage *img = [UIImage imageWithCGImage:ref];
timeLabel.backgroundColor = [UIColor colorWithPatternImage:img];
} else {
self.backgroundColor = [UIColor whiteColor];
self.textLabel.textColor = [UIColor darkGrayColor];
self.detailTextLabel.textColor = UIColorFromRGB(0x808080);
timeLabel.textColor = UIColorFromRGB(0x808080);
timeLabel.backgroundColor = [UIColor whiteColor];
}
}
hope this helps some people!

Related

How to draw UILabel with padding text not hidden?

I will drawing UILabel with code
CustomLabel *label = [[CustomLabel alloc] initWithFrame:CGRectMake(0, 0, 260, 100)];
label.layer.borderColor = [[UIColor redColor] CGColor];
label.layer.borderWidth = 3.0;
[label setFont:[UIFont fontWithName:#"Helvetica" size:16]];
CALayer * l1 = [label layer];
[l1 setMasksToBounds:YES];
[l1 setCornerRadius:10.0];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentLeft; // UITextAlignmentCenter, UITextAlignmentLeft
label.textColor=[UIColor whiteColor];
label.backgroundColor = [UIColor redColor];
label.numberOfLines=4;
label.lineBreakMode=NSLineBreakByWordWrapping;
label.text = [NSString stringWithFormat:#"%#", message];
label.tag = index;
[label sizeToFit];
And of course, i will found a solution from stackoverflow for create padding for label
with code
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.edgeInsets = UIEdgeInsetsMake(5,5,5,5); //customize padding here
}
return self;
}
- (void)drawTextInRect:(CGRect)rect {
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}
Everything working fine, but text in my label showing not good, here is my label screenshot.
I want like this.
Please help and thanks for your time.
you would want to have a look into this library.. its awesome and totally configurable...
https://github.com/AlexBarinov/UIBubbleTableView

Table view not appearing the same way in iOS7 as it did in iOS6

The relevant part of the code is in - tableView:cellForRowAtIndexPath::
cell.textLabel.backgroundColor = [UIColor clearColor];
if (indexPath.row == 0) {
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg",result.level]]];
} else {
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg",indexPath.row+OFFSETTOFIRSTROW]]];
}
cell.backgroundView.alpha = 0.5;
cell.backgroundColor = [UIColor darkGrayColor];
if (totalPieces) {
cell.textLabel.textColor = [UIColor colorWithRed:0.1 green:0.5 blue:0.1 alpha:1.0];
cell.textLabel.shadowColor = [UIColor blackColor];
if (indexPath.row == 0) {
cell.textLabel.shadowColor = [UIColor colorWithRed:0.0 green:0.5 blue:0.0 alpha:1.0];
cell.textLabel.textColor = [UIColor yellowColor];
cell.backgroundView.alpha = 1.0;
}
cell.textLabel.shadowOffset = CGSizeMake(1.0, 1.0);
} else {
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.shadowColor = nil;
}
Essentially, each row of the table has its own background image. This appears correctly in iOS6. But in iOS7, what happens is that the image is covered by what appears to be the background color. That is, until the user scrolls the table. When the user scrolls the table, it displays properly. What's making it behave differently upon first presentation? What should I do to fix it?
The intended result is a background color of dark grey covering the whole cell. Above that is the background image, which has some transparency, through which the background color should show. Above that is the text, which has a transparent background so the image/cell background shows through.
Unless I hear something else, I suppose what I'll do is to stack two images instead of using the cell background.
First clear the background view of cell by setting it to nil. Then set a custom view
[cell setBackgroundView: nil];
UIView* bview = [[UIView alloc] init];
bview.backgroundColor = [UIColor colorWithPatternImage:#"your image"];
[cell setBackgroundView:bview];
This worked for me.. Hope it will work for you too.. :-) Best of luck..
This is an intended change by Apple. To fix it change cell.backgroundColor to [UIColor clearColor].
OK. This solution works, although maybe there's a more efficient one.
cell.textLabel.backgroundColor = [UIColor clearColor];
UIView *backgroundView = [[UIView alloc] initWithFrame:cell.bounds];
backgroundView.backgroundColor = [UIColor darkGrayColor];
if (indexPath.row == 0) {
[backgroundView addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg",result.level]]]];
cell.backgroundView = backgroundView;
} else {
[backgroundView addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg",indexPath.row+OFFSETTOFIRSTROW]]]];
cell.backgroundView = backgroundView;
}

Why does sizeToFit remove my entire label?

I am trying to use the function sizeToFit. It works perfectly throughout the application however it does not work in the code below. Further to that, if it can't be done the way I am doing it please advise.
self.label = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 67.0, 290, 50)];
self.label.font= [UIFont fontWithName:#"Helvetica-Bold" size:20];
self.label.textColor = [UIColor colorWithRed:33.0f/255.0f green:74.0f/255.0f blue:146.0f/255.0f alpha:1.0f];
self.label.numberOfLines = 0;
self.label.lineBreakMode = NSLineBreakByWordWrapping;
self.label.backgroundColor = [UIColor whiteColor];
[self.contentView addSubview:self.label];
self.description = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 380.0, 300, 400)];
self.description.font= [UIFont fontWithName:#"Helvetica-Light" size:15];
self.description.textColor = [UIColor blackColor];
self.description.textAlignment = NSTextAlignmentJustified;
self.description.numberOfLines = 0;
self.description.lineBreakMode = NSLineBreakByWordWrapping;
self.description.backgroundColor = [UIColor whiteColor];
[self.description sizeToFit];
[self.contentView addSubview:self.description];
self.image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 130, 280, 228)];
self.image.clipsToBounds = YES;
//[scrollView addSubview:self.image];
[self.contentView addSubview:self.image];
self.backgroundColor = [UIColor whiteColor];
I passed the data using this method from a different controller
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
NSDictionary *item = [_articleListmain objectAtIndex:indexPath.item];
// set the article image
[cell.image setImageWithURL:[item objectForKey:#"image"]];
[cell.label setText:[item objectForKey:#"title"]];
[cell.description setText:[item objectForKey:#"description"]];
return cell;
}
Since the label is empty, sizeToFit shrinks it down to nothing. That's the point. Set some text then call sizeToFit and the label will be just big enough for the text.
I encountered the same problem. I have a customized cell and a separated tableview controller. it turns out that we should set the NumberOfLines and SizeToFit in the controller, not the cell.

UIView not visible in UITableView Header

Why won't my UIView (created below) show up in the UITableViewHeader that I try to add it to?
UIView *view = [[UIView alloc] initWithFrame: CGRectMake ( 20, 20, 400, 400)];
searchBar = (UISearchBar *)self.tableView.tableHeaderView;
[searchBar.subviews objectAtIndex:0] removeFromSuperview];
searchBar.backgroundColor = [UIColor clearColor];
[view addSubview:searchBar];
self.tableView.tableHeaderView = nil;
view.backgroundColor = [UIColor redColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, self.view.frame.size.width, 60)];
label.shadowColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:.35];
label.shadowOffset = CGSizeMake(0, -1.0);
label.text = #"188 People";
label.textColor = [UIColor grayColor];
label.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12.0];
label.backgroundColor = [UIColor clearColor];
[view addSubview:label];
self.tableView.tableHeaderView = view;
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame: CGRectMake ( 20, 20, 400, 400)];
searchBar = (UISearchBar *)self.tableView.tableHeaderView;
[searchBar.subviews objectAtIndex:0] removeFromSuperview];
searchBar.backgroundColor = [UIColor clearColor];
[view addSubview:searchBar];
view.backgroundColor = [UIColor redColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, self.view.frame.size.width, 60)];
label.shadowColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:.35];
label.shadowOffset = CGSizeMake(0, -1.0);
label.text = #"188 People";
label.textColor = [UIColor grayColor];
label.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12.0];
label.backgroundColor = [UIColor clearColor];
[view addSubview:label];
return view;//Please note the header will look the same for each section. You can add a switch or if/else to augment this
}
-As a side note, if you're planning on using the label here to set the text for the header, I suggest looking into the following methods:
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

iOS GCD use for a UITableView

I have a pretty intensive UITableView that needs to be optimized a little. The question is, how to use grand central station to do it effectively. Each cell has a UIView with a couple labels and two images. I have subclassed the TableViewCell and the view's are being reused though it is still a little laggy when the table gets bigger. How would I go about using GCD to optimize the table? OR is there a better way around it? I am not very strong in thread management and looking for some advice.
Here is the code to my tableview:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"texture3.png"]];
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.callTypeLabel.text = currentCall.currentCallType;
cell.locationLabel.text = currentCall.location;
cell.unitsLabel.text = currentCall.units;
cell.stationLabel.text = [#"Station: " stringByAppendingString:currentCall.station];
cell.selectedBackgroundView = cell.selectionView;
if ([currentCall.callType isEqualToString:#"F"]) {
cell.imageType = Fire;
}
else {
cell.imageType = EMS;
}
if ([currentCall.county isEqualToString:#"W"]) {
cell.imageType1 = Washington;
}
else {
cell.imageType1 = Clackamas;
}
return cell;
}
Here is the subclassed tableviewcell:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
callView = [[UIView alloc] initWithFrame:CGRectMake(7.5, 7, 305, 65)];
[callView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth];
[callView setContentMode:UIViewContentModeTopLeft];
[callView setBackgroundColor: [UIColor colorWithRed:240.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0]];
callView.layer.borderWidth = 1.0;
callView.layer.borderColor = [UIColor colorWithRed:(0/255.0) green:(0/255.0) blue:(0/255.0) alpha:1.0].CGColor;
[self.contentView addSubview:callView];
callTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 2, 190, 21)];
callTypeLabel.font = [UIFont boldSystemFontOfSize:12.0];
callTypeLabel.textColor = [UIColor blackColor];
callTypeLabel.backgroundColor = [UIColor clearColor];
callTypeLabel.highlightedTextColor = [UIColor whiteColor];
callTypeLabel.adjustsFontSizeToFitWidth = YES;
[callView addSubview:callTypeLabel];
locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 17 , 190, 15)];
locationLabel.font = [UIFont systemFontOfSize:10.0];
locationLabel.textColor = [UIColor blackColor];
locationLabel.backgroundColor = [UIColor clearColor];
locationLabel.highlightedTextColor = [UIColor whiteColor];
locationLabel.adjustsFontSizeToFitWidth = YES;
[callView addSubview:locationLabel];
unitsLabel = [[UILabel alloc]initWithFrame:CGRectMake(4, 43, 190, 21)];
unitsLabel.font = [UIFont systemFontOfSize:10.0];
unitsLabel.textColor = [UIColor blackColor];
unitsLabel.backgroundColor = [UIColor clearColor];
unitsLabel.highlightedTextColor = [UIColor whiteColor];
unitsLabel.adjustsFontSizeToFitWidth = NO;
[callView addSubview:unitsLabel];
stationLabel = [[UILabel alloc]initWithFrame:CGRectMake(195 , 25, 75, 20)];
stationLabel.font = [UIFont systemFontOfSize:12.0];
stationLabel.textColor = [UIColor blackColor];
stationLabel.backgroundColor = [UIColor clearColor];
stationLabel.highlightedTextColor = [UIColor whiteColor];
stationLabel.adjustsFontSizeToFitWidth = YES;
[callView addSubview:stationLabel];
CGRect countyImageFrame = CGRectMake(275, 10, 18, 18);
UIImageView *countyImageView = [[UIImageView alloc] initWithFrame:countyImageFrame];
countyImageView.image = countyImage;
[callView addSubview:countyImageView];
CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
UIImageView *callTypeImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
callTypeImageView.image = callTypeImage;
[callView addSubview:callTypeImageView];
selectionView = [[UIView alloc] initWithFrame:CGRectMake(10, 7, 200, 65)];
[selectionView setBackgroundColor: [UIColor clearColor]];
}
return self;
}
- (void)setImageType:(CallType)newImageType {
imageType = newImageType;
if (imageType == Fire) {
CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
UIImageView *callTypeImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
callTypeImageView.image = [UIImage imageNamed:#"red.png"];
[callView addSubview:callTypeImageView];
}
else if (imageType == EMS) {
CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
UIImageView *callTypeImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
callTypeImageView.image = [UIImage imageNamed:#"yellow.png"];
[callView addSubview:callTypeImageView];
}
}
- (void)setImageType1:(County)newImageType1 {
imageType1 = newImageType1;
if (imageType1 == Washington) {
CGRect callTypeImageFrame = CGRectMake(275, 10, 18, 18);
UIImageView *countyImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
countyImageView.image = [UIImage imageNamed:#"blue.png"];
[callView addSubview:countyImageView];
}
else if (imageType1 == Clackamas) {
CGRect callTypeImageFrame = CGRectMake(275, 10, 18, 18);
UIImageView *countyImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
countyImageView.image = [UIImage imageNamed:#"green.png"];
[callView addSubview:countyImageView];
}
}
This is a little subtle, but the main area your code will hang on is in the setImageType: method.
You're adding a programmatically created Image View into your view hierarchy here:
UIImageView *callTypeImageView = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
callTypeImageView.image = [UIImage imageNamed:#"red.png"];
[callView addSubview:callTypeImageView];
But you never actually remove the old image view. A better way to do this might be to cache the created image view in a property of the cell, then when you set the image type, send the message -[UIView removeFromSuperview] to the old image view before creating a new one.
As your code stands now, every time a cell is dequeued, a new image view is added to it, so every time the user scrolls up and down the table view, a new image view is created and added to the cell. It won't take long for there to be dozens of image views in each cell. I suspect this is causing many times more drawRect calls into image views than is actually necessary to achieve your purpose.
A better way of doing this would be to have both types of image views as properties that you create in the cell's init method, which are only configured in the setType methods. That way you only create one image view per type, and simply set configure its image in the appropriate setType method. Doing it this way, do bear in mind that removeFromSuperview will release the imageview, so you'll have to declare it as a strong property (assuming you're using ARC).
I appreciate neither of these solutions has anything to do with Grand Central Dispatch, but hopefully that should solve your problem without using a sledgehammer to crack a nut :).

Resources