iOS富文本設置UILabel

Demo1

#//

//? ViewController.m

//? labeltext

//

//? Created by bj on 2017/9/2.

//? Copyright ? 2017年 Doctor. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

#define UILABEL_LINE_SPACE 3

#define HEIGHT [ [ UIScreen mainScreen ] bounds ].size.height

//給UILabel設置行間距和字間距

-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {

NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

paraStyle.alignment = NSTextAlignmentLeft;

paraStyle.lineSpacing = UILABEL_LINE_SPACE; //設置行間距

paraStyle.hyphenationFactor = 1.0;

paraStyle.firstLineHeadIndent = 0.0;

paraStyle.paragraphSpacingBefore = 0.0;

paraStyle.headIndent = 0;

paraStyle.tailIndent = 0;

//設置字間距 NSKernAttributeName:@1.5f

NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f

};

NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:str attributes:dic];

[attributeStr? addAttribute:NSForegroundColorAttributeName value:[UIColor? redColor] range:NSMakeRange(0, 4)];

label.attributedText = attributeStr;

}

//計算UILabel的高度(帶有行間距的情況)

-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width {

NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

paraStyle.alignment = NSTextAlignmentLeft;

paraStyle.lineSpacing = UILABEL_LINE_SPACE;

paraStyle.hyphenationFactor = 1.0;

paraStyle.firstLineHeadIndent = 0.0;

paraStyle.paragraphSpacingBefore = 0.0;

paraStyle.headIndent = 0;

paraStyle.tailIndent = 0;

NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f

};

CGSize size = [str boundingRectWithSize:CGSizeMake(width, HEIGHT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

return size.height;

}

- (void)viewDidLoad {

[super viewDidLoad];

NSString * str = @"長大的寂寞第一次進入校園,遇見的便是一顆櫻花樹,很多學生在嬉戲打鬧,很想接近他們,可,還是,沒膽量。進入班級后,認識了很多人,但還是不敢主動去聊天,說話。以前,朋友之間的友誼很簡單,一場游戲、一件玩具,就可能因此成為朋友。以前牽著手在瓜田里玩耍,累了,就直接躺在地上,和伙伴們說著悄悄話,看螞蟻搬家,看蜻蜓點水……現(xiàn)在,有自尊心,總是害怕被拒絕,被孤立,被忽略。心里的事只能自己埋藏起來。班里會有很多的緋聞,搞得都不敢和異性接觸,整天就是在忙學習,稍微有一點躲藏什么東西就會被宣傳,都會被人好奇,每天和朋友也只是聊一會就繼續(xù)學習。朋友很多,但很少是親密,家長也總是不理解我們的心情,沒事就是被批,和朋友的距離越來越遠了……長大,好累,好寂寞。愛幫族美文網(wǎng),欣賞更多美文";

UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 300, 300)];

label.backgroundColor = [UIColor? yellowColor];

label.numberOfLines = 0;

[self? setLabelSpace:label withValue:str withFont:[UIFont systemFontOfSize:17]];

CGRect? frame = label.frame;

frame.size.height = [self getSpaceLabelHeight:str withFont:[UIFont? systemFontOfSize:17] withWidth:300];

label.frame = frame;

[self.view addSubview:label];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

// Do any additional setup after loading the view, typically from a nib.

}

//富文本設置(根據(jù)NsRange? 改變顏色 和 大小)

-(NSMutableAttributedString? *)changeText:(NSString *)text timeFont:(UIFont *)timeFont withTimeColor:(UIColor *)timeColor? timeRange:(NSRange )timeRange? contentFont:(UIFont *)contentFont withContentColor:(UIColor *)contentColor? contentRange:(NSRange )contentRange{

NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

paraStyle.alignment = NSTextAlignmentLeft;

paraStyle.lineSpacing = UILABEL_LINE_SPACE; //設置行間距

paraStyle.hyphenationFactor = 1.0;

paraStyle.firstLineHeadIndent = 0.0;

paraStyle.paragraphSpacingBefore = 0.0;

paraStyle.headIndent = 0;

paraStyle.tailIndent = 0;

//設置字間距 NSKernAttributeName:@1.5f

NSDictionary *dic = @{NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f

};

NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:text];

[AttributedStr addAttribute:NSFontAttributeName

value:timeFont

range:timeRange];

[AttributedStr addAttribute:NSForegroundColorAttributeName

value:timeColor

range:timeRange];

[AttributedStr? addAttribute:NSFontAttributeName

value:contentFont

range:contentRange];

[AttributedStr? addAttribute:NSForegroundColorAttributeName

value:contentColor

range:contentRange];

[AttributedStr? addAttribute:NSHTMLTextDocumentType

value:contentColor

range:contentRange];

return AttributedStr;

}

+ (CGFloat)cellDefaultHeight:(TextEntity *)entity

{

//展開后得高度(計算出文本內(nèi)容的高度+固定控件的高度)

NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:17]};

NSStringDrawingOptions option = (NSStringDrawingOptions)(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading);

NSString * timeText = @"90:90";

NSString * newContent = [[[TextListCell? alloc]init] removeHTML:entity.content];

NSString * withTime = [NSString? stringWithFormat:@"%@ %@",timeText,newContent];

CGSize size = [withTime boundingRectWithSize:CGSizeMake(kWidth - 30, 80) options:option attributes:attribute context:nil].size;

return size.height + 60;

//默認cell高度

//return 120.0;

}

+ (CGFloat)cellMoreHeight:(TextEntity *)entity

{

//展開后得高度(計算出文本內(nèi)容的高度+固定控件的高度)

NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:17]};

NSStringDrawingOptions option = (NSStringDrawingOptions)(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading);

NSString * timeText = @"90:90";

NSString * newContent = [[[TextListCell? alloc]init] removeHTML:entity.content];

NSString * withTime = [NSString? stringWithFormat:@"%@ %@",timeText,newContent];

CGSize size = [withTime boundingRectWithSize:CGSizeMake(kWidth - 30, 100000) options:option attributes:attribute context:nil].size;

return (size.height + 60) ;

}

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容