2012-08-31 3 views
-3

iPad 응용 프로그램을 만들어야합니다.UITextfield 너비를 동적으로 설정하는 방법은 무엇입니까?

세 가지 질문이 있습니다 (textfields). 동적 너비 UITextField을 설정해야합니다. text웹 서비스에서옵니다. 나는

텍스트 크기가 작은 다음 UITextField 크기가 작이다 질문, 는 텍스트 내가 코드를 다음 한

, 크기가 큰 다음 UITextField 크기가 큰 인 질문을 할 때 .. 질문 텍스트로 UITextField 폭을 동일하게 설정해야 .....

txtQuestionOne.hidden=NO; 
      txtQuestionTwo.hidden=NO; 
      txtQuestionThree.hidden=NO; 

      imgQueone.hidden=NO; 
      imgQueTwo.hidden=NO; 
      imgQueThree.hidden=NO; 

      [txtQuestionOne setPlaceholder:[[appDelegate.questions objectAtIndex:0] objectForKey:@"question"]]; 

      appDelegate.FirstQues =[[appDelegate.questions objectAtIndex:0] objectForKey:@"question"]; 

      NSLog(@"current q1 %@", [[appDelegate.questions objectAtIndex:0] objectForKey:@"question"]); 


      if ([[[appDelegate.questions objectAtIndex:0] objectForKey:@"permission"] isEqualToString:@"1"]) { 

       ratingButton1.hidden=NO; 

       ratingLabel1.hidden=NO; 
      } 


      [txtQuestionTwo setPlaceholder:[[appDelegate.questions objectAtIndex:1] objectForKey:@"question"]]; 

      appDelegate.SecondQues =[[appDelegate.questions objectAtIndex:1] objectForKey:@"question"]; 

      NSLog(@"current q2 %@", [[appDelegate.questions objectAtIndex:1] objectForKey:@"question"]); 

      if ([[[appDelegate.questions objectAtIndex:1] objectForKey:@"permission"] isEqualToString:@"1"]) { 

       ratingButton2.hidden=NO; 

       ratingLabel2.hidden=NO; 
      } 


      [txtQuestionThree setPlaceholder:[[appDelegate.questions objectAtIndex:2] objectForKey:@"question"]]; 

      appDelegate.ThridQues =[[appDelegate.questions objectAtIndex:2] objectForKey:@"question"]; 

      NSLog(@"current q3 %@", [[appDelegate.questions objectAtIndex:2] objectForKey:@"question"]); 

      if ([[[appDelegate.questions objectAtIndex:2] objectForKey:@"permission"] isEqualToString:@"1"]) { 

       ratingButton3.hidden=NO; 

       ratingLabel3.hidden=NO; 
      } 

내가 무엇을 할 수 있는지 알고 싶지 않으십니까?

+0

왜'UILabel'을 대신 사용하지 않는? – Maulik

답변

2

지정된 글꼴 스타일의 NSString에 대한 크기를 가져 오는 데 사용합니다. 반환 된 CGSize를 사용하여 UI 요소의 크기를 레이블, 텍스트 필드로 설정할 수 있습니다.

(comments from apple documentation) 
// Single line, no wrapping. Truncation based on the UILineBreakMode. 
- (CGSize)sizeWithFont:(UIFont *)font; // Uses UILineBreakModeWordWrap 
- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode; 

// for multi lined 
// Wrapping to fit horizontal and vertical size. Text will be wrapped and truncated using the UILineBreakMode. If the height is less than a line of text, it may return 
// a vertical size that is bigger than the one passed in. 
// If you size your text using the constrainedToSize: methods below, you should draw the text using the drawInRect: methods using the same line break mode for consistency 

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size; // Uses UILineBreakModeWordWrap; 
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode; 

예 :

NSString *valueString = @"This is example"; 
CGSize newSize = [valueString sizeWithFont: [UIFont fontWithName: @"TrebuchetMS" size: 12] ]; 

// assign new size 
CGRect textFrame = textbox. frame; 
textFrame. size = newSize; 
+0

텍스트 필드 너비를 늘리고 글꼴 크기를 텍스트 필드로 줄 필요가 있습니다. 것이 가능하다? –

+0

예. 위의 UIFont 변수 .. 걸립니다 글꼴 이름과 크기 –

+0

어떻게 사용할 수 있습니까? 몇 가지 예를 들어 주시겠습니까? 나는 아이폰에 새롭다. –

1

당신이

CGRect frame= yourTextField.frame; 
frame.size.width=your_required_width; 
yourTextField.frame=frame; 
0

this

그것은 당신에게 당신의 텍스트의 크기에서 당신에게 CGSize 객체를 제공 sizeWithFont를 사용하는 방법을 보여줍니다에서보세요 시도했다. 그런 다음이 CGSize 객체의 데이터로 텍스트 필드 프레임을 편집하십시오.

관련 문제