我必须制作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;
}
请帮助我,我不知道我该怎么办?
答案 0 :(得分: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;
答案 1 :(得分:1)
CGRect frame= yourTextField.frame;
frame.size.width=your_required_width;
yourTextField.frame=frame;
答案 2 :(得分:0)
查看this帖子
它向您展示了使用sizeWithFont
,它根据文本的大小为您提供了CGSize对象。然后使用此CGSize对象的数据编辑textfields框架。