动态计算字符串的字体大小并据此设置行间距---NSAttributedString
来源:互联网 发布:万国数据员工待遇 编辑:程序博客网 时间:2024/09/21 09:02
#pragma mark-计算字符串的字体大小并据此设置行间距
+ (NSMutableAttributedString *)caculateLabelHeightWithMutableAttributedString:(NSString *)string andFontSize:(CGFloat)fontSize {
NSMutableAttributedString *attributeStr = [[NSMutableAttributedStringalloc]initWithString:string];
NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStylealloc]init];
//设置行间距
[paragraphStyle1 setLineSpacing:7];
//NSParagraphStyleAttributeName;(段落)
[attributeStr addAttribute:NSParagraphStyleAttributeNamevalue:paragraphStyle1range:NSMakeRange(0, [attributeStrlength])];
//设置字体大小
[attributeStr addAttribute:NSFontAttributeNamevalue:[UIFontboldSystemFontOfSize:fontSize]range:NSMakeRange(0, [attributeStrlength])];
//设置颜色
[attributeStr addAttribute:NSForegroundColorAttributeNamevalue:[UIColorcolorWithRed:139/256.0green:137/256.0blue:137/256.0alpha:1]range:NSMakeRange(0, [attributeStrlength])];
return attributeStr;
}
//计算字符串在label上的高度
+ (CGSize)caculateMutableAttribu:(NSMutableAttributedString *)attributeStr andLabelWidth:(CGFloat)width {
//计算字符串在label上的高度
CGSize labelSize = [attributeStrboundingRectWithSize:CGSizeMake(width,MAXFLOAT)options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeadingcontext:nil].size;
return labelSize;
}
如下图所示:字体的大小和行间距已经动态计算好了
#pragma mark--给某个字体设置颜色
- (NSMutableAttributedString *)changeSearchTextColorWithDic:(NSString *)string {
//在姓名这个字符串中搜索到self.searchStr
NSRange range = [string rangeOfString:self.searchStr];
//NSAttributedString是一个带有属性的字符串,通过该类可以灵活地操作和呈现多种样式的文字
NSMutableAttributedString *nameText = [[[NSMutableAttributedStringalloc]initWithString:string]autorelease];
//创建NSShadow对象
NSShadow *shadow = [[NSShadowalloc]init];
shadow.shadowOffset = CGSizeMake(3, 16);
shadow.shadowBlurRadius =2.5;
shadow.shadowColor = [UIColorpurpleColor];
//给搜索的字符设置格式
[nameText addAttributes:@{NSForegroundColorAttributeName:[UIColorredColor],NSUnderlineStyleAttributeName:[NSNumbernumberWithInteger:NSUnderlineStyleSingle],NSFontAttributeName:[UIFontsystemFontOfSize:27],NSStrokeWidthAttributeName:@(3),NSShadowAttributeName:shadow}range:range];
[shadow release];
return nameText;
}
什么是NSAttributedString:
按个人的理解,NSAttributedString是一个带有属性的字符串,通过该类可以灵活地操作和呈现多种样式的文字数据。
在iOS中AttributedString也分为NSAttributedString和NSMutableAttributedString,一个AttributedString的对象包含很多的属性,每一个属性都有其对应的字符区域,一般是使用NSRange来进行描述的。
使用AttributedString的方式通常有两种:
方式一:
NSString *originStr = @"Hello,中秋节!";
UILabel *label = [[UILabelalloc]init];
//创建 NSMutableAttributedString
NSMutableAttributedString *attributedStr01 = [[NSMutableAttributedString alloc] initWithString: originStr];
//添加属性
//给所有字符设置字体为Zapfino,字体高度为15像素
[attributedStr01 addAttribute: NSFontAttributeName value: [UIFont fontWithName: @"Zapfino" size: 15]
range: NSMakeRange(0, originStr.length)];
//分段控制,最开始4个字符颜色设置成蓝色
[attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(0, 4)];
//分段控制,第5个字符开始的3个字符,即第5、6、7字符设置为红色
[attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(4, 3)];
//赋值给显示控件label的 attributedText
label.attributedText = attributedStr01;
运行结果:
方式二:
首先创建属性字典,初始化各种属性,然后和需要控制的文本一起创建并赋值给控件的AttributedText,该方法适合于需要控制的文本较多整体控制的情况,通常是从文件中读取的大段文本控制。
NSString *originStr = @"Hello,中秋节!";
UILabel *label = [[UILabelalloc]init];
- 1 //创建属性字典
- 2 NSDictionary *attrDict = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 27], NSForegroundColorAttributeName: [UIColor blueColor] };
- 3
- 4 //创建 NSAttributedString 并赋值
NSMutableAttributedString * attributedStr02 = [[NSMutableAttributedStringalloc]initWithString: originStr];
[nameText addAttributes: attrDict range:NSMakeRange(4, 3)];
//赋值给显示控件label的attributedText
label.attributedText = attributedStr02;
属性详解:
// NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
// NSForegroundColorAttributeNam 设置字体颜色,取值为 UIColor对象,默认值为黑色--NSForegroundColorAttributeName设置的颜色与UILabel的textColor属性设置的颜色在地位上是相等的,谁最后赋值,最终显示的就是谁的颜色。
// NSBackgroundColorAttributeName 设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil,透明色--这说明 NSForegroundColorAttributeName 和 NSBackgroundColorAttributeName 的低位是相等的,哪个属性最后一次赋值,就会冲掉前面的效果,
// NSLigatureAttributeName 设置连体属性,取值为NSNumber对象(整数),0表示没有连体字符,1表示使用默认的连体字符--NSLigatureAttributeName的取值为NSNumber对象,所以不能直接将一个整数值赋给它,创建 NSNumber 对象的方法有很多,或者可以简写成 @(int)
// NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
// NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数)
// NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色
// NSUnderlineStyleAttributeName 设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
// NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色
// NSStrokeWidthAttributeName 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
// NSStrokeColorAttributeName 填充部分颜色,不是字体颜色,取值为 UIColor 对象
// NSShadowAttributeName 设置阴影属性,取值为 NSShadow 对象
NSShadow *shadow1 = [[NSShadow alloc] init]; //NSShadow 对象比较简单,只有3个属性:阴影颜色,模糊半径和偏移
shadow1.shadowOffset = CGSizeMake(3, 3); //阴影偏移(X方向偏移和Y方向偏移)
shadow1.shadowBlurRadius = 0.5; //模糊半径
shadow1.shadowColor = [UIColor orangeColor]; //阴影颜色
// NSTextEffectAttributeName 设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
// NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
// NSObliquenessAttributeName 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
// NSExpansionAttributeName 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
// NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写
// NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0表示横排文本,1表示竖排文本
// NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址
// NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
// NSParagraphStyleAttributeName 设置文本段落排版格式,取值为 NSParagraphStyle 对象
- 动态计算字符串的字体大小并据此设置行间距---NSAttributedString
- NSAttributedString 动态计算字符串的高度ios6,7
- 设置行间距,计算带行间距的文本高度
- 动态的计算带有行间距的字段的长高
- iOS 计算带有行间距的字符串的高度
- UILabel 设置行间距 && 自动计算text 的frame
- 设置行间距,计算label的高度 ios
- iOS设置UILabel的行间距并自适应高度
- iOS 设置UILabel的行间距并自适应高度
- iOS UILabel设置行间距和字间距并计算高度
- iOS UILabel设置行间距和字间距并计算高度
- iOS UILabel设置行间距和字间距并计算高度
- iOS UILabel设置行间距和字间距并计算高度
- iOS UILabel设置行间距和字间距并计算高度
- iOS开发技巧之:UILabel设置行间距和字间距并计算高度
- iOS UILabel设置行间距和字间距并计算高度
- iOS UILabel 设置 行间距 和 字间距 并计算高度
- iOS UILabel设置行间距和字间距并计算高度
- 一些公式或者知识(不断补充)
- 继承自UITableViewController 需要往视图上添加固定按钮
- 关于OKHTTP与图片缓存的使用
- pomelo集群配置模式下的日志问题
- 取出带有tag的控件方法
- 动态计算字符串的字体大小并据此设置行间距---NSAttributedString
- windows桌面便笺使用小技巧
- MapReduce-XML处理-定制InputFormat及定制RecordReader
- 专注
- shell下操作快捷键
- UltraEdit快捷键
- rhel5.x&rhel6.x更换CentOS yum源
- ARM核心板,三星S5P4418核心板
- php7的一些新特性