首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >编辑: UITextView标签被切成两半(水平)

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

Stack Overflow用户
提问于 2012-04-17 11:02:24
回答 3查看 360关注 0票数 1

我需要帮助来解决这个特殊的问题。我有一个多项选择题应用程序,我有作为UITextview的选择。有时,选择D会因为某种原因被一分为二。

截图:

不知道这是怎么回事。基本上,我让UITextView框架调整为它的contentSize。

代码语言:javascript
运行
复制
                CGRect dFrame = choiceD.frame;
                dFrame.size.height = choiceD.contentSize.height;
                choiceD.frame = dFrame;

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

EN

回答 3

Stack Overflow用户

发布于 2012-04-17 11:10:50

计算字符串的大小:

代码语言:javascript
运行
复制
    NSString *choiceDString = @"Equal the present value....";
    CGSize size = [choiceDString sizeWithFont:[UIFont systemFontOfSize:CHOICE_FONT_SIZE] constrainedToSize:CGSizeMake(CHOICE_WIDTH, 100000)];

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

代码语言:javascript
运行
复制
    UILabel *choiceDLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,size.width,size.height)];
    choiceDLabel.text= choiceDString;

添加按钮的子视图标签:

代码语言:javascript
运行
复制
    [button addSubview:choiceLabel];
票数 0
EN

Stack Overflow用户

发布于 2012-04-17 11:16:55

使用此code..Yo可根据您的文本长度定义标签的高度...

代码语言:javascript
运行
复制
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];

希望这能帮助you...chill

票数 0
EN

Stack Overflow用户

发布于 2012-04-17 12:57:43

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

代码语言:javascript
运行
复制
      //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中输入文本后编写上述代码。希望是helps.Thanks :)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10184495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档