-4

我必须制作 iPad 应用程序。

有三个问题textfields。我必须UITextField动态设置宽度,text来自webservice。我必须设置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;
            }

请帮助我,我不知道我该怎么办?

4

3 回答 3

2

使用它来获取指定字体样式的 NSString 的大小。并使用返回的 CGSize 设置 UI 元素的大小,可以是 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;

例如:

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

// assign new size
CGRect textFrame = textbox. frame;
textFrame. size  = newSize;
于 2012-08-31T09:35:36.700 回答
1

你试过这个吗

CGRect frame= yourTextField.frame;
frame.size.width=your_required_width;
yourTextField.frame=frame;
于 2012-08-31T06:46:03.677 回答
0

看看这个帖子

它向您展示了使用sizeWithFont它为您提供了一个来自文本大小的 CGSize 对象。然后用这个 CGSize 对象的数据编辑你的文本框。

于 2012-08-31T06:58:42.473 回答