美文网首页
iOS富文本设置UILabel

iOS富文本设置UILabel

作者: 如果我们是朋友 | 来源:发表于2017-09-02 11:50 被阅读0次

    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 = @"长大的寂寞第一次进入校园,遇见的便是一颗樱花树,很多学生在嬉戏打闹,很想接近他们,可,还是,没胆量。进入班级后,认识了很多人,但还是不敢主动去聊天,说话。以前,朋友之间的友谊很简单,一场游戏、一件玩具,就可能因此成为朋友。以前牵着手在瓜田里玩耍,累了,就直接躺在地上,和伙伴们说着悄悄话,看蚂蚁搬家,看蜻蜓点水……现在,有自尊心,总是害怕被拒绝,被孤立,被忽略。心里的事只能自己埋藏起来。班里会有很多的绯闻,搞得都不敢和异性接触,整天就是在忙学习,稍微有一点躲藏什么东西就会被宣传,都会被人好奇,每天和朋友也只是聊一会就继续学习。朋友很多,但很少是亲密,家长也总是不理解我们的心情,没事就是被批,和朋友的距离越来越远了……长大,好累,好寂寞。爱帮族美文网,欣赏更多美文";

    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.

    }

    //富文本设置(根据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 | 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

    {

    //展开后得高度(计算出文本内容的高度+固定控件的高度)

    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) ;

    }

    相关文章

      网友评论

          本文标题:iOS富文本设置UILabel

          本文链接:https://www.haomeiwen.com/subject/gpoljxtx.html