简体   繁体   中英

Is this the way to create a custom UIView?

I've subclassed UIView so I can add multiple view of the same type to my project. The view is basically a rectangle with a radius, a picture in the left side and a piece of text on the right. Now don't get me wrong the code bellow works and all but i'm always asking myself "is this the way, or am I getting it really wrong".

1) I'm mixing CALayers with UILabels, is this ok?

2) Is the setUpView the way its done.

3) If this is ok, whats my best bet for letting the imageLayer respond to touch events?

4) Could I just ask a side question as well. As a part time beginner with only one basic app in the store am I been a bit picky. What I mean is, should I just make it work and crack on! What do you think?

@synthesize dateLabel = _dateLabel;
@synthesize nameLabel = _nameLabel;
@synthesize photo = _photo;
@synthesize roundRect= _roundRect;
@synthesize imageLayer = _imageLayer;



-(void)setUpView
{

    //create the white rectangle
        self.roundRect.frame = CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height);
    self.roundRect.cornerRadius = 15.0;
    self.roundRect.backgroundColor = [UIColor clearColor].CGColor;
    self.roundRect.borderColor = [UIColor whiteColor].CGColor;
    self.roundRect.borderWidth = 2.0;
    self.roundRect.masksToBounds = YES;

   //create the photo 
   CGImageRef image = [self.photo CGImage];
    self.imageLayer.frame = CGRectMake(0.0, 0.0, self.roundRect.frame.size.width*0.48, self.roundRect.frame.size.height);
    self.imageLayer.borderColor = [UIColor whiteColor].CGColor;
    self.imageLayer.borderWidth = 2.0;
    self.imageLayer.masksToBounds = YES;
   [self.imageLayer setContents:(__bridge id)image];
   [self.imageLayer setContentsGravity:kCAGravityResizeAspectFill];
    [self.imageLayer setContentsRect:CGRectMake(0.0,0.0,1.1,1.0)];

    //create label
    self.nameLabel.frame = CGRectMake(self.imageLayer.frame.size.width+5, self.roundRect.frame.size.height*0.05, self.roundRect.frame.size.width*0.48,self.roundRect.frame.size.height*0.35);
    self.nameLabel.backgroundColor = [UIColor clearColor];
    self.nameLabel.textAlignment = UITextAlignmentCenter;
    self.nameLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    self.nameLabel.textColor = [UIColor whiteColor];
    self.nameLabel.adjustsFontSizeToFitWidth =YES;

    self.nameLabel.font = [UIFont fontWithName:@"Gill Sans" size:50.0];








    [self.roundRect addSublayer:self.imageLayer];
    [self.layer addSublayer:self.roundRect];
    [self addSubview:self.nameLabel];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{


}

-(void)setPhoto:(UIImage *)photo
{
    _photo = photo;

    [self setUpView];

}

-(void)setNameLabel:(UILabel *)nameLabel
{
    _nameLabel = nameLabel;

    [self setUpView];

}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.roundRect = [[CALayer alloc]init];
        self.imageLayer = [[CALayer alloc]init];
        self.nameLabel = [[UILabel alloc]init];


    [self setUpView];
    }
    return self;
}

You could make this easier by using the Interface Builder. I would create a XIB with a UIView set to the size I want. Then add a label (nameLabel) and configure it how you like. You can use QuartzCore layers cornerRadius to set your rounded corners. Then you can use your subclass in the XIB and set it as the class type for the UIView. Not the file owner but the view. Then you can link your label as an outlet and you won't need to create it manually. You can remove some of the code above and still have your functionality and looks. I've learned to let the Interface Builder do the work when you can.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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