1

我需要帮助解决这个特殊的问题。我有一个多项选择题应用程序,我可以选择 UITextview。有时,无论出于何种原因,选项 D 都会被减半。

截屏: D选项减半!

不知道这里发生了什么。我基本上让 UITextView 框架调整到它的 contentSize。

                CGRect dFrame = choiceD.frame;
                dFrame.size.height = choiceD.contentSize.height;
                choiceD.frame = dFrame;

有任何想法吗?提前致谢。

4

3 回答 3

0

计算字符串的大小:

    NSString *choiceDString = @"Equal the present value....";
    CGSize size = [choiceDString sizeWithFont:[UIFont systemFontOfSize:CHOICE_FONT_SIZE] constrainedToSize:CGSizeMake(CHOICE_WIDTH, 100000)];

初始化一个标签以包含选择字符串:

    UILabel *choiceDLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,size.width,size.height)];
    choiceDLabel.text= choiceDString;

为按钮添加子视图标签:

    [button addSubview:choiceLabel];
于 2012-04-17T03:10:49.500 回答
0

使用此代码..您已根据您的文本长度定义标签的高度...

NSString *summary;
summary = @" your text";
CGSize sizeofbuttonorlable = [summary sizeWithFont:[UIFont systemFontOfSize:30] 
               constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40, MAXFLOAT)  
                   lineBreakMode:UILineBreakModeWordWrap];

CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, sizeofbuttonorlable.height);
UILabel *choiceDLabel = [[UILabel alloc] initWithFrame:frame];
choiceDLabel.text= summary;
[button addSubview:choiceLabel];

希望,这会帮助你......冷静

于 2012-04-17T03:16:55.373 回答
0

我的建议是首先计算您在 textView 中输入的文本的大小,例如:-

      //Give the maximum size of label which that label can have.
CGSize maximumLabelSize = CGSizeMake(300,500);
CGSize expectedLabelSize = [Label.text sizeWithFont:Label.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap];

      //adjust the label the new height.
CGRect newDescFrame = Label.frame;
newLabelFrame.size.height = expectedLabelSize.height;
NSLog(@"%f",newLabelFrame.size.height);
      //adjust the label the new width.
newLabelFrame.size.width = expectedLabelSize.width;
NSLog(@"%f",newLabelFrame.size.width);
      //Set the label size according to the new height and width.
label.frame = newLabelFrame;

在 textView 中输入文本后编写上述代码。希望它有所帮助。谢谢:)

于 2012-04-17T04:57:43.047 回答