-5

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

所缺少的只是 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
4

1 回答 1

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。

于 2013-07-16T08:39:52.613 回答