Cgrectzero

Discussion in 'iOS Programming' started by daproject85, Oct 11, 2012.

  1. daproject85 macrumors member

    Joined:
    Apr 13, 2011
    #1
    Hi Forum,

    I am a bit confused about initializing views or UIVIEW object / subclasses. I have seen lots of examples and code snippets like below

    Code:
    - (UILabel *)makeActivityLabelWithText:(NSString *)labelText;
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        
        label.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
        label.textAlignment = UITextAlignmentLeft;
        label.textColor = [UIColor blackColor];
        label.backgroundColor = [UIColor clearColor];
        label.shadowColor = [UIColor whiteColor];
        label.shadowOffset = CGSizeMake(0.0, 1.0);
        label.text = labelText;
        
        return label;
    
    my question is about the below line <br>
    so CGRectZero makes a rectangle frame of the view object (in this case UILable) in the superview...basically a tiiiiinny tinnny ( LOL ) rectangle at the very top right corner of the superview....in all the subsequent lines where the text is massaged and stuff would this text even show? isn't the text label so tiny?? or does it resize itself automatically?
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    To confirm your belief, create a minor project to test it. That is what I did and below the the code from the ViewController.m file that I added from your listing. I altered it slightly. Try it out as is, then uncomment the block I have commented out.


    Code:
    - (void) viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear: animated];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        
        label.hidden = NO;
        label.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = [UIColor blackColor];
        label.backgroundColor = [UIColor greenColor];
        label.shadowColor = [UIColor whiteColor];
        label.shadowOffset = CGSizeMake(0.0, 1.0);
        label.text = @"Hello World\n\nI am GOD!";
            
    /*
        NSLog(@"frame: %f %f %f %f", label.frame.origin.x, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
        label.numberOfLines = 0;
        [label sizeToFit]; // call after setting numberOfLines.
    */    
        
    
        NSLog(@"frame: %f %f %f %f", label.frame.origin.x, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
    
        NSLog(@"%@", label.text);
    
        [self.view addSubview: label];
    }
    
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    xStep, here's an easier way to NSLog a frame:
    Code:
    NSLog(@"frame: %@", NSStringFromCGRect(label.frame));
     
  4. caffeineios macrumors newbie

    Joined:
    Oct 9, 2012
    #4
    Wow I never knew that, very useful, thanks! :)
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    There's also NSStringFromCGAffineTransform, NSStringFromCGPoint, NSStringFromCGSize, NSStringFromUIEdgeInsets, and NSStringFromUIOffset.
     
  6. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #6
    Oh man! I don't know why I never thought of that. Thanks for a great tip! :)
     
  7. Mark FX macrumors regular

    Mark FX

    Joined:
    Nov 18, 2011
    Location:
    West Sussex, UK
    #7
    Thanks Dejo, I never picked upon the NSStringFromCGRect method before.

    No more more accessing the different %f values of a CGRect.

    Great!

    Regards Mark
     

Share This Page