2013-09-22 12 views
7

UIPickerViewはピッカービュー項目のNSAttributedStringの使用をサポートしていないようです。誰でもこれを確認できますか? UIPickerView.hファイルにNS_AVAILABLE_IOS(6_0)が見つかりましたが、これは問題ですか?これの周りに道があるのですか、または私は幸運の外ですか?UIPickerView:NSAttributedStringはiOS 7では使用できません。

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0); // attributed title is favored if both methods are implemented 
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view; 
+1

帰属文字列を指定するとどうなりますか?どのようにサポートされていませんか? – Wain

+1

私の謝罪私はおそらくこれを言及していたはずです。それは完璧に動作しますが、pickerViewはiOS 7の指定されたフォントでは表示しません。システムのデフォルトを使用します。 iOS 6では、これは想定されるフォントで表示されます。 – Rob

+1

別の方法として属性付きテキストのラベルを返そうとしましたか? – Wain

答えて

13

この問題の唯一の解決策は、pickerView:viewForRow:forComponent:reusingView:を使用して、Appleは明らかにそうでない帰属文字列を使用して無効にしているので、属性付きテキストでUILabelを返すことは明らかです。

+1

この方法の唯一の問題は、ピッカービューのビューでは、選択したときにすばらしいズーム効果が得られないが、文字列は表示される点です。 – Indoor

+0

私は同意し、実際に自分でそれを実装する方法があるのか​​分かりません。これは、Appleがもう一度独自のコードを壊すという不幸な問題です。 – Rob

7

Robは正しいですか、バグかどうかは、iOS 7のUIPickerViewで属性付きテキストを取得する最も簡単な方法は、pickerView:viewForRow:forComponent:reusingView:メソッドをハックすることです。ここで

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
{ 
    // create attributed string 
    NSString *yourString = @"a string"; //can also use array[row] to get string 
    NSDictionary *attributeDict = @{NSForegroundColorAttributeName : [UIColor whiteColor]}; 
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:yourString attributes:attributeDict]; 

    // add the string to a label's attributedText property 
    UILabel *labelView = [[UILabel alloc] init]; 
    labelView.attributedText = attributedString; 

    // return the label 
    return labelView; 
} 

が...私がやったことだそれは、iOS 7に大きな見えますが、あなたは私の白いテキストを見ることができないので、iOSの6のデフォルトの背景は白です。私はiOSのバージョンを確認し、それぞれに基づいて異なる属性を実装することをお勧めします。

+1

https://github.com/erica/iOS-6-Advanced-Cookbook/tree/master/C01%20-%20Device/02%20-%20UIDevice-ハードウェア これはちょうどチェックするのに使う良いライブラリですデバイスに関するすべてについてUIDevice-Hardware.hを入手してください – Rob

4

pickerViewを使用する例を次に示します。viewForRow:forComponent:reusingView:リサイクルされたビューを尊重します。

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UILabel *)recycledLabel { 
    UILabel *label = recycledLabel; 
    if (!label) { // Make a new label if necessary. 
     label = [[UILabel alloc] init]; 
     label.backgroundColor = [UIColor clearColor]; 
     label.textAlignment = NSTextAlignmentCenter; 
    } 
    label.text = [self myPickerTitleForRow:row forComponent:component]; 
    return label; 
} 
関連する問題