Search code examples
iosuipickerview

Adding sections for font size and style to a pickerView


I have made a font selector that displays all fonts inside the wheel.

I now need to add 2 extra sections to it for altering the font size and style (bold, italic, underlined).

Are style and size members of the UIFont class? And if so, how are they accessed?

Here is the code I'm using :

.h file :

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    NSArray *fonts;


    NSMutableArray *fontNames;
    NSArray *fontSize;
    NSArray *fontStyle;

}


@property (strong, nonatomic,retain) IBOutlet UIPickerView *fontPicker;
@property (strong, nonatomic) IBOutlet UILabel *fontLabel;

@end

the updated .m file. This gives me a section to pick a size, but it changes the font as well :

#import "ViewController.h"

@interface ViewController ()
{

}

@end

@implementation ViewController

#define MINIMUM_ALLOWED_FONT_SIZE 13

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.



     fontNames = [NSMutableArray array];

    for(NSString *familyName in [UIFont familyNames]) {
        for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
            [fontNames addObject:fontName];
        }
    }

    _fontPicker.hidden = YES;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{

    if(component == 0)
    {
    return fontNames.count;
    }
    else if (component == 1){
        return MINIMUM_ALLOWED_FONT_SIZE + 17;
    }


    return -1;

}



- (UIView *)pickerView:(UIPickerView *)pickerView
            viewForRow:(NSInteger)row
          forComponent:(NSInteger)component
           reusingView:(UIView *)view {


  UILabel *pickerLabel = (UILabel *)view;
    if (component == 0) {

        CGRect frame = CGRectMake(0.0, 0.0, 80, 32);
        pickerLabel = [[UILabel alloc] initWithFrame:frame];
        pickerLabel.backgroundColor = [UIColor clearColor];
        pickerLabel.textAlignment = NSTextAlignmentLeft;
        pickerLabel.text = [NSString stringWithFormat:@"%@",[fontNames objectAtIndex:row]];
        pickerLabel.font = [UIFont fontWithName:[fontNames objectAtIndex:row] size:15];
        return pickerLabel;

    } else if (component == 1) {
        sizeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,80,32)];
         sizeLabel.backgroundColor = [UIColor clearColor];
        sizeLabel.textAlignment = NSTextAlignmentLeft;
        sizeLabel.text = [NSString stringWithFormat:@"%i", (row + MINIMUM_ALLOWED_FONT_SIZE)];
           return sizeLabel;
    }

    return nil;




}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component
{


    _fontLabel.text = [fontNames objectAtIndex:row];
    _fontLabel.font = [UIFont fontWithName:[fontNames objectAtIndex:row] size:MINIMUM_ALLOWED_FONT_SIZE];

    if(component == 1)
    {
        _fontLabel.font = [UIFont fontWithName:[fontNames objectAtIndex:row] size:row+MINIMUM_ALLOWED_FONT_SIZE];
    }




}

- (IBAction)operationPressed:(id)sender {

   _fontPicker.hidden = NO;



}


@end

Solution

  • Size is separate and not dependent upon the font.

    Bold and italic are dependent upon the font and should already be covered by your handling of the font names.

    Things like underline and strike through are presentation attributes so are not dependent upon the font (drawn explicitly or via attributed string).


    To add font size:

    Change numberOfComponentsInPickerView to return 2;

    Change pickerView:viewForRow:forComponent:reusingView: to

    if (component == 0) {
        // add the current code you have here
    } else {
        // create a new label here
        pickerLabel.text = [NSString stringWithFormat:@"%i", (row + MINIMUM_ALLOWED_FONT_SIZE)];
    }
    

    Add #define MINIMUM_ALLOWED_FONT_SIZE 13

    p.s. don't use [NSString stringWithFormat:@"%@", if you already have a string (you do), it's pointless and wasteful.


    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component
    {
        _fontLabel.text = [fontNames objectAtIndex:[pickerView selectedRowInComponent:0]];
        _fontLabel.font = [UIFont fontWithName:fontNames objectAtIndex:[pickerView selectedRowInComponent:0]]
                                      size:MINIMUM_ALLOWED_FONT_SIZE + [pickerView selectedRowInComponent:1]];
    }