2009-02-22 8 views
8

UIPickerView의 텍스트를 어떻게 오른쪽 정렬 할 수 있습니까? 행 뷰에 대해 사용자 정의 UILabel을 만들려고했지만 어떤 이유로 든 오른쪽 정렬 또는 다른 방법으로 아무것도 표시되지 않습니다. 여기에 내가 쓴 내용은 다음과 같습니다 나는 UICatalog 예에서 그것을 보았 기 때문에 사람이 궁금해 경우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을 사용했다.

답변

3

CGRectZero 때문에 아무 것도 보이지 않습니다. 케이스에 크기를 설정해야합니다.

UICatalog에서 CGRectZero를 어떻게 CustomView에 사용했는지에 대해 이야기한다면 CustomView.m을 보면 실제로 CGRectZero를 무시하고 initWithFrame의 크기로 프레임을 설정하는 것을 볼 수 있습니다.

0

확인 확실 :

  • 는 필요에 따라 다양한 높이이며 명시 적 프레임을 제공합니다;
  • 레이블의 표시 불투명도 (.opaque = YES 또는 NO)와 배경색을 적절하게 설정합니다 (대개 NO 및 [UIColor clearColor] 필요).
  • 글꼴을 명시 적으로 설정하십시오.
8

특정 구성 요소의 행 제목을 설정하기 위해 두 개의 구성 요소를 선택기와 두 개의 배열로 가져 왔습니다.

아래 코드는 선택기의 기본 글꼴과 글꼴 크기가 중앙에있는 선택기 데이터를 표시합니다. 그것은 pickerdata의 중앙 정렬을 사용하여 정확한 pickerdata 디스플레이 동작을 제공합니다. 여기

,

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]; 
     } 
위의 예제 코드의 출력은 모양을

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

당신이 방법 위에 사용하는 경우는 언급 UIPickerView의 대리자 메서드 아래에 의견을해야

...

아래 아이폰 OS 6에

alt text

3

, 당신이 지금 할 수있는 텍스트 정렬 속성을 포함 할 수있는 NSAttributedString을 반환합니다. 다른 관련 질문에 짧은 스 니펫을 올렸습니다. https://stackoverflow.com/a/14035356/928963

관련 문제