美文网首页
iOS官方文档 Foundation篇---Strings wi

iOS官方文档 Foundation篇---Strings wi

作者: qianfei | 来源:发表于2019-02-20 14:03 被阅读1次

    世人最能慷慨赠与的,就是建议,但背后目的,未必真称得上慷慨两字。

    ————赮毕波罗

    timg.jpg

    NSAttributedString

    富文本

    
    //创建
    NSAttributedString *string = [[NSAttributedString alloc]initWithString:@"qwerty"];
    NSAttributedString *attriStr = [[NSAttributedString alloc]initWithAttributedString:string];
        
    //带属性创建 
    //阴影
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowBlurRadius = 2; // 模糊度
    shadow.shadowColor = [UIColor cyanColor]; // 阴影颜色
    shadow.shadowOffset = CGSizeMake(5, 5);     // 阴影偏移
    //文本属性
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:kFont(25),NSFontAttributeName ,[UIColor redColor],NSForegroundColorAttributeName,[UIColor lightGrayColor],NSBackgroundColorAttributeName,[NSURL URLWithString:@"www.baidu.com"],NSLinkAttributeName,shad w,NSShadowAttributeName,@1,NSVerticalGlyphFormAttributeName, nil];
    NSAttributedString *attriString = [[NSAttributedString alloc]initWithString:@"qwerty" attributes:dict];
        
    //比较字符串
    BOOL isEqual = [attriStr isEqualToAttributedString:attriString];
    //截取字符串
    NSAttributedString *subStr = [attriString attributedSubstringFromRange:NSMakeRange(0, 1)];
        
    //对属性字符串中特定属性的每个范围执行指定的
    [attriString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, 3) options:NSAttributedStringEnumerationReverse usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
            
    }];
    [attriString enumerateAttributesInRange:NSMakeRange(0, 3) options:NSAttributedStringEnumerationReverse usingBlock:^(NSDictionary<NSAttributedStringKey,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
            
    }];
    
    #NSAttributedStringKey:
    NSFontAttributeName; //字体,value是UIFont对象
    NSParagraphStyleAttributeName;//绘图的风格(居中,换行模式,间距等诸多风格),value是NSParagraphStyle对象
    NSForegroundColorAttributeName;// 文字颜色,value是UIFont对象
    NSBackgroundColorAttributeName;// 背景色,value是UIFont
    NSLigatureAttributeName; //  字符连体,value是NSNumber
    NSKernAttributeName; // 字符间隔
    NSStrikethroughStyleAttributeName;//删除线,value是NSNumber
    NSUnderlineStyleAttributeName;//下划线,value是NSNumber
    NSStrokeColorAttributeName; //描绘边颜色,value是UIColor
    NSStrokeWidthAttributeName; //描边宽度,value是NSNumber
    NSShadowAttributeName; //阴影,value是NSShadow对象
    NSTextEffectAttributeName; //文字效果,value是NSString
    NSAttachmentAttributeName;//附属,value是NSTextAttachment 对象
    NSLinkAttributeName;//链接,value是NSURL or NSString
    NSBaselineOffsetAttributeName;//基础偏移量,value是NSNumber对象
    NSUnderlineColorAttributeName;//下划线颜色,value是UIColor对象
    NSStrikethroughColorAttributeName;//删除线颜色,value是UIColor
    NSObliquenessAttributeName; //字体倾斜
    NSExpansionAttributeName; //字体扁平化
    NSVerticalGlyphFormAttributeName;//垂直或者水平,value是 NSNumber,0表示水平,1垂直
    
    
    #NSMutableParagraphStyle:
    @property(NS_NONATOMIC_IOSONLY) CGFloat lineSpacing; // 行间距
    @property(NS_NONATOMIC_IOSONLY) CGFloat paragraphSpacing; // 段间隙
    @property(NS_NONATOMIC_IOSONLY) NSTextAlignment alignment; // 文本排列 (对齐方式)
    @property(NS_NONATOMIC_IOSONLY) CGFloat firstLineHeadIndent; // 首行缩进
    @property(NS_NONATOMIC_IOSONLY) CGFloat headIndent; // 整体缩进(首行除外)
    @property(NS_NONATOMIC_IOSONLY) CGFloat tailIndent; // 整体缩进(末行除外)
    @property(NS_NONATOMIC_IOSONLY) NSLineBreakMode lineBreakMode; // 换行模式
    @property(NS_NONATOMIC_IOSONLY) CGFloat minimumLineHeight; // 最小行高
    @property(NS_NONATOMIC_IOSONLY) CGFloat maximumLineHeight; // 最大行高
    @property(NS_NONATOMIC_IOSONLY) NSWritingDirection baseWritingDirection; // 文本的书写方向(方向有三个,从左到右,从右到做,从上到下)
    @property(NS_NONATOMIC_IOSONLY) CGFloat lineHeightMultiple;  //行间距倍数
    @property(NS_NONATOMIC_IOSONLY) CGFloat paragraphSpacingBefore; //段首行空白空
    @property(NS_NONATOMIC_IOSONLY) float hyphenationFactor;  // 连字属性 在iOS,唯一支持的值分别为0和1
    @property(null_resettable, copy, NS_NONATOMIC_IOSONLY) NSArray<NSTextTab *> *tabStops NS_AVAILABLE(10_0, 7_0); // 制表符
    
    #NSUnderlineStyle:
    NSUnderlineStyleNone         = 0x00,   // 无样式
    NSUnderlineStyleSingle       = 0x01,   // 下划线(细)
    NSUnderlineStyleThick        = 0x02,   // 下划线(粗)
    NSUnderlineStyleDouble       = 0x09,   // 双下划线
    NSUnderlinePatternSolid      = 0x0000, // 固体
    NSUnderlinePatternDot        = 0x0100, // 圆点
    NSUnderlinePatternDash       = 0x0200, // 破折号
    NSUnderlinePatternDashDot    = 0x0300, // 破折号圆点
    NSUnderlinePatternDashDotDot = 0x0400, // 破折号双圆点
    NSUnderlineByWord            = 0x8000  // 词
    

    NSMutableAttributedString
    可变富文本

    //创建
    NSAttributedString *attriStr = [[NSAttributedString alloc]initWithString:@"QW"];
    NSMutableAttributedString *mulAttriStr = [[NSMutableAttributedString alloc]initWithString:@"qwerty"];
    
    //用普通文本替换指定范围的字符
    [mulAttriStr replaceCharactersInRange:NSMakeRange(0, 1) withString:@"Q"];//Qwerty
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.firstLineHeadIndent = 20;
    style.lineSpacing = 10;
    //添加指定范围的单个属性
    [mulAttriStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, mulAttriStr.length)];
    
    //文本属性
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:kFont(15),NSFontAttributeName, nil];
    
    //添加指定范围的属性集
    [mulAttriStr addAttributes:dict range:NSMakeRange(0, mulAttriStr.length)];
    
    //设置指定范围的富文本属性
    [mulAttriStr setAttributes:dict range:NSMakeRange(0, mulAttriStr.length)];
    
    //删除指定范围富文本属性
    [mulAttriStr removeAttribute:NSParagraphStyleAttributeName range:NSMakeRange(0, mulAttriStr.length)];
    
    //替换指定范围的富文本
    [mulAttriStr replaceCharactersInRange:NSMakeRange(0, mulAttriStr.length-1) withAttributedString:attriStr];
    
    //指定索引插入富文本
    [mulAttriStr insertAttributedString:attriStr atIndex:3];
    
    //删除给定范围富文本
    [mulAttriStr deleteCharactersInRange:NSMakeRange(0, 3)];
    
    //替换全部富文本
    [mulAttriStr setAttributedString:attriStr];
    
    欢迎留言指正,会持续更新!!!

    相关文章

      网友评论

          本文标题:iOS官方文档 Foundation篇---Strings wi

          本文链接:https://www.haomeiwen.com/subject/zaieeqtx.html