2013-07-16 101 views
-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 
+1

如何用所有260種識別字體填充視圖? – Desdenova

+0

這就是我想要做的,問題是如何 – Exothug

+0

你讀過這個嗎?什麼是問題? http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIPickerView_Class/index.html – Desdenova

回答

1

你得到使用這個循環的字體:

NSMutableArray *fontNames = [NSMutableArray array]; 

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

你如何展示撿拾工具取決於你的UI佈局。你可以在UIPopoverController中使用UITableView(只需谷歌它,你會發現很好的教程TONS)或UIPickerView。

+0

將您的循環添加到我的代碼中,嘗試使用數組作爲數據源。返回新的例外。看編輯的代碼。 – Exothug

+1

你絕對誤解了UIKit的工作原理。您不能將mutableArray設置爲pickerView的dataSource。 pickerView的類型必須是'(id )'。你可以在你的頭文件中採用UIPickerViewDelegate和-dataSource,實現它並將代理設置爲'self'。最好的方法是觀看關於YouTube上的UIPickerView或UITableView的教程(他們的工作類似)。 –

+0

你可以在你的答案中添加一個例子嗎? – Exothug