2013-02-03 6 views
0

보기의 맨 위에 레이블을 배치하여 중앙에 배치하는 데 문제가 있습니다. 그러나 날짜를 사용함에 따라 문자열의 너비가 다양합니다. 나는 또한 3.5 인치 스크린에서 텍스트가 맞지 않는다는 것을 알았다. 라벨의 양쪽에 두 개의 이미지 버튼이 있습니다.Obj-C, 뷰의 레이블 가운데 맞추기/

그래서 여백이 필요합니다. 문제를 더욱 복잡하게

는보기가 아니라 풍경에 표시, 그래서 왼쪽은 내가 할 필요가

ViewController *nextController = [[ViewController alloc] 
       initWithNibName:@"View" bundle:nil]; 
CGAffineTransform newTransform = CGAffineTransformMake(0.0,1.0,-1.0,0.0,0.0,0.0);   
nextController.view.transform = newTransform; 
nextController.hidesBottomBarWhenPushed = YES; 

나는 모든 생각 했 등 최고입니다했다 ...

int margin = 50; 
lblTitle.frame = CGRectMake(margin, 2, th - (margin * 2), 22); 

하지만 그건 작동하지 않습니다. 왼쪽에 상태 표시 줄의 높이를 추가하는 것과 거의 같지만 상태 표시 줄은 표시됩니다. 나는 수많은 시도를했는데, 내가 뭘 잘못하고 있는지 확신 할 수 없었다.

lblTitle = [[UILabel alloc] initWithFrame:CGRectZero]; 
lblTitle.text = [NSString stringWithFormat:@"%@ - from %@ to %@", 
         strChartType, displayStartDate, displayEndDate]; 

int th = self.view.frame.size.height; 
//int th = self.view.bounds.size.height; 

lblTitle.frame = CGRectMake(0, 2, th, 22); // x, y, w, h 

//[lblTitle setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | 
      UIViewAutoresizingFlexibleRightMargin]; 

//lblTitle.center = CGPointMake(self.view.bounds.size.width/2, 10); 

[lblTitle setTextAlignment:UITextAlignmentCenter]; 
lblTitle.font = [UIFont boldSystemFontOfSize:14]; 
lblTitle.adjustsFontSizeToFitWidth = FALSE; 

lblTitle.backgroundColor = [UIColor yellowColor]; 

[self.view addSubview:lblTitle]; 
+0

'CGRectGetMidX()'와'CGRectGetMidY()'는 도움이되어야합니다. –

+0

평가 대상은 무엇이며, 무엇이 좋을 것이라고 생각합니까? – Joel

답변

0

화면 경계를 얻은 다음 상태 막대의 높이를 빼서 레이블 너비를 얻을 수 있습니다.

float screenHeight = [UIScreen mainScreen].bounds.size.height; 
float statusBarHeight = ([UIApplication sharedApplication].statusBarFrame.size.height < [UIApplication sharedApplication].statusBarFrame.size.width) ? [UIApplication sharedApplication].statusBarFrame.size.height : [UIApplication sharedApplication].statusBarFrame.size.width; 
float labelWidth = screenHeight - statusBarHeight; 

참고 : 프레임의 너비 또는 높이가 올바른 값이 있는지 확인해야합니다 있도록 방향에 대해 걱정할 필요가 없습니다,하지만 statusBarFrame 회전 할 수 있도록 mainScreen 경계는 항상 세로 모드에서 반환 용도.

+0

안녕하세요 @ 조엘, 나는 너비에서 100을 벗었습니다. 그리고 이것은 위의 이미지와 같은 것을 만들어 냈습니다. 사실 내가 생산 한 것과 같은 결과입니다. – Jules

+0

100을 빼지 않으면 예상대로 위쪽에서 아래쪽으로 이동합니까? 다른 일이 벌어지고있다. 모든 프레임 크기 (보기, 레이블 등)를 기록하고 예기치 않은 결과를 생성하는 프레임을 확인해야합니다. – Joel

+0

네, 그게 아니야 100 – Jules