Changing font size in uipicker program

Discussion in 'iOS Programming' started by freshwreckage, Jun 7, 2010.

  1. freshwreckage macrumors newbie

    Joined:
    Jun 2, 2010
    #1
    Hello all...

    I am a noob at programming but I am making some headway so... thank you in advance for any help you may choose to give. I am building an app that displays a 3 column picker. The words in the array are long, so I am trying to resize the fonts. I found a method I am trying to implement, but when I insert it into my code, my wheels come up blank. I know that this is a noob issue. But have yet to find a solution.

    Here is my code...

    DoubleComponentPickerViewController.h
    Code:
    #import <UIKit/UIKit.h>
    #define kfirstComponent 0	
    #define kmiddleComponent 1
    #define klastComponent 2
    
    
    @interface DoubleComponentPickerViewController : UIViewController
    	<UIPickerViewDelegate, UIPickerViewDataSource>
    	{
    		IBOutlet UIPickerView *doublePicker;
    			NSArray *firstTypes;
    			NSArray *middleTypes;
    			NSArray *lastTypes;	
    	}
    
    @property (nonatomic,retain)  UIPickerView *doublePicker;
    @property (nonatomic,retain)  NSArray *firstTypes;
    @property (nonatomic,retain)  NSArray *middleTypes;
    @property (nonatomic,retain)  NSArray *lastTypes;
    
    -(IBAction)buttonPressed;
    
    
    
    @end
    
    DoubleComponentPickerViewController.m
    Code:
    #import "DoubleComponentPickerViewController.h"
    
    @implementation DoubleComponentPickerViewController
    
    @synthesize doublePicker;
    @synthesize firstTypes;
    @synthesize middleTypes;
    @synthesize lastTypes;
    
    -(IBAction)buttonPressed
    	{
    		NSInteger firstRow = [doublePicker selectedRowInComponent:kfirstComponent];
    		NSInteger middleRow = [doublePicker selectedRowInComponent:kmiddleComponent];
    		NSInteger lastRow = [doublePicker selectedRowInComponent:klastComponent];
    		NSString *first = [firstTypes objectAtIndex:firstRow];
    		NSString *middle = [middleTypes objectAtIndex:middleRow];
    		NSString *last = [lastTypes objectAtIndex:lastRow];
    		NSString *message = [[NSString alloc] initWithFormat:@"%@ %@ %@ Warning.",first, middle, last];
    		UIAlertView *alert = [[ UIAlertView alloc] initWithTitle:@"Description:"
    			message:message
    			delegate:nil
    			cancelButtonTitle:@"Cancel"
    			otherButtonTitles:nil];
    		[alert show];
    		[alert release];
    		[message release];
    	}
    
    
    - (void)viewDidLoad 
    	{
    		NSArray *firstArray = [[NSArray alloc] initWithObjects:
    			@"sample1a",@"sample2a",@"sample3a",nil];
    		self.firstTypes = firstArray;
    		[firstArray release];
    	
    		NSArray *middleArray = [[NSArray alloc] initWithObjects:
    			@"sample1b",@"sample2b",@"sample3b",nil];
    		self.middleTypes = middleArray;
    		[middleArray release];
    		
    		NSArray *lastArray = [[NSArray alloc] initWithObjects:
    			@"sample1c",@"sample2c",@"sample3c",nil];
    		self.lastTypes = lastArray;
    		[lastArray release];
    
    		
    		[super viewDidLoad];
    	}
    
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    	{
    		return 3;
    	}
    
    
    /* Ugly attempt at sizing font */
    
    
    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
    	{
    	UILabel *pickerLabel = (UILabel *)view;
    	// Reuse the label if possible, otherwise create and configure a new one
    	if ((pickerLabel == nil) || ([pickerLabel class] != [UILabel class])) 
    		{  //newlabel
    		CGRect frame = CGRectMake(0.0, 0.0, 270, 32.0);
    		pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
    		pickerLabel.textAlignment = UITextAlignmentLeft;
    		pickerLabel.backgroundColor = [UIColor clearColor];
    		pickerLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:8];
    		}			
    	pickerLabel.textColor = [UIColor blackColor];
    	return pickerLabel;	
    	}
    
    
    
     
     -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    	{
    		if (component == 0) 
    		{
    			return [self.firstTypes count];
    		}
    		
    		if (component == 1) 
    		{
    			return [self.middleTypes count];
    		}
    		
    		else 
    		{
    			return [self.lastTypes count];
    		}
    	}
    
     -(NSString *)pickerView:(UIPickerView *)pickerView
    	titleForRow:(NSInteger)row forComponent:(NSInteger)component 
    	{
    		if (component == 0) 
    		{
    			return [self.firstTypes objectAtIndex:row];
    		}
    		
    		if (component == 1) 
    		{
    			return [self.middleTypes objectAtIndex:row];
    		}
    		
    		else 
    		{
    			return [self.lastTypes objectAtIndex:row];
    		}
    		
    		/*if (component == kfirstComponent)
    			return [self.firstTypes objectAtIndex:row];
    			return [self.middleTypes objectAtIndex:row]; 
    		    return [self.lastTypes objectAtIndex:row];
    	*/
    	}
     
    
    - (void)didReceiveMemoryWarning 
    	{
    	    [super didReceiveMemoryWarning];
    	}
    
    - (void)viewDidUnload 
    	{
    	}
    
    
    - (void)dealloc 
    	{
        [super dealloc];
    	}
    
    @end
    
    
    

    Thank you again for your help...
    (please be kind to the noob... :)
     
  2. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    I think you need to set the text for the label that you return in viewForRow before you return it. The idea is that you either provide viewForRow or titleForRow for a given row.
     
  3. freshwreckage thread starter macrumors newbie

    Joined:
    Jun 2, 2010
    #3
    If I understand you correctly, I had tried that... Or I think I did...

    Is this what youmean?
    Code:
    
    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
    {
    	UILabel *pickerLabel = (UILabel *)view;
    	// Reuse the label if possible, otherwise create and configure a new one
    	if ((pickerLabel == nil) || ([pickerLabel class] != [UILabel class])) 
    	{  //newlabel
    		CGRect frame = CGRectMake(0.0, 0.0, 270, 32.0);
    		pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
    		pickerLabel.textAlignment = UITextAlignmentLeft;
    		pickerLabel.backgroundColor = [UIColor clearColor];
    		pickerLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:8];
    	}			
    	pickerLabel.textColor = [UIColor blackColor];
    	return pickerLabel;	
    }
    
    
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    	{
    		return 3;
    	}
    
    I have the method ahead of the -(NSInteger) line at one point but still only recieved blank wheels...
     
  4. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    No this:

    Code:
    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
    	{
    	UILabel *pickerLabel = (UILabel *)view;
    	// Reuse the label if possible, otherwise create and configure a new one
    	if ((pickerLabel == nil) || ([pickerLabel class] != [UILabel class])) 
    		{  //newlabel
    		CGRect frame = CGRectMake(0.0, 0.0, 270, 32.0);
    		pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
    		pickerLabel.textAlignment = UITextAlignmentLeft;
    		pickerLabel.backgroundColor = [UIColor clearColor];
    		pickerLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:8];
    		}			
    	pickerLabel.textColor = [UIColor blackColor];
            NSString*  text = nil;
    
    		if (component == 0) 
    		{
    			text = [self.firstTypes objectAtIndex:row];
    		}
                    else
    		if (component == 1) 
    		{
    			text =  [self.middleTypes objectAtIndex:row];
    		}
    		else 
    		{
    			text =  [self.lastTypes objectAtIndex:row];
    		}
    
    	pickerLabel.text = text;
    
    	return pickerLabel;	
    	}
     
  5. freshwreckage thread starter macrumors newbie

    Joined:
    Jun 2, 2010
    #5
    Tried and failed before with the code in this order.
    I get blank wheels...
    Not sure why though... it should work.
     
  6. meierjo macrumors newbie

    Joined:
    May 4, 2010
    #6
    it works - just change the CGRect frame width:

    CGRect frame = CGRectMake(0.0, 0.0, 270, 32.0);
    270 is pretty wide!

    Try something like 44
     

Share This Page