UILabel富文本 段落格式以及UILabel添加图片
之前文本整理有一点乱,这边重新整理一下,下面是效果图,一共两个UILabel,
富文本整理:
/*NSForegroundColorAttributeName设置字体颜色,对象UIColor;
NSParagraphStyleAttributeName设置段落格式,对象NSMutableParagraphStyle;
NSFontAttributeName设置字体,对象UIFont;
NSBackgroundColorAttributeName设置背景颜色,对象UIColor;
NSKernAttributeName设置字符间距,对象NSNumber
NSStrikethroughStyleAttributeName是指删除线样式 对象NSUnderlineStyle
NSStrikethroughColorAttributeName删除线颜色,对象UIColor
NSUnderlineStyleAttributeName设置下划线,参考删除线
NSUnderlineColorAttributeName设置下划线颜色
NSStrokeWidthAttributeName设置笔画宽度,对象NSNumber,负值填充效果,正值中空效果
NSStrokeColorAttributeName当文字中空或者填充设置文字的描边颜色,对象UIColor;
NSShadowAttributeName 设置阴影,NSShadow对象
NSBaselineOffsetAttributeName设置基线偏移值,对象NSNumber (float),正值上偏,负值下偏
NSObliquenessAttributeName设置字形倾斜度,对象NSNumber (float),正值右倾,负值左倾
NSExpansionAttributeName 设置文本横向拉伸属性,对象NSNumber (float),正值横向拉伸文本,负值横向压缩文本
NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
NSLinkAttributeName设置链接属性,点击后调用浏览器打开指定URL地址,对象NSString,NSUrl,这个只能UITextView可用UITextView的代理方法中shouldInteractWithURL返回true可用
NSTextEffectAttributeName设置文本特殊效果,目前只有一个可用效果 NSTextEffectLetterpressStyle(凸版印刷效果
设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符//没有测出效果
*/
源码展示:
NSString *text = @"池塘大桥下,游过一群鸭,快来快来数一数,二四六七八,嘎嘎嘎嘎,真呀真多呀,fly,数不清到底多少鸭";
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//行间距为
[paragraphStyle setLineSpacing:8];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName : [UIColor blueColor], NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName:[UIFont boldSystemFontOfSize:18]}] ; //设置特殊字体文本格式
NSRange range = [text rangeOfString:@"池塘大桥下"];
[string addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSBackgroundColorAttributeName:[UIColor yellowColor],NSKernAttributeName:@(12),NSStrikethroughStyleAttributeName:@(NSUnderlineStyleDouble),NSStrikethroughColorAttributeName:[UIColor purpleColor]} range:range];//添加属性 NSRange range1 = [text rangeOfString:@"游过"];
[string addAttributes:@{NSStrokeWidthAttributeName:@(-2),NSStrokeColorAttributeName:[UIColor redColor]} range:range1];//添加属性 NSRange range2 = [text rangeOfString:@"一群鸭"];
NSShadow *shadow = [NSShadow new];
[shadow setShadowColor:[UIColor redColor]];
[shadow setShadowOffset:CGSizeMake(5.0f, 5.0f)];//设置x,y轴偏移量
[shadow setShadowBlurRadius:1.0];//设置模糊度
[string addAttributes:@{NSShadowAttributeName:shadow} range:range2];//添加属性 NSRange range3 = [text rangeOfString:@"快来快来"];
[string addAttributes:@{NSBaselineOffsetAttributeName:@(10)} range:range3];//添加属性 NSRange range4 = [text rangeOfString:@"数一数"];
[string addAttributes:@{NSObliquenessAttributeName:@(0.5)} range:range4];//添加属性
NSRange range5 = [text rangeOfString:@"二四"];
[string addAttributes:@{NSExpansionAttributeName:@(0.8)} range:range5];//添加属性
NSRange range6 = [text rangeOfString:@"六七八"];
[string addAttributes:@{NSExpansionAttributeName:@(-0.8)} range:range6];//添加属性 /**
没有测出具体的区别
NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写
@[@(NSWritingDirectionLeftToRight | NSWritingDirectionEmbedding)]
@[@(NSWritingDirectionLeftToRight | NSWritingDirectionOverride)]
@[@(NSWritingDirectionRightToLeft | NSWritingDirectionEmbedding)]
@[@(NSWritingDirectionRightToLeft | NSWritingDirectionOverride)]
*/ [string addAttributes:@{NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft | NSWritingDirectionEmbedding)]} range:NSMakeRange(0, text.length)];//添加属性 [string addAttributes:@{NSVerticalGlyphFormAttributeName:@1} range:NSMakeRange(0, text.length)];//添加属性 NSRange range8 = [text rangeOfString:@"嘎嘎嘎嘎"];
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
[string addAttributes:@{NSLinkAttributeName:url} range:range8];//添加属性
NSRange range9 = [text rangeOfString:@"真呀真多呀"];
[string addAttributes:@{NSTextEffectAttributeName:NSTextEffectLetterpressStyle} range:range9];//添加属性
NSRange range10 = [text rangeOfString:@"fly"];
[string addAttributes:@{NSLigatureAttributeName:@0} range:range10];//添加属性 self.label.attributedText = string;
NSMutableParagraphStyle用法整理:
NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; //使用一张图片作为Attachment数据 attachment.image = [UIImage imageNamed:@"chepaihao"]; //这里bounds的x值并不会产生影响 attachment.bounds = CGRectMake(5, (self.label1.font.lineHeight - self.label1.font.pointSize) / 2, 20, 10); NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle1 setLineSpacing:20]; //首行缩进 [paragraphStyle1 setFirstLineHeadIndent:20]; [paragraphStyle1 setLineBreakMode:NSLineBreakByTruncatingMiddle]; //两端对齐的)文本对齐方式 paragraphStyle1.minimumLineHeight = 10;//最低行高 paragraphStyle1.maximumLineHeight = 20;//最大行高 paragraphStyle1.paragraphSpacing = 45;//段与段之间的间距 // paragraphStyle1.paragraphSpacingBefore = 1000;//段首行空白空间 paragraphStyle1.baseWritingDirection = NSWritingDirectionLeftToRight;//从左到右的书写方向(一共➡️三种) // paragraphStyle1.lineHeightMultiple = -100;没有测出效果 // paragraphStyle1.hyphenationFactor = 1;//连字属性 在iOS,唯一支持的值分别为0和1 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"你是一个好人,你是一个好人,你是一个好人,你是一个好人,你是一个好人,你是一个好人,你是一个好人,你是一个好人,你是一个好人,你是一个好人" attributes:@{NSParagraphStyleAttributeName: paragraphStyle1}]; [attributedString appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]]; [attributedString appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]]; [attributedString appendAttributedString:[[NSMutableAttributedString alloc] initWithString:@"\n你是一个好人,你是一个好人,你是一个好人,你是一个好人,你是一个好人,你是一个好人,你是一个好人,你是一个好人,你是一个好人,你是一个好人" attributes:@{NSParagraphStyleAttributeName: paragraphStyle1}]]; [attributedString appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]]; self.label1.backgroundColor = [UIColor redColor]; // self.label1.textAlignment = NSTextAlignmentCenter; self.label1.attributedText = attributedString; // self.label1.lineBreakMode = NSLineBreakByCharWrapping;
获取富文本的高度和普通label的高度一样:
CGSize attSize = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
注意事项:我们设置NSMutableParagraphStyle的lineSpacing可能和我们想象的不大一样,间距会比我们想象的高一点点,如果我们设置10个像素,就需要这么设置,减去系统自带的间距
paragraphStyle.lineSpacing =
10 - (label.font.lineHeight - label.font.pointSize);
UILabel富文本 段落格式以及UILabel添加图片的更多相关文章
- Swift中的富文本注释格式
Apple当前启用了Markup语法形式的富文本注释格式,并且为此列出了官方文档,可以参考此链接:https://developer.apple.com/library/ios/documentati ...
- UIlabel - 富文本属性
1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFontOfSize:_fontS ...
- UILabel(富文本)
本文转载至 http://www.jianshu.com/p/5d24d22f99c3 富文本 NSString *str = @"人生若只如初见,何事秋风悲画扇.\n等闲变却故人心,却道故 ...
- JAVAEE——宜立方商城04:图片服务器FastDFS、富文本编辑器KindEditor、商品添加功能完成
1. 学习计划 1.图片上传 a) 图片服务器FastDFS b) 图片上传功能实现 2.富文本编辑器的使用KindEditor 3.商品添加功能完成 2. 图片服务器的安装 1.存储空间可扩展. 2 ...
- Java 读取Word文本/段落格式属性
本文介绍通过Java后端程序代码来读取Word文本和段落格式的方法. 本次测试环境如下: Word版本:2013 编译环境:IntelliJ IDEA2018 Work库:free spire.doc ...
- vue富文本编辑器vue-quill-editor使用总结(包含图片上传,拖拽,放大和缩小)
vue-quill-editor是vue很好的富文本编辑器,富文本的功能基本上都支持,样式是黑白色,简洁大方. 第一步下载 vue-quill-editor: npm i vue-quill-edit ...
- 富文本编辑器CKeditor的配置和图片上传,看完不后悔
CKeditor是一款富文本编辑器,本文将用极为简单的方式介绍一下它的使用和困扰大家很久的图片上传问题,要有耐心. 第一步:如何使用 1.官网下载https://ckeditor.com/ckedit ...
- vue v-html 富文本解析 空格,换行,图片大小问题
1.保留空格,换行属性 //保留换行空格问题 white-space: pre-wrap; 2.超出部分,强制换行,一般用于数字 //富文本换行 word-wrap: break-word; tabl ...
- C# WinForm 富文本编辑器 用kindeditor实现本地图片只选取不上传到编辑器
以下资料有参考网上其它童鞋作品,原作者看到务喷!!!! 以下资料有参考网上其它童鞋作品,原作者看到务喷!!!! 重要的事只要说两遍... 网上找了几天关于WinForm富文本编辑效果都不理想,各种坑, ...
随机推荐
- RN(八)——react-native-image-viewer & react-native-swiper
以项目(业务GO)为例: react-native-swiper 轮播(用在首页的图集轮播) https://github.com/leecade/react-native-swiper react- ...
- mysql分组查询获取组内某字段最大的记录
id sid cid 1 1 12 1 23 2 1 以sid分组,最后取cid最大的那一条,以上要取第2.3条 1 方法一: select * from (select * from table o ...
- iOS - 代码规范的提示
我们在些程序时会发现苹果里面有好多非常好的提示 比如: 1.每次SDK升级后 一些方法的方法已经过时了,这时候会给你提示描述该方法已经过期(作用:1.兼顾老版本 2.给开发者一个提示) 2.有时候项目 ...
- IT零起步-CentOS6.4部署OpenVPN服务器
OpenVPN是一个用于创建虚拟专用网络加密通道的软件包,实现二/三层的基于隧道的VPN.最早由James Yonan编写.OpenVPN允许创建的VPN使用公开密钥.数字证书.或者用户名/密码来进行 ...
- Express+Less+Gulp配置高效率开发环境
版权声明:本文由金朝麟原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/627049001486519548 来源:腾云阁 h ...
- 微信公众号关联(小游戏 小程序 跳转 盒子 wx.navigateToMiniProgram)
参考: 公众号关联小程序 关联公众号 关联后,登录小游戏,可在设置-关联设置中看到关联的公众号 在小游戏中使用wx.navigateToMiniProgram wx.navigateToMiniPro ...
- Unity3D笔记二十 多媒体与网络
1 游戏音频 1.游戏音乐:如游戏背景音乐 2.游戏音效:如开枪或打怪物时“砰砰”的游戏音效 Unity 3D游戏引擎共支持4种音乐格式的文件,具体如下. aiff:适用于较短的音乐文件,可用作游戏音 ...
- 9.7 Django 书单列表页面
昨天的迭代版本,增加了编辑出版社,编辑列表,增添了返回页面! 具体的看 github : https://github.com/TrueNewBee/pythonDemo 看一下效果图: 整体来说还是 ...
- java8新特性之Optional类
NullPointException可以说是所有java程序员都遇到过的一个异常,虽然java从设计之初就力图让程序员脱离指针的苦海,但是指针确实是实际存在的,而java设计者也只能是让指针在java ...
- TOP100summit 2017:微博如何做到1小时增加一千台服务器应对鹿晗恋情带来的流量暴增
10月8日中午12点,鹿晗和关晓彤宣布恋情,年近30的我并不关心小鲜肉们的分分合合,但是作为壹佰案例这个软件研发行业自媒体的小编,我更关注因此引发的微博宕机事件. 鹿晗公布恋情的微博获得462884次 ...