简体   繁体   English

如何增加主视图的子视图大小

[英]how to increase the size of subview of mainview

I have a editor where over UIView I put a label and there is labeltext over the label . 我有一个编辑器,在UIView放置了一个标签,在标签上有labeltext。 Now user can create as many UIView by writing text on UITextView and click on button . 现在,用户可以通过在UITextView上编写文本并单击button来创建尽可能多的UIView Now If I need to edit text , on Tapgesture I get the text and show it on the UITextView and user could edit that text . 现在,如果需要编辑文本,可以在Tapgesture上获取文本并将其显示在UITextView ,用户可以编辑该文本。

Now if a user add more text then I need to increase the width of the subview over which the text will fall after clicking on the submit button . 现在,如果用户添加更多文本,那么我需要增加子视图的宽度,单击提交按钮后,文本将落在该子视图上。 So my question is that is it possible to increase a subview width within a mainview . 所以我的问题是,有可能在主视图中增加子视图的宽度。 Here is the code through which I get all the subview from the main view 这是我从主视图获得所有子视图的代码

for (UIView *subview in mainview.subviews)
        {
            if(subview.tag==sender.tag)
            {

                for(UIView *getsusublabel in subview.subviews)
                {

                    if([getsusublabel isKindOfClass:[UILabel class]])
                    {
                        UILabel *subsublabel = (UILabel *)getsusublabel;
                        subsublabel.text=imagelabeltext.text;
                    }

                }
            }
        }

here subsublabel is the label on subview . 这里subsublabel是子视图上的标签。

If you want to increase label width according to the text, try this code snippet: 如果要根据文本增加标签宽度,请尝试以下代码片段:

//use this for custom font
CGFloat width =  [label.text sizeWithFont:[UIFont fontWithName:@"ChaparralPro-Bold" size:40 ]].width;

//use this for system font 
CGFloat width =  [label.text sizeWithFont:[UIFont systemFontOfSize:40 ]].width;

label.frame = CGRectMake(point.x, point.y, width,height);

//point.x, point.y -> origin for label;
//height -> your label height; 

If you want to change the size of the subview with the text, then you can try the folowing supposing that your subView is a label: 如果要使用文本更改子视图的大小,则可以尝试以下操作,假设您的subView是标签:

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font 
                    constrainedToSize:maximumLabelSize 
                    lineBreakMode:yourLabel.lineBreakMode];

And you can try it on other subViews accordingly. 您可以相应地在其他子视图上进行尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM