在PickerView部分中改变字体大小和样式

时间:2013-07-16 08:01:50

标签: ios uipickerview

我的字体选择器现在工作正常,

所有缺少的是PickerView中的2个额外部分,用于字体大小和样式(粗体,斜体和下划线)。

我不知道如何改变这些价值观。

这是我的选择器代码:

#import "ViewController.h"

@interface ViewController ()
{

}

@end

@implementation ViewController

- (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];
        }
    }
}


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

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

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

    return fontNames.count;



}
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{

    return [fontNames objectAtIndex:row];

}



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

    UILabel *pickerLabel = (UILabel *)view;
    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;

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

@end

1 个答案:

答案 0 :(得分:1)

使用此循环获取字体:

NSMutableArray *fontNames = [NSMutableArray array];

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

如何呈现选择器取决于您的UI布局。您可以在UIPopoverController中使用UITableView(只需google它,你会发现很好的教程)或UIPickerView。