From 22c2fd1fc62e26c67b42415b97a68c69c1a36be5 Mon Sep 17 00:00:00 2001 From: Azen Xu Date: Fri, 31 Jul 2015 16:42:53 +0800 Subject: [PATCH] Label`s Lazy-Initial&Rewrite(setFrame:) 1. Use the lazy-Initial method to create the progressLabel 2. rewrite (setFrame:) method to make sure that the progressLabel also could be created , when I use the method [[DALabeledCircularProgressView alloc] init] , and set the frame later. --- .../DALabeledCircularProgressView.m | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/DACircularProgress/DALabeledCircularProgressView.m b/DACircularProgress/DALabeledCircularProgressView.m index c0e2bda..be407bf 100644 --- a/DACircularProgress/DALabeledCircularProgressView.m +++ b/DACircularProgress/DALabeledCircularProgressView.m @@ -10,6 +10,15 @@ @implementation DALabeledCircularProgressView +- (UILabel *)progressLabel +{ + if (_progressLabel == nil) { + _progressLabel = [[UILabel alloc] init]; + [self addSubview:_progressLabel]; + } + return _progressLabel; +} + - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; @@ -28,6 +37,11 @@ - (id)initWithCoder:(NSCoder *)aDecoder return self; } +- (void)setFrame:(CGRect)frame +{ + [super setFrame:frame]; + [self initializeLabel]; +} #pragma mark - Internal methods @@ -37,7 +51,7 @@ - (id)initWithCoder:(NSCoder *)aDecoder */ - (void)initializeLabel { - self.progressLabel = [[UILabel alloc] initWithFrame:self.bounds]; + self.progressLabel.frame = self.bounds; self.progressLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; self.progressLabel.textAlignment = NSTextAlignmentCenter; self.progressLabel.backgroundColor = [UIColor clearColor];