iOS富⽂本设置UILabel
Demo1
#//
// ViewController.m
// labeltext
//
// Created by bj on 2017/9/2.
// Copyright © 2017年 Doctor. All rights rerved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
#define UILABEL_LINE_SPACE 3
#define HEIGHT [ [ UIScreen mainScreen ] bounds ].size.height
//给UILabel设置⾏间距和字间距
-(void)tLabelSpace:(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:NSStringDrawingUsLineFragmentOrigin attributes:dic context:nil].size;
return size.height;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSString * str = @"长⼤的寂寞第⼀次进⼊校园,遇见的便是⼀颗樱花树,很多学⽣在嬉戏打闹,很想接近他们,可,还是,没胆量。进⼊班级后,认识了很多⼈,但还是不敢主动去聊天,说话。以前,
朋友之间的友谊很简单,⼀场游戏、⼀件玩具,就可能因此成为朋友。以前牵着⼿在⽠⽥⾥玩耍,累了,就直接躺在地上,和伙伴们说着悄悄话,看蚂蚁搬家,看蜻蜓点⽔……现在,有⾃尊⼼,总是害怕被拒绝,被孤⽴,被忽略。⼼⾥的事只能⾃⼰埋藏起来。班⾥会有很多的绯闻,搞得都不敢和异性接触,整天就是在忙学习,稍微有⼀点躲藏什么东西就会被宣传,都会被⼈好奇,每天和朋友也只是聊⼀会就继续学习。朋友很多,但很少是亲密,家长也总是不理解我们的⼼情,没事就是被批,和朋友的距离越来越远了……长⼤,好累,好寂寞。爱帮族美⽂⽹,欣赏更多美⽂";
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 300, 300)];
label.backgroundColor = [UIColor yellowColor];
label.numberOfLines = 0;
[lf tLabelSpace:label withValue:str withFont:[UIFont systemFontOfSize:17]];
CGRect frame = label.frame;
frame.size.height = [lf getSpaceLabelHeight:str withFont:[UIFont systemFontOfSize:17] withWidth:300];
label.frame = frame;
[lf.view addSubview:label];
// Do any additional tup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispo of any resources that can be recreated.
}
@end
// Do any additional tup after loading the view, typically from a nib.
}
//富⽂本设置(根据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
{
//展开后得⾼度(计算出⽂本内容的⾼度+固定控件的⾼度)
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:17]};
NSStringDrawingOptions option = (NSStringDrawingOptions)(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsLineFragmentOrigin | NSStringDrawingUsFontLeading);
NSString * timeText = @"90:90";
NSString * newContent = [[[TextListCell alloc]init] t];
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
{
//展开后得⾼度(计算出⽂本内容的⾼度+固定控件的⾼度)
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:17]};
NSStringDrawingOptions option = (NSStringDrawingOptions)(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsLineFragmentOrigin | NSStringDrawingUsFontLeading);
NSString * timeText = @"90:90";
NSString * newContent = [[[TextListCell alloc]init] t];
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) ;
}