2009-02-22 15 views
8

में टेक्स्ट को सही संरेखित करें मैं UIPickerView में टेक्स्ट को सही तरीके से संरेखित कैसे कर सकता हूं? मैंने पंक्ति दृश्यों के लिए कस्टम UILabel एस बनाने की कोशिश की, लेकिन किसी कारण से, कुछ भी दिखाई नहीं दे रहा है, दाएं संरेखित या अन्यथा। यहाँ मैं क्या लिखा है:UIPickerView

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; 
    [label setText:[NSString stringWithFormat:@"row %d", row]]; 
    [label setTextAlignment:UITextAlignmentRight]; 
    return [label autorelease]; 
} 

मामले में किसी को भी सोच रहा है, मैं CGRectZero इस्तेमाल किया क्योंकि मैं इसे UICatalog उदाहरण में देखा था।

उत्तर

3

आप CGRectZero की वजह से कुछ भी नहीं देख रहे हैं। आपको अपने मामले में एक आकार निर्धारित करने की जरूरत है।

UICatalog में, अपने बारे में कैसे वे CustomView के लिए CGRectZero इस्तेमाल किया बात कर अगर ... अच्छी तरह से अगर आप CustomView.m को देखो आप वे वास्तव में CGRectZero की अनदेखी करने और initWithFrame में एक आकार करने के लिए एक फ्रेम सेट कर रहे हैं देखेंगे:

0

सुनिश्चित करें कि आप:

  • एक स्पष्ट फ्रेम के रूप में उच्च और व्यापक जरूरत के रूप में है कि प्रदान करना;
  • लेबल के प्रदर्शन अपारदर्शी (.opaque = YES या NO के माध्यम से) और पृष्ठभूमि रंग को उचित रूप से सेट करें (आप आमतौर पर क्रमशः NO और [UIColor clearColor] चाहते हैं)।
  • फ़ॉन्ट को स्पष्ट रूप से सेट करें।
8

मैंने पिकर में दो घटक और एक विशेष घटक में पंक्तियों का शीर्षक सेट करने के लिए दो सरणी ली हैं।

नीचे कोड पिकरडेटा को डिफ़ॉल्ट फ़ॉन्ट और पिकर के फ़ॉन्ट्स के साथ केंद्र में प्रदर्शित करेगा। यह पिकरडेटा के केंद्र संरेखण के साथ सटीक पिकरडेटा प्रदर्शन व्यवहार देगा।

यहाँ

,

NSArray *component1Array=[NSArray arrayWithObjects:@"0 lbs",@"1 lbs",@"2 lbs",@"3 lbs",@"4 lbs",@"5 lbs",nil]; 

NSArray *component2Array=[NSArray arrayWithObjects:@"0.00 oz",@"0.25 oz",@"0.50 oz",@"0.75 oz",@"1.00 oz",nil]; 

    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
     { 
      //I have taken two components thats why I have set frame of my "label" accordingly. you can set the frame of the label depends on number of components you have... 

      UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 145, 45)]; 

      //For right alignment of text,You can set the UITextAlignmentRight of the label. 
      //No need to set alignment to UITextAlignmentLeft because it is defaulted to picker data display behavior. 

      [label setTextAlignment:UITextAlignmentCenter]; 
      label.opaque=NO; 
      label.backgroundColor=[UIColor clearColor]; 
      label.textColor = [UIColor blackColor]; 
      UIFont *font = [UIFont boldSystemFontOfSize:20]; 
      label.font = font; 
      if(component == 0) 
      { 
       [label setText:[NSString stringWithFormat:@"%@",[component1Array objectAtIndex:row]]]; 
      } 
      else if(component == 1) 
      { 
       [label setText:[NSString stringWithFormat:@"%@", [component2Array objectAtIndex:row]]]; 
      } 
      return [label autorelease]; 
     } 

आप उल्लेख UIPickerView प्रतिनिधि विधि नीचे टिप्पणी अगर आप विधि से ऊपर का उपयोग कर रहे चाहिए ...

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 

ऊपर नमूना कोड के उत्पादन की तरह दिखाई देगा

नीचे

alt text

3

आईओएस 6 में, अब आप कर सकते हैं एक NSAttributedString वापस करें, जिसमें टेक्स्ट संरेखण विशेषताएँ हो सकती हैं। मैंने यहां एक और संबंधित प्रश्न पर एक छोटा स्निपेट पोस्ट किया: https://stackoverflow.com/a/14035356/928963

संबंधित मुद्दे