#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

NSString * context = @"\n27日下午,美国总统奥巴马首度访问二战核爆地——日本广岛,\n在和平纪念公园向核爆纪念碑献花圈。任期接近尾声的奥巴马,\n为了无核武的理想,最终踏上被美国核弹轰炸过的广岛。\n\n在他来之前的70多年里,美国的历任总统,没有任何一位在\n任期内来过。作为美国总统去直面核爆受害者,奥巴马的广岛之行\n从一开始就备受热议,外界更关注奥巴马是否会道歉,日本右翼人士则直接\n要求他向成千上万核爆受害者道歉。对此,奥巴马临行前接受日\n媒专访时曾明确表示,不会道歉,\n“检讨当年投放原子弹的对错,那是历史学家的工作”。";

//字体大小

UIFont *font = [UIFont systemFontOfSize:18];

//行间距

CGFloat lineSpace = 8;

//段间距

CGFloat paragraphSpacing = 0;

//label宽高

CGSize labSize = CGSizeMake(400, 1000);

//字间距

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;

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

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

label.numberOfLines = 0;

label.backgroundColor = [UIColor purpleColor];

[label sizeToFit];

label.frame =  CGRectMake(40, 40, 300, sizeHeight);

label.attributedText = [[NSAttributedString alloc] initWithString:context attributes:dic];

[self.view addSubview:label];

}

/*

*给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

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

NSString * context = @"\n27日下午,美国总统奥巴马首度访问二战核爆地——日本广岛,\n在和平纪念公园向核爆纪念碑献花圈。任期接近尾声的奥巴马,\n为了无核武的理想,最终踏上被美国核弹轰炸过的广岛。\n\n在他来之前的70多年里,美国的历任总统,没有任何一位在\n任期内来过。作为美国总统去直面核爆受害者,奥巴马的广岛之行\n从一开始就备受热议,外界更关注奥巴马是否会道歉,日本右翼人士则直接\n要求他向成千上万核爆受害者道歉。对此,奥巴马临行前接受日\n媒专访时曾明确表示,不会道歉,\n“检讨当年投放原子弹的对错,那是历史学家的工作”。";

//字体大小

UIFont *font = [UIFont systemFontOfSize:18];

//行间距

CGFloat lineSpace = 8;

//段间距

CGFloat paragraphSpacing = 0;

//label宽高

CGSize labSize = CGSizeMake(400, 1000);

//字间距

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;

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

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

label.numberOfLines = 0;

label.backgroundColor = [UIColor purpleColor];

[label sizeToFit];

label.frame =  CGRectMake(40, 40, 300, sizeHeight);

label.attributedText = [[NSAttributedString alloc] initWithString:context attributes:dic];

[self.view addSubview:label];

}

/*

*给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

附上效果图

UILabel属性小解的更多相关文章

  1. IOS开发UI基础UILabel属性

    UILabel属性 1.text:设置标签显示的文本. 2.attributedText:设置标签属性文本. Ios代码 NSString *text = @"first";  N ...

  2. 基本控件文档-UILabel属性

    CHENYILONG Blog 基本控件文档-UILabel属性 Fullscreen UILabel属性技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http ...

  3. 基本控件文档-UILabel属性---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址   //转载请注明出处--本文永久链接:http://www.cnblogs.com/ ...

  4. UIlabel 属性text

    UILabel *pLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,100,200,100)]; pLabel.text = @"测试 ...

  5. iOS -Swift 3.0 -UILabel属性大全

    昨天研究了一下苹果近两年新出的Swift语言,感觉学起来并不是很吃力,毕竟自己有过Objective-C的语言功底,所以各方面的属性控件还是一眼就可以认出的,只是Swift的写法与Objective- ...

  6. UILabel属性

    1.text:设置标签显示文本. 2.attributedText:设置标签属性文本. Ios代码 NSString *text = @"first"; NSMutableAttr ...

  7. UILabel 属性祥记

    创建label UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)]; 设置背景色 label1. ...

  8. 文档学习 - UILabel - 属性详解

    #import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super vie ...

  9. transform 属性小解

    css中transform包括三种: 旋转rotate(), translate()移动, 缩放scale(), skew()扭曲以及矩形变换matrix() 语法: transform: none ...

随机推荐

  1. [置顶] cuzy sdk之起源

    程序员都熟知一句话, “不要重复制造轮子".应该说互联网的繁荣和普及给刚入门的软件工程师还是带来很大的好处的.尤其是github,sourceforge在国内日渐的流行. 在学习iOS和an ...

  2. Xshell和VirtualBox虚机CentOS7的连接

    后面的不能连接问题,出处为 http://m.blog.csdn.net/article/details?id=52755571 1.centos7的ip ,这里的enp0s3相当于eth0,是一个默 ...

  3. Android Studio利用异步任务AsyncTask发送post请求获取json数据

    syncTask,是Android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以通过接口实现UI进度更新),最后反馈执行的结果给UI主 ...

  4. 转:Monoids and Finger Trees

    转自:http://apfelmus.nfshost.com/articles/monoid-fingertree.html This post grew out of the big monoid ...

  5. python学习-基础语法

    字符编码 1.python 2.x 默认是ASCII 编码 不支持中文,所以在代码有中文的时候 需要在文件最上一行加上#coding=utf-8.python 3.x则没有该问题. 变量命名规则 1. ...

  6. JavaScript - 平稳退化

    JavaScript使用window对象的open()方法来创建新的浏览器窗口.这个方法有三个参数:window.open(url,name,features)这三个参数都是可选的.1.第一个参数是想 ...

  7. InnoDB与MyISAM引擎区别

    mysql中InnoDB与MyISAM两种数据库引擎的区别: 一.InnoDB引擎: 1.支持事务性, 2.支持外部键, 3.行级锁, 4.不保存表的具体行数,执行select count(*) fr ...

  8. Cracking the Coding Interview 第一章

    第一章:数组与字符串 1 数组与字符串 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,T ...

  9. Linux下的数据监控工具

    Vmstat Vmstat,virtual memmory statistics(虚拟内存统计),主要是对操作系统的内存信息.进程状态.cpu活动等进行监视,但是它不能对某个进程进行深入的分析. Pr ...

  10. 用SecureCRT连接虚拟机

    1.Root用户进入虚拟机系统 2.打开控制台 3.永久关闭防火墙,打开sshd,这样SecureCRT才能连接 chkconfig iptables off;service sshd start 4 ...