摘自https://github.com/CaiWanFeng/BeautifulView
- (void)setItemsArray:(NSArray *)itemsArray {
_itemsArray = itemsArray;
UIView *lastView = nil;
for (int i = 0; i < _itemsArray.count; i ++) {
CQPickTimeAlertItem *itemView = [[CQPickTimeAlertItem alloc] init];
[self addSubview:itemView];
if (!lastView) {
itemView.frame = CGRectMake(5, 40, self.frame.size.width-10, 0);
} else {
itemView.frame = CGRectMake(5, CGRectGetMaxY(lastView.frame) + 5, self.frame.size.width-10, 0);
}
itemView.model = _itemsArray[i];
lastView = itemView;
}
// 调整自身高度
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, CGRectGetMaxY(lastView.frame)+10);
// 虚线边框
CAShapeLayer *imaginaryLine = [CAShapeLayer layer];
imaginaryLine.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
imaginaryLine.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:0].CGPath;
imaginaryLine.lineWidth = 1. / [[UIScreen mainScreen] scale];
imaginaryLine.lineDashPattern = @[@2, @2];
imaginaryLine.fillColor = [UIColor clearColor].CGColor;
imaginaryLine.strokeColor = [UIColor grayColor].CGColor;
[self.layer addSublayer:imaginaryLine];
}
- (void)layoutSubviews {
[super layoutSubviews];
[self.titleLabel sizeToFit]; //自动拿labelframe
self.titleLabel.frame = CGRectMake(0, 0, self.titleLabel.frame.size.width + 6, self.titleLabel.frame.size.height + 2);
// 设置富文本:首行缩进
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
style.firstLineHeadIndent = self.titleLabel.frame.size.width + 4;
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.descLabel.text];
[attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, self.titleLabel.text.length)];
self.descLabel.attributedText = attrString;
self.descLabel.frame = CGRectMake(0, 0, self.frame.size.width, 30);
[self.descLabel sizeToFit];
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, CGRectGetMaxY(self.descLabel.frame));
}
suojin.png
网友评论