Skip to content

Attributes String Editing Factory. 富文本编辑工厂, 让代码更清晰. 简便操作, 让你爽到爆!

License

Notifications You must be signed in to change notification settings

FFFFF0001/SJAttributesFactory

 
 

Repository files navigation

SJAttributesFactory

Use

pod 'SJAttributesFactory'

Example


###上下图文效果: 上下图文.jpg
之前:

   // 文本字典
    NSDictionary *titleDict = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize],
                                NSForegroundColorAttributeName: titleColor};
    NSDictionary *spacingDict = @{NSFontAttributeName: [UIFont systemFontOfSize:spacing]};
    
    // 图片文本
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = image;
    attachment.bounds = CGRectMake(0, 0, imageW, imageH);
    NSAttributedString *imageText = [NSAttributedString attributedStringWithAttachment:attachment];
    
    // 换行文本
    NSAttributedString *lineText = [[NSAttributedString alloc] initWithString:@"\n\n" attributes:spacingDict];
    
    // 按钮文字
    NSAttributedString *text = [[NSAttributedString alloc] initWithString:title attributes:titleDict];
    
    // 合并文字
    NSMutableAttributedString *attM = [[NSMutableAttributedString alloc] initWithAttributedString:imageText];
    [attM appendAttributedString:lineText];
    [attM appendAttributedString:text];

现在:

[SJAttributesFactory alterStr:@"9999" block:^(SJAttributesFactory * _Nonnull worker) {
        worker
        .insertText(@"\n", 0)
        .insertImage([UIImage imageNamed:@"sample2"], CGPointZero, CGSizeMake(50, 50), 0)
        .lineSpacing(8) // 加点行间隔
        .alignment(NSTextAlignmentCenter)
        .font([UIFont boldSystemFontOfSize:14])
        .fontColor([UIColor whiteColor]);
    }];

###左缩进 + 右缩进 左缩进 + 右缩进.jpeg
之前:

    NSString *str = @"故事:可以解释为旧事、旧业、先例、典故等涵义,同时,也是文学体裁的一种,侧重于事情过程的描述,强调情节跌宕起伏,从而阐发道理或者价值观。";
    
    NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:str];
    [attrM addAttribute:NSFontAttributeName
                  value:[UIFont boldSystemFontOfSize:14]
                  range:NSMakeRange(0, 3)];
    NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
    style.firstLineHeadIndent = 8;
    style.headIndent = [[attrM attributedSubstringFromRange:NSMakeRange(0, 3)]
                                       boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)
                                                    options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
     context:nil].size.width + style.firstLineHeadIndent;
    
    style.tailIndent = -8;
    [attrM addAttribute:NSParagraphStyleAttributeName
                  value:style
                  range:NSMakeRange(0, str.length)];

现在:

    [SJAttributesFactory alterStr:@"故事:可以解释为旧事、旧业、先例、典故等涵义,同时,也是文学体裁的一种,侧重于事情过程的描述,强调情节跌宕起伏,从而阐发道理或者价值观。" block:^(SJAttributesFactory * _Nonnull worker) {
        worker.nextFont([UIFont boldSystemFontOfSize:14]).range(NSMakeRange(0, 3));
        CGFloat startW = worker.width(NSMakeRange(0, 3));
    
        worker
        .firstLineHeadIndent(8)
        .headIndent(startW + 8)
        .tailIndent(-8);       
    }];

###下划线 + 删除线 下划线 + 删除线.jpg

之前:

    NSString *price = @"$ 999";
    NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:price];
    NSRange range = NSMakeRange(0, price.length);
    [attrM addAttribute:NSFontAttributeName
                  value:[UIFont systemFontOfSize:40]
                  range:range];
    [attrM addAttribute:NSUnderlineStyleAttributeName
                  value:@(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble)
                  range:range];
    [attrM addAttribute:NSUnderlineColorAttributeName
                  value:[UIColor yellowColor]
                  range:range];
    [attrM addAttribute:NSStrikethroughStyleAttributeName
                  value:@(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble)
                  range:range];
    [attrM addAttribute:NSStrikethroughColorAttributeName
                  value:[UIColor redColor]
                  range:range];

现在:

    [SJAttributesFactory alterStr:@"$ 999" block:^(SJAttributesFactory * _Nonnull worker) {
        worker.font([UIFont systemFontOfSize:40]);
        worker.underline(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble, [UIColor yellowColor]).strikethrough(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble, [UIColor redColor]);
    }];

About

Attributes String Editing Factory. 富文本编辑工厂, 让代码更清晰. 简便操作, 让你爽到爆!

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 85.6%
  • Ruby 14.4%