调整行间距
- (void)layoutSubviews {
[super layoutSubviews];
NSMutableAttributedString *attributedString=[[NSMutableAttributedString alloc] initWithString: self.sharedContext.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:6];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.sharedContext.text.length)];
self.sharedContext.attributedText = attributedString;
}
UILabel根据现实内容自适应大小
- (void)setupLabel {
//准备工作
UILabel *textLabel = [[UILabel alloc] init];
textLabel.font = [UIFont systemFontOfSize:16];
NSString *str = @"222222222222222222222222222222222222222222";
textLabel.text = str;
textLabel.backgroundColor = [UIColor redColor];
textLabel.numberOfLines = 0;//根据最大行数需求来设置
textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
CGSize maximumLabelSize = CGSizeMake(100, 9999);//labelsize的最大值
//关键语句
CGSize expectSize = [textLabel sizeThatFits:maximumLabelSize];
//别忘了把frame给回label,如果用xib加了约束的话可以只改一个约束的值
textLabel.frame = CGRectMake(20, 70, expectSize.width, expectSize.height);
[self.view addSubview:textLabel];
}