-3

I have to make iPad application.

There are three questions textfields. I have to set width UITextField dynamically, text is coming from webservice. and I have to set UITextField width same as questions text..

when texts question size is small then UITextField size is small, when texts question size is large then UITextField size is large,

I have following code.....

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;
            }

Please help me i dont know what can i do?

Girish
  • 4,692
  • 4
  • 35
  • 55
Virja Rahul
  • 415
  • 2
  • 4
  • 12

3 Answers3

2

Use this to get size for a NSString of specified font style. And use the returned CGSize to set size of UI element can be label, textfield.

 (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;

Eg:

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

// assign new size
CGRect textFrame = textbox. frame;
textFrame. size  = newSize;
Ashwin Kumar
  • 622
  • 4
  • 7
1

did you try this

CGRect frame= yourTextField.frame;
frame.size.width=your_required_width;
yourTextField.frame=frame;
Neo
  • 2,807
  • 1
  • 16
  • 18
0

Have a look at this post

It shows you the use of sizeWithFont which gives you a CGSize object from the size of your text. Then edit your textfields frame with the data of this CGSize object.

Community
  • 1
  • 1
E. Lüders
  • 1,495
  • 1
  • 12
  • 27