2013-10-22 2 views
3

이제 UILabel Text를 화면 중앙에 배치하는 방법을 가르쳐주는 많은 예제를 보았습니다.하지만 제가 요구하는 것은 아닙니다. UILabel의 텍스트가 화면의 왼쪽 경계와 오른쪽 경계 사이의 중간에 있는지 확인하는 방법에 대해 묻습니다. 그리고해야 할 일은 수직으로 조정하는 것입니다. 나는 다음이 : 지금은 폭 전체 라벨 (320)를 만든 다음 바로 텍스트의 중심을 시도했습니다UILabel TEXT를 가로로 가운데에 배치하려면 어떻게합니까?

self.aboutMee = [[UILabel alloc] initWithFrame:CGRectMake(0, 400, 120, self.view.bounds.size.width)]; 
      self.aboutMee.text = @"About Me"; 
      self.aboutMee.textAlignment = NSTextAlignmentCenter; 
      self.aboutMee.backgroundColor = [UIColor clearColor]; 
      self.aboutMee.font = [UIFont fontWithName:@"Helvetica" size:14]; 
      [self.aboutMee setTextColor:[UIColor colorWithRed:0.0/255.0 green:140.0/255.0 blue:199.0/255.0 alpha:1.0]]; 
      [self.aboutMee setFont:[UIFont fontWithName:@"HelveticaNeueLTStd-ThCn" size:35]]; 
      [self.scrollView addSubview:self.aboutMee]; 

,하지만 작동하지 않습니다. 어떤 아이디어?

+1

방금 ​​인터페이스 빌더를 구축 할 수 있습니까? NSLayoutContraints를 사용하여 뷰를 중앙에 고정 할 수 있습니다. 매우 쉽게 IB에서 할 수 있습니다. –

답변

10

당신은 제대로 프레임을 설정해야합니다 : CGRectMake (pos.x, pos.y, 폭, 높이)

self.aboutMee = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)]; 
     self.aboutMee.text = @"About Me"; 
     self.aboutMee.textAlignment = NSTextAlignmentCenter; 
     self.aboutMee.backgroundColor = [UIColor clearColor]; 
     self.aboutMee.font = [UIFont fontWithName:@"Helvetica" size:14]; 
     [self.aboutMee setTextColor:[UIColor colorWithRed:0.0/255.0 green:140.0/255.0 blue:199.0/255.0 alpha:1.0]]; 
     [self.aboutMee setFont:[UIFont fontWithName:@"HelveticaNeueLTStd-ThCn" size:35]]; 
     [self.scrollView addSubview:self.aboutMee]; 
+0

오 ~ 이런. 나는 내가 그것을 놓쳤다라고 생각할 수 없다! 고티, 고티! –

관련 문제