这次主要介绍UILabel用于显示多行文本并设置行间距的显示效果,主要的类有NSMutableParagraphyStyle,NSMutableAttributeString。
源代码演示:
一、 创建一个UILabel的分类,提供类方法和实例方法的接口
1.类方法计算文本显示的大小
/**
* 计算文本占用的size
*
* @param text 文本
* @param lines 行数 lines = 0不限行数
* @param font 字体类型
* @param lineSpacing 行间距
* @param cSize 文本最大区域
*
* @return 文本占用的size
**/
+(CGSize) sizeWithText:(NSString *)text lines:(NSInteger) lines font:(UIFont *)font andLineSpace:(CGFloat) lineSpacing contrainedToSize:(CGSize) cSize;
2.实例方法,设置文本多行可控行间距
/**
* 计算文本占用的size
*
* @param text 文本
* @param lines 行数 lines = 0不限行数
* @param font 字体类型
* @param lineSpacing 行间距
* @param cSize 文本最大区域
*
* @return 文本占用的size
**/
- (CGSize)setText:(NSString *)text lines:(NSInteger)lines andLineSpacing:(CGFloat)lineSpacing constrainedToSize:(CGSize)cSize;
3.接口实现
@implementation UILabel(MultipleLines)
- (CGSize)setText:(NSString *)text lines:(NSInteger)lines andLineSpacing:(CGFloat)lineSpacing constrainedToSize:(CGSize)cSize;
{
self.numberOfLines = lines;
if(!text || text.length == 0){
return CGSizeZero;
}
CGSize textSize = [self.class sizeWithText:text lines:lines font:self.font andLineSpace:lineSpacing contrainedToSize:cSize];
if([self p_isSingleLine:textSize.height font:self.font]){
lineSpacing = 0.0f;
}
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = lineSpacing;
style.lineBreakMode = NSLineBreakByTruncatingTail;
NSMutableAttributedString * attr = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSParagraphStyleAttributeName:style,NSFontAttributeName:self.font}];
[self setAttributedText:attr];
return CGSizeMake(textSize.width, textSize.height);
}
+(CGSize) sizeWithText:(NSString *)text lines:(NSInteger) lines font:(UIFont *)font andLineSpace:(CGFloat) lineSpacing contrainedToSize:(CGSize) cSize
{
if(!text || text.length == 0){
return CGSizeZero;
}
CGFloat oneRowHeight = font.lineHeight;
CGSize textSize = [text boundingRectWithSize:cSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
CGFloat rows = textSize.height / oneRowHeight;
CGFloat realHeight = oneRowHeight;
if(lines == 0){
if(rows >= 1){
realHeight = (rows * oneRowHeight) + (rows - 1) * lineSpacing;
}
}else{
if(rows >= lines){
rows = lines;
}
realHeight = (rows * oneRowHeight) + (rows - 1) * lineSpacing;
}
return CGSizeMake(cSize.width, realHeight);
}
//单行判断
-(BOOL) p_isSingleLine:(CGFloat) height font:(UIFont *) font
{
BOOL isSingleLine = NO;
CGFloat oneRowHeight = self.font.lineHeight;
if(fabs(height - oneRowHeight) < 0.001f){
isSingleLine = YES;
}
return isSingleLine;
}
@end
4.运用实例
tableView代理方法
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.data.count;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
TestTableViewCell *cell = [TestTableViewCell createCell:tableView];
[cell setContent:[self.data objectAtIndex:indexPath.row]];
NSLog(@"%f",cell.cellHeight);
return cell.cellHeight;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TestTableViewCell *cell = [TestTableViewCell createCell:tableView];
[cell setContent:[self.data objectAtIndex:indexPath.row]];
return cell;
}
tableViewCell实现方法
@implementation TestTableViewCell
+(TestTableViewCell *) createCell:(UITableView *)tableView
{
static NSString *re = @"dfsdfsd";
TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:re];
if(cell == nil){
cell = [[TestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:re];
}
return cell;
}
-(instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self){
[self configView];
}
return self;
}
-(void) setContent:(NSString *)content
{
CGSize size= [self.contenLabel setText:content lines:3 andLineSpacing:5 constrainedToSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 24, MAXFLOAT)];
self.contenLabel.frame = CGRectMake(12, 12, [UIScreen mainScreen].bounds.size.width - 24, size.height);
self.cellHeight = size.height + 24;
}
-(void) configView
{
_contenLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 12, [UIScreen mainScreen].bounds.size.width - 24, 0)];
_contenLabel.layer.borderWidth = 1.0f;
_contenLabel.layer.borderColor = [UIColor redColor].CGColor;
_contenLabel.font = [UIFont systemFontOfSize:15];
[self.contentView addSubview:_contenLabel];
}
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
二、 demo地址