2015-04-19 54 views
1

是否可以在向下/向上滾動UIPickerView時更改文本的字體,該UIPickerView具有所有正確的系統字體名稱。以便文本將其字體更改爲pickerView中當前選定的字體。 ??通過滾動更改字體UIPickerView

+1

您是否希望UIPickerView'上的字體隨用戶滾動字體列表而改變?或者你是否希望字體本身顯示字體的每個名稱? –

回答

0

您可以設置一個方法,用於UIPickerView在滾動向上/向下,在viewdidload或UIPickerview存在的方法中更改選擇,如

[uorPickerView addTarget:self action:@selector(ValueChanged:) forControlEvents: UIControlEventValueChanged]; 

您可以設置已經從UIPickerview在選擇的ValueChanged方法類似

-(void)ValueChanged :(UIPickerView *) sender { 
     //Lets say you have UILable or UITextfield or UITextview as textView, so u can set your text like, 
     // fontArray = Array you have provided for UIPickerView datasource 
     [textView setFont:[UIFont fontWithName:[fontArray objectAtIndex:[sender selectedRowInComponent:0]] size:16]] 
     // make sure that fontArray has proper value of font family. 
} 

希望這有助於你的字體,

HTH,享受編碼!

+0

它有助於解決我的問題,謝謝。回覆晚了非常抱歉。 – Lumbrjck

+0

addTarget不適用於PickerView – user2526811

0

你可以得到所有的系統字體,並使用它作爲您的數據源

for(NSString *familyName in [UIFont familyNames]) { 
    for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) { 
    NSLog(@"%@", fontName); 
    } 
} 

實現您的委託,您可以爲每一行自定義視圖

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

    UILabel *pickerLabel = (UILabel *)view; 

    if (pickerLabel == nil) { 
    CGRect frame = CGRectMake(0.0, 0.0, 80, 32); 
    pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease]; 
    [pickerLabel setTextAlignment:UITextAlignmentLeft]; 
    [pickerLabel setBackgroundColor:[UIColor clearColor]]; 
    [pickerLabel setFont:/*font from datasource*/]; 
    } 

    [pickerLabel setText:[pickerDataArray objectAtIndex:row]]; 

    return pickerLabel; 

}