【问题标题】:Adding sections for font size and style to a pickerView将字体大小和样式的部分添加到 pickerView
【发布时间】:2013-07-17 16:47:14
【问题描述】:

我制作了一个字体选择器,可以显示轮子内的所有字体。

我现在需要添加 2 个额外的部分来更改字体大小和样式(粗体、斜体、下划线)。

样式和大小是 UIFont 类的成员吗?如果是这样,它们是如何访问的?

这是我正在使用的代码:

.h 文件:

#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

更新的 .m 文件。这给了我一个选择大小的部分,但它也改变了字体:

#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

【问题讨论】:

    标签: ios uipickerview


    【解决方案1】:

    大小是独立的,不依赖于字体。

    粗体和斜体取决于字体,并且应该已经包含在您处理字体名称的过程中。

    下划线和删除线之类的东西是表示属性,因此不依赖于字体(显式绘制或通过属性字符串绘制)。


    添加字体大小:

    numberOfComponentsInPickerView 更改为return 2;

    pickerView:viewForRow:forComponent:reusingView: 更改为

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

    添加#define MINIMUM_ALLOWED_FONT_SIZE 13

    附言不要使用[NSString stringWithFormat:@"%@", 如果你已经有一个字符串(你有),那是没有意义的和浪费的。


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

    【讨论】:

    • 如何在我的选择轮上添加大小和粗体/斜体?我昨天才开始使用那个控制器,所以我有点绿
    • 如果我只使用字体系列名称,然后更改粗体/斜体,那可以吗?
    • 并非所有字体都有粗体/斜体,因此您需要对字体名称进行一些处理以确定是否应该向用户显示选项...
    • 能否添加一个示例,将字体大小部分添加到选取器视图中?
    • 这确实添加了一个额外的部分,但它是空的
    猜你喜欢
    • 1970-01-01
    • 2013-02-06
    • 2019-02-12
    • 1970-01-01
    • 2013-06-12
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    相关资源
    最近更新 更多