Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-01-2011, 09:31 AM   #1 (permalink)
Awesome
 
Esko2300's Avatar
 
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Esko2300 is on a distinguished road
Default Quick Tip for not cutting off your textfields string

Heres a quick tip to size and textfield to fit the amount of characters in the string with in a tableview cell

Code:
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
float constraintSizeWidth = 262.2
	//First get the text you want to display into a string
	NSString *cellText = [NSString stringWithFormat:@"%@, myString]];
	
        //set the font to the font style and size you want to display
	UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
	
        //create a CGSize variable that holds the bounds of the textfield that you want the string to be restricted too
	CGSize constraintSize = CGSizeMake(constraintSizeWidth, MAXFLOAT);
        //create the size for the label
	CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
	
        //return the labelSize + padding
	return labelSize.height+20;	
}

and then you place these 3 lines of code in your cellForRowAtIndexPath function
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {	
	
	static NSString *CellIdentifier = @"Cell";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if(cell==nil){
		[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
		cell = self.mySelectedCell;
		self.mySelectedCell = nil;
	}
 	UILabel *label = (UILabel*)[cell.contentView viewWithTag:1];
        [label setFrame:have to set size of frame here];
	[label setText:[NSString stringWithFormat:@"%@,myString]];
                    
	
	//This sets the label to word wrap in the cell
	[label setLineBreakMode:UILineBreakModeWordWrap];
	[label setNumberOfLines:0];
	[label sizeToFit];
	
	return cell;
}

Last edited by Esko2300; 06-08-2011 at 11:38 AM. Reason: If you dont set the frame where i bolded the line of code for the frame. the text will not fit correctly
Esko2300 is offline   Reply With Quote
Old 06-01-2011, 10:09 AM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

This should be in the tutorial section.
baja_yu is offline   Reply With Quote
Reply

Bookmarks

Tags
uifont, uitableviewcell, uitextfield

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 333
4 members and 329 guests
Dnnake, iOS.Lover, jenniead38, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:17 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0