编辑:UITextView标签被切成两半(水平)

时间:2012-04-17 03:02:24

标签: iphone objective-c ios uibutton contentsize

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

截图: Choice D is halved!

不确定这里发生了什么。我基本上将UITextView框架调整为其contentSize。

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

有什么想法吗?提前谢谢。

3 个答案:

答案 0 :(得分: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];

答案 1 :(得分: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];

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

答案 2 :(得分: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中输入文本后写上面提到的代码。 希望它有所帮助。谢谢:)