UILabel属性小解
#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属性小解的更多相关文章
- IOS开发UI基础UILabel属性
UILabel属性 1.text:设置标签显示的文本. 2.attributedText:设置标签属性文本. Ios代码 NSString *text = @"first"; N ...
- 基本控件文档-UILabel属性
CHENYILONG Blog 基本控件文档-UILabel属性 Fullscreen UILabel属性技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http ...
- 基本控件文档-UILabel属性---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/ ...
- UIlabel 属性text
UILabel *pLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,100,200,100)]; pLabel.text = @"测试 ...
- iOS -Swift 3.0 -UILabel属性大全
昨天研究了一下苹果近两年新出的Swift语言,感觉学起来并不是很吃力,毕竟自己有过Objective-C的语言功底,所以各方面的属性控件还是一眼就可以认出的,只是Swift的写法与Objective- ...
- UILabel属性
1.text:设置标签显示文本. 2.attributedText:设置标签属性文本. Ios代码 NSString *text = @"first"; NSMutableAttr ...
- UILabel 属性祥记
创建label UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)]; 设置背景色 label1. ...
- 文档学习 - UILabel - 属性详解
#import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super vie ...
- transform 属性小解
css中transform包括三种: 旋转rotate(), translate()移动, 缩放scale(), skew()扭曲以及矩形变换matrix() 语法: transform: none ...
随机推荐
- 内存错误:CRT detected that the application wrote to memory after end of heap buffer
今天调试测试代码时,发现在用完了new出来的内存buf后,在执行delete时报错了,具体信息为: HEAP_CORRUPTION_DETECTED: after Normal block(#908) ...
- Redis 集成Spring(spring-data-redis)
Spring-data-redis是spring大家族的一部分,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行 ...
- 游戏对象、组件和Prefabs
如标题所言,本文由3个部分组成,分别讲述游戏对象.组件和Prefabs(预设体). 1. 游戏对象 任何游戏对象都由组件组成,组件是实现一切功能所必需的.我们创建的对象会在Hierarchy视图中显示 ...
- zabbix 布署实践【7 H3C网络设备监控模版制作思路】
我们知道,zabbix安装后自带Template OS Linux 模版已满足了绝大部分Linux服务器的基础环境监控,只是我们在其模版上稍微修改,可配合将SWAP监控取消,另存为一个叫OS Linu ...
- 一名测试初学者听JAVA视频笔记(一)
搭建pho开发环境与框架图 韩顺平 第一章: No1 关于文件以及文件夹的管理 将生成的文本文档做成详细信息的形式,显示文件修改时间以及文件大小,便于文件查看和管理,也是对于一名IT人士高效能工作的 ...
- Redmine插件及使用
Plugins Plugin list A full list of available Redmine plugins can be found at the Plugin Directory. M ...
- USACO 3.3 Camelot
CamelotIOI 98 Centuries ago, King Arthur and the Knights of the Round Table used to meet every year ...
- Android Tips
(1).设置图片缓存大小,一般可以设置为内存的1/8 int memoryCache = (int) (Runtime.getRuntime().maxMemory() / 8); (2). (3). ...
- 使用navicat连接远程linux的mysql中文显示乱码的问题
在navicat对应的连接上 右键->连接属性->高级 去掉使用mysql字符集 然后上面的编码选择 (65001)utf-8 接着打开连接 找到对应的数据库 右键 数据库属性 把编码 ...
- SqlParameter 中 top 的使用
public DataTable GetAdminTopDCSCheckReport(int top) { StringBuilder strSql = new StringBuilder(); st ...