UILabel多行顯示可設置行間距

這次主要介紹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地址

UILabel設置行間距多行顯示demo

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末匈睁,一起剝皮案震驚了整個濱河市卷哩,隨后出現(xiàn)的幾起案子蛋辈,更是在濱河造成了極大的恐慌,老刑警劉巖将谊,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件冷溶,死亡現(xiàn)場離奇詭異,居然都是意外死亡尊浓,警方通過查閱死者的電腦和手機逞频,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來栋齿,“玉大人苗胀,你說我怎么就攤上這事⊥叨拢” “怎么了基协?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長菇用。 經常有香客問我澜驮,道長,這世上最難降的妖魔是什么刨疼? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任泉唁,我火速辦了婚禮,結果婚禮上揩慕,老公的妹妹穿的比我還像新娘亭畜。我一直安慰自己,他們只是感情好迎卤,可當我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布拴鸵。 她就那樣靜靜地躺著,像睡著了一般蜗搔。 火紅的嫁衣襯著肌膚如雪劲藐。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天樟凄,我揣著相機與錄音聘芜,去河邊找鬼。 笑死缝龄,一個胖子當著我的面吹牛汰现,可吹牛的內容都是我干的挂谍。 我是一名探鬼主播,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼瞎饲,長吁一口氣:“原來是場噩夢啊……” “哼口叙!你這毒婦竟也來了?” 一聲冷哼從身側響起嗅战,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤妄田,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后驮捍,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體疟呐,經...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年厌漂,在試婚紗的時候發(fā)現(xiàn)自己被綠了萨醒。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡苇倡,死狀恐怖富纸,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情旨椒,我是刑警寧澤晓褪,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站综慎,受9級特大地震影響涣仿,放射性物質發(fā)生泄漏。R本人自食惡果不足惜示惊,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一好港、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧米罚,春花似錦钧汹、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至隘竭,卻和暖如春塘秦,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背动看。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工尊剔, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人菱皆。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓须误,卻偏偏與公主長得像笔咽,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子霹期,可洞房花燭夜當晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內容