ios 富文本

本文介绍一些关于富文本的简单运用,分享一下开发中遇到的问题及解决方案;

    NSString *str = [NSString stringWithFormat:@"原价%zd元,折扣价%.0f元,共节省%.0f元",totalPrice,zkPrice,popBetween];
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
    NSString *total = [NSString stringWithFormat:@"%zd",totalPrice];
    NSString *discount = [NSString stringWithFormat:@"%.0f",zkPrice];
    NSString *save = [NSString stringWithFormat:@"%.0f",popBetween];
    
    /*
    //删除线 -  ios 10.3之后不支持 NSStrikethroughStyleAttributeName方法,有NSUnderlineStyle时
    [attrStr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(2, total.length)];
    [attrStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, total.length)];
    */
    
    //删除线 - 兼容ios 10.3以上版本
    [attrStr addAttributes:@{
                             NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle),
                             NSForegroundColorAttributeName:
                                 [UIColor redColor],
                             NSBaselineOffsetAttributeName:
                                 @(0),
                             NSFontAttributeName: [UIFont systemFontOfSize:15]
                             } range:NSMakeRange(2, total.length)];
    
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, total.length)];
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(7 + total.length, discount.length)];
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(12 + total.length + discount.length, save.length)];

    _youhuiLabel.attributedText = attrStr;

富文本设置,下列参考于:http://www.cnblogs.com/goodboy-heyang/p/5143135.html

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 对象
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,813评论 25 708
  • 最近浮躁,毛线都没写,不断有人关注点赞我,必须总结点干货了。 项目上要加载html格式的文本,学习一下富文本相关内...
    简单也好阅读 49,045评论 18 229
  • 虽然对iOS工作有几年了但是没怎么写过文章,一直觉得自己技术还不够,写文章出来有点丢人现眼,但是发现有关iOS富文...
    CharlesYOO阅读 2,498评论 2 11
  • 说说和聊天界面,会要求把链接,@,##等,特殊的字符串能点击,可能它涉及到CoreText层,自己写起来比较麻烦,...
    pengmengli阅读 2,534评论 3 4
  • 漫长的七天工作日终于结束了。为了好好犒劳一下自己,今晚决定去看电影。不知道是不是上课太累了,好像有点咳嗽,喉咙有...
    Eva华阅读 211评论 0 0