【问题标题】:EDIT: UITextView Label is Cut in Half (Horizontally)编辑: UITextView 标签被切成两半(水平)
【发布时间】:2012-04-17 03:02:23
【问题描述】:

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

截图:

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

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

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

【问题讨论】:

  • UIButton 没有 contentSize 属性,是吗?你得到contentSize 的对象是什么类型的?
  • choiceD 知道它的宽度是固定的吗?如果您设置dFrame.size = choiceD.contentSize,它是否会通过使按钮太宽而无法提供可用空间来使其适合其文本?也可以尝试使用NSLog 来显示前后的尺寸。
  • @warrenm 对不起,我的意思是 UITextView

标签: iphone objective-c ios uibutton contentsize


【解决方案1】:

计算字符串的大小:

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

【讨论】:

  • 今晚晚些时候我一定会尝试一下。 ty
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    我的建议是先计算你在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 中输入文本后编写上述代码。 希望它有所帮助。谢谢:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      • 2015-08-15
      • 1970-01-01
      • 2013-11-12
      相关资源
      最近更新 更多