UITableView with columns - iphone

I need to display a list items in a tableview but with about 5 different columns similar to a spreadsheet. The last column would be a UIButton (which I know is possible).
Is it possible to achieve this type of design with the UITableView?

I found an easier way of doing this by following one of Apple's samples:
- (UITableViewCell *)tableViewCellWithReuseIdentifier:(NSString *)identifier {
/*
Create an instance of UITableViewCell and add tagged subviews for the name, local time, and quarter image of the time zone.
*/
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
/*
Create labels for the text fields; set the highlight color so that when the cell is selected it changes appropriately.
*/
UILabel *label;
CGRect rect;
// Create a label for the time zone name.
rect = CGRectMake(LEFT_COLUMN_OFFSET, (ROW_HEIGHT - LABEL_HEIGHT) / 2.0, LEFT_COLUMN_WIDTH, LABEL_HEIGHT);
label = [[UILabel alloc] initWithFrame:rect];
label.tag = NAME_TAG;
label.font = [UIFont boldSystemFontOfSize:MAIN_FONT_SIZE];
label.adjustsFontSizeToFitWidth = YES;
[cell.contentView addSubview:label];
label.highlightedTextColor = [UIColor whiteColor];
[label release];
// Create a label for the time.
rect = CGRectMake(MIDDLE_COLUMN_OFFSET, (ROW_HEIGHT - LABEL_HEIGHT) / 2.0, MIDDLE_COLUMN_WIDTH, LABEL_HEIGHT);
label = [[UILabel alloc] initWithFrame:rect];
label.tag = TIME_TAG;
label.font = [UIFont systemFontOfSize:MAIN_FONT_SIZE];
label.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview:label];
label.highlightedTextColor = [UIColor whiteColor];
[label release];
// Create an image view for the quarter image.
rect = CGRectMake(RIGHT_COLUMN_OFFSET, (ROW_HEIGHT - IMAGE_SIDE) / 2.0, IMAGE_SIDE, IMAGE_SIDE);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect];
imageView.tag = IMAGE_TAG;
[cell.contentView addSubview:imageView];
[imageView release];
return cell;
}

A customized workaround, you can make your own table cells this way:
#interface MyOwnCell : UITableViewCell {
NSMutableArray *columns;
}
- (void)addColumn:(CGFloat)position;
#end
#implementation MyOwnCell
- (void)addColumn:(CGFloat)position {
[columns addObject:[NSNumber numberWithFloat:position]];
}
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0);
CGContextSetLineWidth(ctx, 0.25);
for (int i = 0; i < [columns count]; i++) {
CGFloat f = [((NSNumber*) [columns objectAtIndex:i]) floatValue];
CGContextMoveToPoint(ctx, f, 0);
}
[super drawRect:rect];
}
#end
Refer to TheLearner's solution which is actually more easier :)

Related

How to set UIlabel TextColor Dynamically

I have created more than one label using this code,
.H file
#interface ViewController : UIViewController
{
NSArray * phraseAry ;
UIView * containerView;
UILabel * oldLabel;
NSMutableArray *dataArray;
}
#property (strong, nonatomic) IBOutlet UIScrollView *myScrollView;
.M file
- (void)viewDidLoad
{
[super viewDidLoad];
heightValue = 20;
widthValue = 0;
xValue = 5;
yValue = 10;
containerView = [[UIView alloc] init];
for (int i=0; i<phraseAry.count; i++) {
widthValue = [self returnWidth:[phraseAry objectAtIndex:i]];
int newXValue = xValue+widthValue+5;
//NSLog(#"newXValue : %i",newXValue);
if (newXValue > 310) {
yValue +=20;
xValue = 5;
newXValue = xValue+widthValue+5;
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(xValue, yValue, widthValue, heightValue)];
lbl.text = [phraseAry objectAtIndex:i];
[lbl setFont:[UIFont fontWithName:#"Helvetica" size:14.0]];
lbl.tag = i;
lbl.textColor = [UIColor colorWithRed:(92/255.0) green:(109/255.0) blue:(43/255.0) alpha:1];
[containerView addSubview:lbl];
xValue = newXValue;
} else {
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(xValue, yValue, widthValue, heightValue)];
lbl.text = [phraseAry objectAtIndex:i];
[lbl setFont:[UIFont fontWithName:#"Helvetica" size:14.0]];
lbl.tag = i;
lbl.textColor = [UIColor colorWithRed:(92/255.0) green:(109/255.0) blue:(43/255.0) alpha:1];
[containerView addSubview:lbl];
xValue = newXValue;
}
}
containerView.frame = CGRectMake(0, 0, 320, yValue);
//add code to customize, e.g. polygonView.backgroundColor = [UIColor blackColor];
[self.myScrollView addSubview:containerView];
self.myScrollView.contentSize = containerView.frame.size;
}
And than i set background color and textcolor in specific table using this code
- (void)updateLabelMethod
{
oldLabel.backgroundColor = [UIColor clearColor];
UILabel *label = (UILabel *)[containerView viewWithTag:2];
oldLabel = label;
label.backgroundColor = [UIColor colorWithRed:(98/255.0) green:(147/255.0) blue:(216/255.0) alpha:1];
label.textColor = [UIColor whiteColor];
containerView.backgroundColor = [UIColor clearColor];
}
It will update background of label fine but when i use this code to update textcolor it will display error like this
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setTextColor:]: unrecognized selector sent to instance 0xbaa3b30'
Any ways to set textcolor? Plz help.
Start your label tag from 1 instead of 0, because by default the tag value is 0 for all view.
lbl.tag = i+1;
Because
UILabel *label = (UILabel *)[containerView viewWithTag:0];
it will return containerView itself, and UIView not have textColor property :P
[containerView viewWithTag:2];
check container view has only one view that is a label with tag 2.
May be some other view is set with tag 2 and may be causing the issue.
NSLog(#"%#",label);
can give you what the label here gives the output .It should point out an UILabel itself
make use of isKindOfClass: to identify it is a label you are updating
for (UILable *lblTemp in containerView.subviews){
if ([lblTemp isKindOfClass:[UILable class]] && lblTemp.tag==2){
// change the lbl color
break;
}
}
check using this code ;)

UITableViewCell frame.size.width is the same for ipad and iphone?

Trying to place a UILabel containing the date in the lower right hand corner of a cell whether it's on an iPhone or iPad.
Here's what I currently have, what do I need to do to get the actual cell size.width, tried doing cell.bounds.size.width as well, same value whether iPhone or iPad:
float xValue = cell.frame.size.width - 100;
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(xValue, 20.0f, 100, 17.0f)];
[nameLabel setTag:1];
[nameLabel setFont:[UIFont boldSystemFontOfSize:15.0]];
nameLabel.textColor = [UIColor grayColor];
nameLabel.text = #"05-12-13";
// custom views should be added as subviews of the cell's contentView:
[cell.contentView addSubview:nameLabel];
ETA, here is the whole custom cell class, maybe someone can see what's causing the duplication of the label:
#interface VideoTableViewCell : UITableViewCell
+ (VideoTableViewCell *)cellForTableView:(UITableView *)tableView;
+ (CGFloat)heightForVideo:(id<VideoProtocol>)video;
- (void)updateCellForVideo:(id<VideoProtocol>)video;
#end
#define MARGIN 10
#implementation VideoTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.textLabel.font = [UIFont boldSystemFontOfSize:14];
self.detailTextLabel.font = [UIFont systemFontOfSize:13];
self.textLabel.numberOfLines = 2;
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return self;
}
+ (VideoTableViewCell *)cellForTableView:(UITableView *)tableView {
NSString *identifier = #"TweetCell";
VideoTableViewCell *cell = (VideoTableViewCell *)[tableView
dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[VideoTableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:identifier];
}
return cell;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect cvf = self.contentView.frame;
self.imageView.frame = CGRectMake(0.0,
0.0,
cvf.size.height-1,
cvf.size.height-1);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
CGRect frame = CGRectMake(cvf.size.height + MARGIN,
self.textLabel.frame.origin.y,
cvf.size.width - cvf.size.height - 2*MARGIN,
self.textLabel.frame.size.height);
self.textLabel.frame = frame;
frame = CGRectMake(cvf.size.height + MARGIN,
self.detailTextLabel.frame.origin.y,
cvf.size.width - cvf.size.height - 2*MARGIN,
self.detailTextLabel.frame.size.height);
self.detailTextLabel.frame = frame;
}
- (void)setDateLabelWithDate:(NSDate *)date {
float xValue = self.frame.size.width - 70;
// create a custom label: x y width height
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(xValue, 40.0f, 100, 12.0f)];
dateLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
[dateLabel setFont:[UIFont boldSystemFontOfSize:13.0]];
dateLabel.textColor = [UIColor grayColor];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yy"];
NSString *dateString = [dateFormatter stringFromDate:date];
dateLabel.text = dateString;
[self.contentView addSubview:dateLabel];
}
+ (CGFloat)heightForVideo:(id<VideoProtocol>)video {
//create a dummy cell
VideoTableViewCell *sampleCell = [[VideoTableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:nil];
[sampleCell updateCellForVideo:video];
//calculate the sizes of the text labels
CGSize textSize = [video.title sizeWithFont: [VideoTableViewCell textLabelFont]
constrainedToSize:sampleCell.textLabel.frame.size
lineBreakMode:UILineBreakModeWordWrap];
CGFloat minHeight = 51 + 10; //image height + margin
return MAX(textSize.height + 20, minHeight);
}
+ (UIFont *)textLabelFont {
return [UIFont systemFontOfSize:13];
}
- (void)updateCellForVideo:(id<VideoProtocol>)video {
// set the text to the date with the tweet text
self.textLabel.text = video.title;
id<VideoAttrProtocol> speaker = [[video.speakers allObjects] objectAtIndex:0];
self.detailTextLabel.text = speaker.name;
[self setDateLabelWithDate:video.post_date];
NSURL *url = [NSURL URLWithString:video.thumbnail];
[self.imageView setImageWithURL:url
placeholderImage:[UIImage imageNamed:#"Logo.png"]];
}
#end
You need to set the label's autoresizingMask. Put the label in the correct place based on the cell's initial size, whatever it is. With the autoresizingMask set properly, the label will adjust as the cell's size changes.
Just before adding the label to the content view, add:
// Keep the label in the bottom-right corner
nameLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
Update based on all of the posted code:
In your custom cell, you create and add a label every time the setDataLabelWithDate: method is called.
You call this method from updateCellForVideo: and possibly from your view controller (you don't show that code).
You need to update your code so you do not add a new label to the cell every time setDataLabelWithDate: is called. Only add the label once.

How to remove duplicated items in iCarousel

https://github.com/nicklockwood/iCarousel/issues/278
I am going to use the iCarousel to show the images linearly.
These images will be gotten dynamically.
Assume that there are 2 images.
But when I update iCarousel, there are also 2 images in background as following image.
While debugging code, I found that viewForItemAtIndex function is called 2 times after I called reloadData function.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
So there are 4 images in iCarousel.
How to prevent to be called 2 times?
Please help me to fix this problem asap.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImageView *puzzlePhoto = nil;
UIImageView *puzzleBG = nil;
UILabel *label = nil;
if (view == nil)
{
view = [[UIImageView alloc] init];
view.frame = CGRectMake(0, 0, 128, 112);
view.backgroundColor = [UIColor clearColor];
view.layer.doubleSided = NO;
puzzlePhoto = [[UIImageView alloc] initWithFrame:CGRectMake(46, 48, 40, 40)];
//! get friend's photo
FForesight *foresightCurrent = (carousel == self.icGameOnwhoAREus) ? [controller.gameManager.m_arrayStartedForesights objectAtIndex:index] : [controller.gameManager.m_arrayEndedForesights objectAtIndex:index];
UIImage *imgPuzzle = [foresightCurrent getPuzzleImage];
if (imgPuzzle)
puzzlePhoto.image = imgPuzzle;
else
puzzlePhoto.image = [UIImage imageNamed:#"puzzle background_small.png"];
puzzleBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 128, 112)];
if (((foresightCurrent.m_intStage & FForesightStage_First) && !foresightCurrent.m_FCreatedByFriend) ||
((foresightCurrent.m_intStage & FForesightStage_Second) && foresightCurrent.m_FCreatedByFriend))
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg_me.png"];
else
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg.png"];
//! get friend name
FUser *userFriend = foresightCurrent.m_userFriend;
label = [[UILabel alloc] initWithFrame:CGRectMake(32, 32, 68, 16)];
label.text = userFriend.m_strName;
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:10];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:205.0f/256 green:55.0f/256 blue:2.0f/255 alpha:1];
[view addSubview:puzzlePhoto];
[view addSubview:puzzleBG];
[view addSubview:label];
}
return view;
}
The problem is almost certainly due to the views being recycled incorrectly. Views are re-used with different indexes, you you should never set index-specific properties in your view creation code. Here is the correct way to set up your item view:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImageView *puzzlePhoto = nil;
UIImageView *puzzleBG = nil;
UILabel *label = nil;
const NSInteger puzzlePhotoTag = 1;
const NSInteger puzzleBGTag = 2;
const NSInteger labelTag = 3;
if (view == nil)
{
//*************************************************
//do setup that is the same for every item view here
//*************************************************
view = [[UIImageView alloc] init];
view.frame = CGRectMake(0, 0, 128, 112);
view.backgroundColor = [UIColor clearColor];
view.layer.doubleSided = NO;
puzzlePhoto = [[UIImageView alloc] initWithFrame:CGRectMake(46, 48, 40, 40)];
puzzlePhoto.tag = puzzlePhotoTag;
[view addSubview:puzzlePhoto];
puzzleBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 128, 112)];
puzzleBG.tag = puzzleBGTag;
[view addSubview:puzzleBG];
label = [[UILabel alloc] initWithFrame:CGRectMake(32, 32, 68, 16)];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:10];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:205.0f/256 green:55.0f/256 blue:2.0f/255 alpha:1];
label.tag = labelTag;
[view addSubview:label];
}
else
{
//get references to subviews
puzzlePhoto = (UIImageView *)[view viewWithTag:puzzlePhotoTag];
puzzleBG = (UIImageView *)[view viewWithTag:puzzleBGTag];
label = (UILabel *)[view viewWithTag:labelTag];
}
//*************************************************
//do setup that is different depending on index here
//*************************************************
//! get friend's photo
FForesight *foresightCurrent = (carousel == self.icGameOnwhoAREus) ? [controller.gameManager.m_arrayStartedForesights objectAtIndex:index] : [controller.gameManager.m_arrayEndedForesights objectAtIndex:index];
UIImage *imgPuzzle = [foresightCurrent getPuzzleImage];
if (imgPuzzle)
puzzlePhoto.image = imgPuzzle;
else
puzzlePhoto.image = [UIImage imageNamed:#"puzzle background_small.png"];
if (((foresightCurrent.m_intStage & FForesightStage_First) && !foresightCurrent.m_FCreatedByFriend) || ((foresightCurrent.m_intStage & FForesightStage_Second) && foresightCurrent.m_FCreatedByFriend))
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg_me.png"];
else
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg.png"];
//! get friend name
FUser *userFriend = foresightCurrent.m_userFriend;
label.text = userFriend.m_strName;
return view;
}
As for the scrolling inertia problem, if you're still seeing that after applying this fix, file a separate bug report on the github page.
I solved this problem by performing reloadData on mainThread
[carousel performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:YES];

How to add padding-left on a UILabel created programmatically?

I know this is a noob question but ...I have these labels on a tableview, but the text is completely squished to the left. I want to add a bit of padding. How do I go about it?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(10,0,300,60)] autorelease];
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
headerLabel.backgroundColor = [UIColor colorWithHexString:[[_months objectAtIndex:section] objectForKey:#"color"]];
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.frame = CGRectMake(0,0,400,30);
headerLabel.text = [[_months objectAtIndex:section] objectForKey:#"name"];
headerLabel.textColor = [UIColor whiteColor];
[customView addSubview:headerLabel];
return customView;
}
any help is much appreciated! Thanks!
For a full list of available solutions, see this answer: UILabel text margin
The most flexible approach to add padding to UILabel is to subclass UILabel and add an edgeInsets property. You then set the desired insets and the label will be drawn accordingly.
OSLabel.h
#import <UIKit/UIKit.h>
#interface OSLabel : UILabel
#property (nonatomic, assign) UIEdgeInsets edgeInsets;
#end
OSLabel.m
#import "OSLabel.h"
#implementation OSLabel
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
}
return self;
}
- (void)drawTextInRect:(CGRect)rect {
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}
#end
you can simple add white space at the begin of you text;
[NSString stringWithFormat:#" %#",text];
It is 'evil' way to add 'padding', but it may help.
I found a better way to do this:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CGRect frame = CGRectMake(0, 0, 320, 25);
UIView *customView = [[UIView alloc] initWithFrame:frame];
UILabel *sectionTitle = [[UILabel alloc] init];
[customView addSubview:sectionTitle];
customView.backgroundColor = [UIColor redColor];
frame.origin.x = 10; //move the frame over..this adds the padding!
frame.size.width = self.view.bounds.size.width - frame.origin.x;
sectionTitle.frame = frame;
sectionTitle.text = #"text";
sectionTitle.font = [UIFont boldSystemFontOfSize:17];
sectionTitle.backgroundColor = [UIColor clearColor];
sectionTitle.textColor = [UIColor whiteColor];
[sectionTitle release];
tableView.allowsSelection = NO;
return [customView autorelease];
}
Set the backgroundColor on the customView also
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CGRect frame = tableView.bounds;
frame.size.height = HEADER_HEIGHT;
UIView* customView = [[[UIView alloc] initWithFrame:frame] autorelease];
customView.backgroundColor = [UIColor redColor];
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectInset(frame, LABEL_PADDING, 0)] autorelease];
// Orientation support
headerLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
headerLabel.backgroundColor = [UIColor redColor];
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.text = #"My Text Label";
headerLabel.textColor = [UIColor whiteColor];
[customView addSubview:headerLabel];
return customView;
}
Try not to hardcode magic numbers: (add these to top of file)
#define HEADER_HEIGHT 60.0f
#define LABEL_PADDING 10.0f
Should give this
Try the following & play around with the padding etc.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CGFloat headerHeight = 60, padding = 10;
UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,320,headerHeight)] autorelease];
customView.backgroundColor = [UIColor colorWithHexString:[[_months objectAtIndex:section] objectForKey:#"color"]];
CGRect frame = CGRectMake(padding,padding,320 - 2*padding,headerHeight-2*padding);
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.text = [[_months objectAtIndex:section] objectForKey:#"name"];
headerLabel.textColor = [UIColor whiteColor];
[customView addSubview:headerLabel];
return customView;
}
You can create a subclass of UILabel and override intrinsicContentSize and - (CGSize)sizeThatFits:(CGSize)size:
- (CGSize) intrinsicContentSize
{
CGSize parentSize = [super intrinsicContentSize];
parentSize.width += 2*PADDING_VALUE;
return parentSize;
}
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize parentSize = [super sizeThatFits:size];
parentSize.width += 2*PADDING_VALUE;
return parentSize;
}
True, it's a bit inexact and hackish, but you could always add a space in front of the month name like this:
headerLabel.text = [NSString stringWithFormat:#" %#",
[[_months objectAtIndex:section] objectForKey:#"name"]];
You could use a UITextView instead. I did this in Cocoa but I'm pretty sure it translates to UITextView:
NSTextView *headerLabel = [[[NSTextView alloc] initWithFrame:NSMakeRect(20.0, 20.0, 400.0, 20.0)] autorelease];
[headerLabel setBackgroundColor: [NSColor redColor]];
[headerLabel setString: #"Testing Stuff"];
[headerLabel setTextColor: [NSColor whiteColor]];
NSSize txtPadding;
txtPadding.width = 20.0;
txtPadding.height = 0.0;
[headerLabel setTextContainerInset:txtPadding];
[[mainWin contentView] addSubview:headerLabel];

increasing width of uilabel dynamically

I want to dynamically assign width to a label depending upon the text length to be displayed. The labels are it self being added on uiview. I am using following code but still i am getting label with shorter width.
- (id)initWithFrame:(CGRect)frame OrangeText:(NSString*)orange WhiteText:(NSString*)white {
if ((self = [super initWithFrame:frame])) {
CGSize textSize = [orange sizeWithFont:[UIFont systemFontOfSize:14]];
OrangeLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, textSize.width, textSize.height+2)];
OrangeLabel.text = orange;
OrangeLabel.backgroundColor = [UIColor clearColor];
OrangeLabel.textColor = [UIColor orangeColor];
[self addSubview:OrangeLabel];
WhiteLabel = [[UILabel alloc] init];
CGSize whiteTextSize = [white sizeWithFont:[UIFont systemFontOfSize:14]];
WhiteLabel.frame = CGRectMake(OrangeLabel.frame.size.width+35, 5, whiteTextSize.width, whiteTextSize.height);
WhiteLabel.text = white;
WhiteLabel.backgroundColor = [UIColor clearColor];
WhiteLabel.textColor = [UIColor whiteColor];
[self addSubview:WhiteLabel]; // Initialization code
}
return self;
}
I think you are looking for this method
[myLabel sizeToFit];
This should resize the label frame to fit its contents.