zoukankan      html  css  js  c++  java
  • 关于iOS富文本使用

    通过使用富文本进行一条字符串的不同字体,不同字间距,不同颜色的的设置

    下面介绍一些关于设置段落属性相关的key值

    NSParagraphStyleAttributeName           key:设置文本段落,value: NSParagraphStyle 对象

    NSFontAttributeName                          key:字体属性,默认值:value :字号  

    NSForegroundColorAttributeNam          key:字体颜色,value: UIColor对象,默认值为黑色  

    NSBackgroundColorAttributeName       key:字体所在区域背景颜色,value: UIColor对象,默认值为nil(透明色 )

    NSLigatureAttributeName                     key:连体属性,value:NSNumber 对象,0 表示没有连体字符,1 表示使用默认的连体字符  (整数)

    NSKernAttributeName                         key:字符间距,value: NSNumber对象 ,正值间距加宽,负值间距变窄  (整数)

    NSStrikethroughStyleAttributeName      key:删除线,value: NSNumber 对象 (整数)

    NSStrikethroughColorAttributeName      key:删除线颜色,value: UIColor 对象,默认值为黑色  

    NSUnderlineStyleAttributeName           key:下划线,value: NSNumber 对象,枚举常量 (整数)

    NSUnderlineColorAttributeName           key:下划线颜色,value: UIColor 对象,默认值为黑色  

    NSStrokeWidthAttributeName              key:笔画宽度,value: NSNumber 对象,负值填充,正值无效果  (整数)

    NSStrokeColorAttributeName              key:填充部分颜色,value:取值为 UIColor 对象  

    NSExpansionAttributeName               key:文本横向拉伸属性,value: NSNumber ,正横拉伸,负横压缩  (浮点数)

    NSWritingDirectionAttributeName        key:文字书写方向,value:从左向右书写或者从右向左书写  

    NSVerticalGlyphFormAttributeName    key:文字排版方向,value: NSNumber 对象(整数),0 横排,1 竖排  

    NSLinkAttributeName                         key:链接属性,value:URL地址  

    NSAttachmentAttributeName              key:文本附件,value:NSTextAttachment对象(图片混排)

     NSShadowAttributeName                   key:阴影属性,value: NSShadow 对象  

     NSTextEffectAttributeName                key:文本特殊效果,value: NSString 对象 

     NSBaselineOffsetAttributeName          key:基线偏移值,value:NSNumber ,正上负下  (浮点数)

     NSObliquenessAttributeName             key:字形倾斜度,value: NSNumber ,正右负左  (浮点数)

    1.下面是实例代码

    我封装了一个方法可以直接进行调用

    - (void)label:(UILabel *)label AndWithText:(NSString *)text AndFont:(UIFont *)font AndLeftColor:(UIColor *)leftColor AndRightColor:(UIColor *)rightColor andNumber:(long)number AndLeftRangeNummber:(NSInteger)leftNumber{

        //创建富文本

        NSMutableAttributedString * labelString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",text]] ;

        //富文本的字体

        [labelString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, labelString.length)];

        //富文本左边的字体颜色

        [labelString addAttribute:NSForegroundColorAttributeName value:kScriptGrayColor range:NSMakeRange(0, leftNumber)];

        //富文本右边的字体颜色

        [labelString addAttribute:NSForegroundColorAttributeName value:kScriptBlackColor range:NSMakeRange(leftNumber,labelString.length-leftNumber)];

        //设置富文本左边的字间距

        CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);

        [labelString addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,leftNumber-2)];

        CFRelease(num);

        

        [label setAttributedText:labelString];

    }

    2.比如说你想创建一个带有富文本的字符串可以直接进行方法的调用

    [self label:self.hospitalLabel AndWithText:@"医院:123" AndFont:[UIFont fontWithName:kFontPingFangMedium size:kFitFontSize(13)] AndLeftColor:kScriptGrayColor AndRightColor:kScriptBlackColor andNumber:20.0f AndLeftRangeNummber:3];

    3.实现的具体实例图片如下:

    利用段落的属性,有关于设置动态计算行高的时候

    CGSize size = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT)                                                     options:NSStringDrawingUsesLineFragmentOrigin                                                  attributes:@{NSFontAttributeName:font,                                                               NSParagraphStyleAttributeName :style}                                                     context:nil].size;

    NSParagraphStyleAttributeName是设置段落风格

    具体的使用方法

    1.创建段落设置的相关对象

     NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 

    2.字体的行间距

    paragraphStyle.lineSpacing = 10; 

    3.对齐方式

    paragraphStyle.alignment = NSTextAlignmentJustified;

    4.首行缩进

    paragraphStyle.firstLineHeadIndent = 20.0f;

    5.文本的省略方式

    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;

    6.最大行高限制

    paragraphStyle.maximumLineHeight = 30;

    7.最小行高限制

     paragraphStyle.minimumLineHeight = 10;

    8.整体进行缩进

    paragraphStyle.headIndent = 5;

    9.尾部缩进

    paragraphStyle.tailIndent = 5;

    10.段头留白

    paragraphStyle.paragraphSpacingBefore = 10.0f;

    11.书写方向

    paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;

    12.支持0、1

    paragraphStyle.hyphenationFactor = 1;

  • 相关阅读:
    Sum of a Function(区间筛)
    (01背包)输出方案数
    删边求概率
    完全背包输出方案数(dp)
    二分
    Just Arrange the Icons(模拟)
    Balls of Buma(回文串)
    dp思想
    刷题-力扣-190. 颠倒二进制位
    刷题-力扣-173. 二叉搜索树迭代器
  • 原文地址:https://www.cnblogs.com/chaoyueME/p/5846744.html
Copyright © 2011-2022 走看看