0
我製作了一個字體選擇器,可以顯示輪子內的所有字體。將字體大小和樣式的部分添加到pickerView
我現在需要添加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
我怎麼會增加大小和粗體/斜體我選擇器輪?我昨天才開始使用該控制器,所以我有點綠色 – Exothug
如果我只是使用字體家族名稱,然後改變大膽/斜體,那會工作嗎? – Exothug
並非所有字體都有粗體/斜體,所以您需要對字體名稱進行一些處理,以決定是否應該向用戶顯示選項... – Wain