IOS之UILabel富文本

NSMutableAttributedString *fruitPrice = [[NSMutableAttributedStringalloc]initWithString:@"16.99"];

    [fruitPrice beginEditing];

    //设置字体大小

    [fruitPrice addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:27.0f]range:NSMakeRange(0,2)];

    [fruitPrice addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:17.0f]range:NSMakeRange(2,3)];

    //设置字体颜色

    [fruitPrice addAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor]range:NSMakeRange(0,5)];

    [fruitPrice addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:10.0f]range:NSMakeRange(5,1)];

    [fruitPrice addAttribute:NSForegroundColorAttributeNamevalue:[UIColorgrayColor]range:NSMakeRange(5,1)];

    //加粗 

    [fruitPrice addAttribute:NSStrokeWidthAttributeNamevalue:[NSNumbernumberWithInt:-3]range:NSMakeRange(0,5)];

    //字符间距

    [fruitPrice addAttribute:NSKernAttributeNamevalue:[NSNumbernumberWithInt:4]range:NSMakeRange(4,2)];

    //删除线

    [fruitPrice addAttribute:NSStrikethroughStyleAttributeNamevalue:[NSNumbernumberWithInt:NSUnderlineStyleSingle]range:NSMakeRange(0,6)];


    self.priceLabel.attributedText = fruitPrice;


属性设置:

// NSFontAttributeName                设置字体属性,默认值:字体:Helvetica(Neue) 字号:12

// NSForegroundColorAttributeNam      设置字体颜色,取值为 UIColor对象,默认值为黑色
// NSBackgroundColorAttributeName     设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
// NSLigatureAttributeName            设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
// NSKernAttributeName                设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
// NSStrikethroughStyleAttributeName  设置删除线,取值为 NSNumber 对象(整数)
// NSStrikethroughColorAttributeName  设置删除线颜色,取值为 UIColor 对象,默认值为黑色
// NSUnderlineStyleAttributeName      设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
// NSUnderlineColorAttributeName      设置下划线颜色,取值为 UIColor 对象,默认值为黑色
// NSStrokeWidthAttributeName         设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
// NSStrokeColorAttributeName         填充部分颜色,不是字体颜色,取值为 UIColor 对象
// NSShadowAttributeName              设置阴影属性,取值为 NSShadow 对象
// NSTextEffectAttributeName          设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
// NSBaselineOffsetAttributeName      设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
// NSObliquenessAttributeName         设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
// NSExpansionAttributeName           设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
// NSWritingDirectionAttributeName    设置文字书写方向,从左向右书写或者从右向左书写
// NSVerticalGlyphFormAttributeName   设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
// NSLinkAttributeName                设置链接属性,点击后调用 浏览器打开指定URL地址
// NSAttachmentAttributeName          设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
// NSParagraphStyleAttributeName      设置文本段落排版格式,取值为 NSParagraphStyle 对象 
 
 
下面就一一举例说明:
 
1. NSFontAttributeName
 
 
//NSForegroundColorAttributeName 设置字体颜色,取值为 UIColor,默认为黑色
 
NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
 
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
 
 
 
注意:
 
       NSForegroundColorAttributeName设置的颜色与UILabel的textColor属性设置的颜色在地位上是相等的,谁最后赋值,最终显示的就是谁的颜色。
 
 
 
2. NSBackgroundColorAttributeName
 
 
//NSForegroundColorAttributeName 设置字体颜色,取值为 UIColor,默认为黑色
 
NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
 
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
 
 
//NSBackgroundColorAttributeName 设置字体所在区域背景的颜色,取值为UIColor,默认值为nil
 
NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };
NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };
NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };
 
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];
 
 
 
 
       仔细观察会发现个问题,我并没有关闭 NSForegroundColorAttributeName 属性,但是在运行结果中,所有字体的颜色都变成了默认色——黑色,这说明 NSForegroundColorAttributeName 和 NSBackgroundColorAttributeName 的低位是相等的,跟前面介绍的 textColor 一样,哪个属性最后一次赋值,就会冲掉前面的效果,若是我们把属性代码顺序交换一下
 
 
//NSBackgroundColorAttributeName 设置字体所在区域背景的颜色,取值为UIColor,默认值为nil
 
NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };
NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };
NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };
 
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];
 
//NSForegroundColorAttributeName 设置字体颜色,取值为 UIColor,默认为黑色
 
NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
 
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
 
 
 
 
但是textColor属性可以与 NSBackgroundColorAttributeName 属性叠加
 
 
_label01.textColor = [UIColor greenColor];
_label02.textColor = [UIColor yellowColor];
_label03.textColor = [UIColor blueColor];
 
//NSForegroundColorAttributeName 设置字体颜色,取值为 UIColor,默认为黑色
 
NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
 
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
 
 
//NSBackgroundColorAttributeName 设置字体所在区域背景的颜色,取值为UIColor,默认值为nil
 
NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };
NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };
NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };
 
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];
 
 
 
       虽然 textColor 在 NSFontAttributeName 之前赋值,但是由于 NSFontAttributeName 的属性效果被NSBackgroundColorAttributeName 属性冲掉了,所以最终显示了 textColor 的颜色。
 
 
 
3. NSLigatureAttributeName
 
//NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符,
//                        2 表示使用所有连体符号,默认值为 1(iOS 不支持 2)
 
NSString *ligatureStr = @"flush";
 
NSDictionary *attrDict1 = @{ NSLigatureAttributeName: [NSNumber numberWithInt: 0],
                             NSFontAttributeName: [UIFont fontWithName: @"futura" size: 30] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: ligatureStr attributes: attrDict1];
 
NSDictionary *attrDict2 = @{ NSLigatureAttributeName: @(1),
                             NSFontAttributeName: [UIFont fontWithName: @"futura" size: 30] 
                             };
_label02.attributedText = [[NSAttributedString alloc] initWithString: ligatureStr attributes: attrDict2];
       由于要展示连体字符,所以将前面使用的带有中文的字符串换成 flush
 
       NSLigatureAttributeName的取值为NSNumber对象,所以不能直接将一个整数值赋给它,创建 NSNumber 对象的方法有很多,或者可以简写成 @(int)
 
 
 
 
 
       注意观察字母f和l之间的变化。
 
       感觉连写就是一个艺术字功能,当字符f和l组合使用组合符号(所谓的字形(glyph))绘制时,看起来确实更加美观。但是并非所有的字符之间都有组合符号,事实上,只有某些字体中得某些字符的组合(如字符f和l,字符f和i等)才具有美观的组合符号。
 
 
 
4. NSKernAttributeName
 
 
//NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
   
   
  NSDictionary *attrDict1 = @{ NSKernAttributeName: @(-3),
                               NSFontAttributeName: [UIFont systemFontOfSize: 20]
                               };
  _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
   
   
  NSDictionary *attrDict2 = @{ NSKernAttributeName: @(0),
                               NSFontAttributeName: [UIFont systemFontOfSize: 20]
                               };
  _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
   
   
  NSDictionary *attrDict3 = @{ NSKernAttributeName: @(10),
                               NSFontAttributeName: [UIFont systemFontOfSize: 20]
                               };
  _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
 
 
 
 
 
 
5. NSStrikethroughStyleAttributeName
 
 
//NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值
// NSUnderlineStyleNone   不设置删除线
// NSUnderlineStyleSingle 设置删除线为细单实线
// NSUnderlineStyleThick  设置删除线为粗单实线
// NSUnderlineStyleDouble 设置删除线为细双实线
 
 
NSDictionary *attrDict1 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle),
                             NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
 
 
NSDictionary *attrDict2 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleThick),
                             NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
 
 
NSDictionary *attrDict3 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleDouble),
                             NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
注意:
 
       虽然使用了枚举常量,但是枚举常量的本质仍为整数,所以同样必须先转化为 NSNumber 才能使用
 
       删除线和下划线使用相同的枚举常量作为其属性值
 
       目前iOS中只有上面列出的4中效果,虽然我们能够在头文件中发现其他更多的取值,但是使用后没有任何效果
 
 
 
 
 
 
可以看出,中文和英文删除线的位置有所不同
 
       另外,删除线属性取值除了上面的4种外,其实还可以取其他整数值,有兴趣的可以自行试验,取值为 0 - 7时,效果为单实线,随着值得增加,单实线逐渐变粗,取值为 9 - 15时,效果为双实线,取值越大,双实线越粗。
 
NSDictionary *attrDict1 = @{ NSStrikethroughStyleAttributeName: @(1),
                             NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
 
 
NSDictionary *attrDict2 = @{ NSStrikethroughStyleAttributeName: @(3),
                             NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
 
 
NSDictionary *attrDict3 = @{ NSStrikethroughStyleAttributeName: @(7),
                             NSFontAttributeName: [UIFont systemFontOfSize:20] };
_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
 
 
 
 
 
6. NSStrikethroughColorAttributeName
 
 
//NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色
   
  NSDictionary *attrDict1 = @{ NSStrikethroughColorAttributeName: [UIColor blueColor],
                               NSStrikethroughStyleAttributeName: @(1),
                               NSFontAttributeName: [UIFont systemFontOfSize:20] };


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: iOS 富文本可以通过使用 NSAttributedString 和 NSTextAttachment 类加载 GIF 图片,达到将富文本与动态效果结合的效果。 首先,需要将 GIF 图片转换为 NSData 格式,可通过使用 NSData 的类方法 dataWithContentsOfFile 或者 dataWithContentsOfURL 来实现。 接着,创建 NSTextAttachment 实例,并以 NSData 格式将 GIF 图片作为参数传入。然后,创建 NSMutableAttributedString 实例,并将富文本内容包含在其中。 最后,使用 UILabel、UITextView 或者 UIWebView 等组件来展示富文本内容,并加以控制动态效果的播放周期及重复次数等属性,达到更丰富的用户体验。 需要注意的是,在使用富文本加载 GIF 图片时,应考虑对应用性能及网络耗费等问题进行优化调整,使得应用流畅稳定,并能够节省用户移动数据流量等资源。 ### 回答2: iOS开发中,要加载gif动图作为富文本,可以使用如下的步骤: 1. 引入SDWebImage库:在项目中添加SDWebImage库,这是一个常用的图片加载库,可以方便地加载并显示网络上的图片。 2. 下载gif图片:从网络上找到合适的gif图片,并将其下载到本地。 3. 将gif图片添加到富文本中:使用SDWebImage提供的方法将gif图片添加到富文本中,可以使用`UIImage sd_animatedGIFWithData:`方法将本地的gif图片转换为UIImage对象,并使用该UIImage对象创建一个`NSTextAttachment`对象。 4. 将NSTextAttachment对象添加到NSAttributedString中:将创建好的NSTextAttachment对象添加到NSMutableAttributedString对象中,可以使用`appendAttributedString:`方法将其追加到NSMutableAttributedString对象的末尾。 5. 将NSAttributedString对象显示在界面上:通过UILabel、UITextView等界面控件,将NSMutableAttributedString对象显示在界面上,即可完成富文本加载gif的功能。 需要注意的是,为了保证gif动图的流畅播放,SDWebImage库会将gif图片展示为一系列的静态图片,然后再按照正确的帧率进行播放。另外,对于高性能的gif加载,也可以使用其他优化库,如FLAnimatedImage等。 综上所述,通过引入SDWebImage库、下载gif图片、将图片添加到富文本中,并将富文本显示在界面上等一系列步骤,即可实现在iOS中加载gif动图作为富文本显示的功能。 ### 回答3: 在iOS中,要加载GIF图像并将其显示在富文本中,我们可以采取以下步骤: 1. 首先,我们需要获取GIF图像文件的URL或路径。可以从互联网上下载或从应用程序的资源文件中获取。例如,如果GIF图像保存在应用程序的资源文件中,则可以使用`Bundle.main.url(forResource: "myGif", withExtension: "gif")`方法获取该文件的URL。 2. 接下来,我们需要将GIF图像文件加载到`Data`对象中,以便将其与`NSAttributedString`富文本一起使用。我们可以使用`Data(contentsOf: gifURL)`方法将URL转换为Data对象。 3. 然后,我们可以创建一个`NSTextAttachment`对象,并将GIF图像数据设置为其`image`属性。例如,可以使用`NSTextAttachment(image: UIImage(data: gifData)!)`来创建`NSTextAttachment`对象。 4. 然后,我们可以使用`NSAttributedString`的`append`方法将`NSTextAttachment`对象添加到富文本中,并设置其适当的位置。例如,可以使用`attributedString.append(NSAttributedString(attachment: textAttachment))`语句将`NSTextAttachment`对象添加到`attributedString`富文本字符串中。 5. 最后,我们可以将包含GIF图像的富文本字符串应用于文本视图或标签等UI元素,以便在界面上显示GIF图像。例如,对于UILabel,可以使用`label.attributedText = attributedString`将富文本字符串应用于标签。 综上所述,以上是利用iOS富文本加载GIF的简要步骤。通过将GIF图像文件加载到`Data`对象中,然后将其作为`NSTextAttachment`对象添加到富文本字符串中,我们可以在富文本中显示GIF图像,并将其应用于iOS界面中的相应UI元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值