label_设置行距、字距及计算含有行间距的label高度

//

//  ViewController.m

//  CNBlogs

//

//  Created by PXJ on 16/5/27.

//  Copyright © 2016 PXJ. All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    NSString * conText = @"\n你的嘴角 微微上翘 性感的无可救药\n想像不到 如此心跳 你的一切都想要\n软性的饮料 上升的气泡 我将对你的喜好 一瓶装全喝掉\n这里最不缺就是热闹 你煽情给拥抱\n烛火在燃烧 有某种情调 眼神失焦了几秒\n关于你的舞蹈 你慵懒的扭动著腰 受不了\n你随风飘扬的笑 \n有迷迭香的味道 语带薄荷味的撒娇 对我发出恋爱的讯号\n你优雅的像一只猫 动作轻逸的围绕 爱的甜味蔓延发酵 暧昧来的刚好\n\n软性的饮料 上升的气泡 我将对你的喜好 一瓶装全喝掉\n这里最不缺就是热闹 你煽情给拥抱\n烛火在燃烧 有某种情调 眼神失焦了几秒\n关于你的舞蹈 你慵懒的扭动著腰 受不了\n你随风飘扬的笑 有迷迭香的味道 语带薄荷味的撒娇 对我发出恋爱的讯号\n你优雅的像一只猫 动作轻逸的围绕 爱的甜味蔓延发酵 暧昧来的刚好\n";

    UIFont * font = [UIFont systemFontOfSize:12]; //字号

    CGFloat lineSpace = 8;//行间距

    CGFloat paragraphSpacing = 0;//段间距

    CGSize labSize = CGSizeMake(400, 1000);//label宽高

    NSNumber * textLengthSpace  = @0.2;//字间距

    

    

    NSDictionary * dic  = [self setTextLineSpaceWithString:conText withFont:font withLineSpace:lineSpace  withTextlengthSpace:textLengthSpace paragraphSpacing:paragraphSpacing];

    

    CGSize size = [conText boundingRectWithSize:labSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

    CGFloat sizeHeight = size.height;//lab的高度

 

    

//   CGFloat sizeHeight =  [self getSpaceLabelHeight:conText withFont:font withLineSpace:lineSpace size:labSize textlengthSpace:textLengthSpace paragraphSpacing:paragraphSpacing];

//    

    UILabel  * lab = [[UILabel alloc] init];

    lab.numberOfLines = 0;

    lab.backgroundColor = [UIColor yellowColor];

    lab.frameCGRectMake(20,20, 400, sizeHeight);

    lab.attributedText = [[NSAttributedString alloc] initWithString:conText attributes:dic];

    [self.view addSubview:lab];

}

 

/*

 *UILabel设置行间距和字间距

 */

-(NSDictionary *)setTextLineSpaceWithString:(NSString*)str withFont:(UIFont*)font withLineSpace:(CGFloat)lineSpace withTextlengthSpace:(NSNumber *)textlengthSpace paragraphSpacing:(CGFloat)paragraphSpacing{

    

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

    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

    paraStyle.alignment = NSTextAlignmentLeft;

    paraStyle.lineSpacing = lineSpace; //设置行间距

    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:textlengthSpace

                          };

    return dic;

}

 

/*

 *计算UILabel的高度(带有行间距的情况)

 */

 

 

//-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withLineSpace:(CGFloat)lineSpace size:(CGSize)textSize textlengthSpace:(NSNumber *)textlengthSpace paragraphSpacing:(CGFloat)paragraphSpacing

//{

//    

//    

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

//    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

//    paraStyle.alignment = NSTextAlignmentLeft;

//    paraStyle.lineSpacing = lineSpace;

//    paraStyle.paragraphSpacing = paragraphSpacing;

//    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:textlengthSpace

//                          };

//    CGSize size = [str boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

//    return size.height;

//}

//

@end

 

效果图

posted @ 2016-05-27 16:14  simplePXJ  阅读(1670)  评论(0编辑  收藏  举报