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 ...
随机推荐
- HTML <form> 标签的 enctype 属性
HTML <form> 标签 定义和用法 enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码. 默认地,表单数据会编码为 "application/x-www- ...
- 微信--高效解决token及授权用户openid的持久化处理办法
摘要 关于微信开发的话题,例子确实已经有不少,但大部分都是人云亦云,很多小细节或者需要注意的地方却大多没有讲清楚,这令很多刚开始开发的人感觉大很迷茫.而我今天要说的话题,主要着眼于两个方面. 一:如 ...
- android 在5.0以后不允许使用隐式Intent方式来启动Service
android5.0以后不能使用隐式intent :需要指定Intent的ComponentName信息:intent.setComponent(xxx),或指定Intent的setPackage(& ...
- ACM博弈问题小试
题目: 取石子(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 一天,TT在寝室闲着无聊,和同寝的人玩起了取石子游戏,而由于条件有限,他/她们是用旺仔小馒头当作 ...
- angularJS 自定义元素和属性
创造自定义元素和属性的方法是:directive('string',function(){ return{}; }); ①函数接收两个参数:一个字符串(指令的名字),一个函数: ②回调函数必须返回一个 ...
- Python map多线程
import os import PIL from multiprocessing import Pool from PIL import Image SIZE = (75,75) SAVE_DIRE ...
- 用SecureCRT连接虚拟机
1.Root用户进入虚拟机系统 2.打开控制台 3.永久关闭防火墙,打开sshd,这样SecureCRT才能连接 chkconfig iptables off;service sshd start 4 ...
- Objective-C Runtime 运行时之五:协议与分类(转载)
Objective-C中的分类允许我们通过给一个类添加方法来扩充它(但是通过category不能添加新的实例变量),并且我们不需要访问类中的代码就可以做到. Objective-C中的协议是普遍存在的 ...
- 一个在浏览器端将html 转为pdf 的js 插件 jsPDF
<!DOCTYPE html> <html> <head> <title>test</title> <meta http-equiv= ...
- python 命名规范
参考Google开源项目风格指南:https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/cont ...