//

//  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.frame =  CGRectMake(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

效果图

label_设置行距、字距及计算含有行间距的label高度的更多相关文章

  1. CSS中字距,词距,首行缩进,字体大小,排版相关问题的探讨

    先说明下,这是在谷歌浏览器下字体显示等问题做个研究,火狐下有点差异,不过火狐占有率低,而且显示的没有谷歌那么合理,不管它先.IE卡的要死,半死不活,也懒得深入研究这些细节,字体排版上不是强迫症,差别也 ...

  2. 可以简易设置文字内边距的EdgeInsetsLabel

    可以简易设置文字内边距的EdgeInsetsLabel 最终效果: 源码: EdgeInsetsLabel.h 与 EdgeInsetsLabel.m // // EdgeInsetsLabel.h ...

  3. 一份新的lilypond谱子,能设置页边距和设置换页符了

    给学生做的一份乐谱,这回能设置页边距了,以及设置换页符了. 顺带能设置一些代码片段(snippet),可以用热键代替使用 设置页边距的snippet: \paper { %双引号里面填页面大小 #(s ...

  4. word设置行距18磅

    参考:word如何设置行距18磅 word设置行距18磅 选中需要设置的段落--"格式"菜单--段落--"缩进和间距"标签--在"行距"下拉 ...

  5. 动态计算Label高度

    //1.设置该label的numberOfLines为0 self.titleLabel.numberOfLines = 0;    //2.字体的设置要与之前相同 NSDictionary * at ...

  6. iOS7中计算UILabel中字符串的高度

    iOS7中计算UILabel中字符串的高度 iOS7中出现了新的方法计算UILabel中根据给定的Font以及str计算UILabel的frameSize的方法.本人提供category如下: UIL ...

  7. js计算元素距离顶部的高度及元素是否在可视区判断

    前言: 在业务当中,我们经常要计算元素的大小和元素在页面的位置信息.比如说,在一个滚动区域内,我要知道元素A是在可视区内,还是在隐藏内容区(滚动到外边看不到了).有时还要进一步知道,元素是全部都显示在 ...

  8. 精确计算微信小程序scrollview高度,全机型适配

    众所周知,可以滑动的 scroll 组件在移动端非常的重要,几乎每个页面都要用到. 而小程序的 scroll-view 组件就比较坑了,非得指定一个高度才能正常使用.布局复杂的时候谁还给你算高度啊.. ...

  9. 根据文字动态计算Label高度或宽度

    //根据已知的label宽度计算文字高度 CGRect rect = [reson boundingRectWithSize:CGSizeMake(label_W, 0) options:NSStri ...

随机推荐

  1. myeclipse 添加服务器运行时环境

    像servlet-api.jar.servlet-api.jar服务器能提供的包 解决方法如下: 1,File->New->Other->Server->Server(注意在n ...

  2. 常见排序算法(JS版)

    常见排序算法(JS版)包括: 内置排序,冒泡排序,选择排序,插入排序,希尔排序,快速排序(递归 & 堆栈),归并排序,堆排序,以及分析每种排序算法的执行时间. index.html <! ...

  3. VC下Debug和Release区别

    整理日: 2015年3月23日 最近写代码过程中,发现 Debug 下运行正常,Release 下就会出现问题,百思不得其解,而Release 下又无法进行调试,于是只能采用printf方式逐步定位到 ...

  4. tyvj 1934 高精度

    「Poetize3」Heaven Cow与God Bull From wwwwodddd     背景 Background __int64 ago,there's a heaven cow call ...

  5. Hibernate 注解多对一 要求在多那边产生一个外键而不会另外产生一个表

    在使用hibernate注解的时候,我们映射一对多时,有时候莫名其妙的产生了两张表,其中一张表是A_B,这并不符合数据库中多的一方放置一个外键的原则,那么如何控制只产生一个表呢,请看下面的例子: 多的 ...

  6. 查看SQLServer各种缓存的情况

    查看页面缓存: SELECT * FROM sys.dm_os_buffer_descriptors 清除页面缓存: CHECKPOINTDBCC DROPCLEANBUFFERS 查看执行计划缓存: ...

  7. ACM2096_小明A+B

    #include<iostream> int main() { using namespace std; int a,b,count; cin>>count; while(co ...

  8. zabbix web场景模拟监控配置

    一,zabbix2.2.3 VMware Vsphere exsi监控配置步骤, 1,添加监控主机 2,添加聚集macro;{$PASSWORD} = yoodo.com{$URL} = http:/ ...

  9. list,set,map,数组间的相互转换

    1.list转set Set set =  new  HashSet( new  ArrayList()); 2.set转list List list =  new  ArrayList( new   ...

  10. request.setAttribute()用法

    小问题: JSP1代码 String [] test=new String[2]; test[0]="1"; test[1]="2"; request.setA ...