为啥 UIView 的大小会通过添加标签来改变?

Posted

技术标签:

【中文标题】为啥 UIView 的大小会通过添加标签来改变?【英文标题】:Why UIView size is changed by adding label on it?为什么 UIView 的大小会通过添加标签来改变? 【发布时间】:2015-10-03 09:30:19 【问题描述】:

我有Manager UIViewController 课程。 Draw Chart UIViewManager ViewController 中的组件之一。它显示在附图中。 在Manager.m中,我设置了DrawChart UIView的大小。

- (IBAction)viewRecordButtonisPressed:(id)sender 
    //set UIView size 
    CGRect frame = self.drawChart.frame;
    frame.origin.x = 0;
    frame.origin.y = (int)((float)self.view.bounds.size.height/6.0);
    frame.size.width = self.view.bounds.size.width;
    frame.size.height = (int)((float)self.view.bounds.size.height*(2.0/3.0));
    self.drawChart.frame = frame;
    self.drawChart.hidden = NO;
    [self.drawChart setNeedsDisplay];


然后在 DrawChart.m 中,我添加了三个标签。然后改变了 Draw Chart 的 UIView 大小。如何在不改变 UIView 大小的情况下在 UIView 上书写?

- (void)drawRect:(CGRect)rect 

    if(![currentNameID isEqualToString:@""])
        [self extractInformation];
        UILabel *idLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
        [idLabel setTextColor:[UIColor whiteColor]];
        [idLabel setBackgroundColor:[UIColor clearColor]];
        [idLabel setFont:[UIFont fontWithName: @"Didot-Italic" size: 15.0f]];
        NSString* idtext = @"ID: ";
        idLabel.text = [idtext stringByAppendingString:currentNameID];
        [self addSubview:idLabel];

        UILabel *dobLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 20)];
        [dobLabel setTextColor:[UIColor whiteColor]];
        [dobLabel setBackgroundColor:[UIColor clearColor]];
        [dobLabel setFont:[UIFont fontWithName: @"Didot-Italic" size: 15.0f]];
        idtext = @"DOB: ";
        dobLabel.text = [idtext stringByAppendingString:dob];
        [self addSubview:dobLabel];

        UILabel *genderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 300, 20)];
        [genderLabel setTextColor:[UIColor whiteColor]];
        [genderLabel setBackgroundColor:[UIColor clearColor]];
        [genderLabel setFont:[UIFont fontWithName: @"Didot-Italic" size: 15.0f]];
        idtext = @"Gender: ";
        genderLabel.text = [idtext stringByAppendingString:gender];
        [self addSubview:genderLabel];
        

【问题讨论】:

【参考方案1】:

不确定是什么问题。我解决的方法是使用 CoreGraphic 来编写文本。它不会改变 UIView 的大小。 我替换为

- (void)drawRect:(CGRect)rect 

    if(![currentNameID isEqualToString:@""])
        [self extractInformation];
        NSDictionary *textAttributes = @NSFontAttributeName: [UIFont fontWithName: @"Didot-Italic" size: 15.0f], NSForegroundColorAttributeName:[UIColor whiteColor];

        // Create string drawing context
        NSStringDrawingContext *drawingContext = [[NSStringDrawingContext alloc] init];
        CGRect drawRect = CGRectMake(10, 10, 300, 20);
        NSString* idtext = @"ID: ";
        idtext = [idtext stringByAppendingString:currentNameID];
        [idtext drawWithRect:drawRect
                     options:NSStringDrawingUsesLineFragmentOrigin
                  attributes:textAttributes
                     context:drawingContext];

        CGRect drawRect1 = CGRectMake(10, 30, 300, 20);
        idtext = @"DOB: ";
        idtext = [idtext stringByAppendingString:dob];
        [idtext drawWithRect:drawRect1
                     options:NSStringDrawingUsesLineFragmentOrigin
                  attributes:textAttributes
                     context:drawingContext];

        CGRect drawRect2 = CGRectMake(10, 50, 300, 20);
        idtext = @"Gender: ";
        idtext = [idtext stringByAppendingString:gender];
        [idtext drawWithRect:drawRect2
                     options:NSStringDrawingUsesLineFragmentOrigin
                  attributes:textAttributes
                     context:drawingContext];

    

【讨论】:

以上是关于为啥 UIView 的大小会通过添加标签来改变?的主要内容,如果未能解决你的问题,请参考以下文章

为啥这个视图会神奇地改变大小?

UIView 根据边缘调整大小

iOS:如果我将 UILabel 添加到 UIView,视图是不是具有标签的固有内容大小?

为啥添加 <merge> 会改变 android 布局?

调整 UIView 的大小,但 drawRect 完成的绘图也会改变大小

Swift - 在标签中动态调整大小后动态调整视图大小