动态计算字符串的字体大小并据此设置行间距---NSAttributedString

#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个字符,即第567字符设置为红色

    [attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(4, 3)];

    

//赋值给显示控件label attributedText

label.attributedText = attributedStr01;

运行结果:



方式二:

   首先创建属性字典,初始化各种属性,然后和需要控制的文本一起创建并赋值给控件的AttributedText,该方法适合于需要控制的文本较多整体控制的情况,通常是从文件中读取的大段文本控制。


NSString *originStr = @"Hello,中秋节!"; 

UILabel *label = [[UILabelalloc]init];


  1. 1 //创建属性字典  
  2. 2 NSDictionary *attrDict = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 27],   NSForegroundColorAttributeName: [UIColor blueColor] };  
  3. 3  
  4. 4 //创建 NSAttributedString 并赋值  

NSMutableAttributedString * attributedStr02 = [[NSMutableAttributedStringalloc]initWithString: originStr];  


[nameText addAttributes: attrDict range:NSMakeRange(4, 3)];

//赋值给显示控件labelattributedText

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 对象





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值