這次主要介紹UILabel用于顯示多行文本并設置行間距的顯示效果,主要的類有NSMutableParagraphyStyle,NSMutableAttributeString。
源代碼演示:
一、 創(chuàng)建一個UILabel的分類带斑,提供類方法和實例方法的接口
1.類方法計算文本顯示的大小
/**
*?? 計算文本占用的size
*
*? @param text????????? ? ? ? 文本
*? @param lines???????? ? ? ? 行數(shù) lines = 0不限行數(shù)
* @param font?????????? ? ? ? 字體類型
* @param lineSpacing???? 行間距
* @param cSize?????????? ? ? 文本最大區(qū)域
*
* @return ? ? ? ? ? ? ? ? ? ? ? ?文本占用的size
**/
+(CGSize) sizeWithText:(NSString *)text lines:(NSInteger) lines font:(UIFont *)font andLineSpace:(CGFloat) lineSpacing contrainedToSize:(CGSize) cSize;
2.實例方法,設置文本多行可控行間距
/**
*?? 計算文本占用的size
*
* @param text????????? 文本
* @param lines???????? 行數(shù) lines = 0不限行數(shù)
* @param font?????????? 字體類型
* @param lineSpacing???? 行間距
* @param cSize?????????? 文本最大區(qū)域
*
* @return 文本占用的size
**/
- (CGSize)setText:(NSString *)text lines:(NSInteger)lines andLineSpacing:(CGFloat)lineSpacing constrainedToSize:(CGSize)cSize;
3.接口實現(xiàn)
@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實現(xiàn)方法
@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地址