图文混排

1.近日公司需要做个图文混排的功能,自己就写了demon,验证完毕,觉得有必要分享给大家,使用简单,相对于其他很多图文混排要简便许多。


2.里面有正则工具RegexkitLite,导入后需要在Build Phases内找到RegexkitLite.m 编辑-fno-objc-arc;在Link Binary With Libraries 导入库libicucore.A.tbd


3.使用代码如下

#import "ViewController.h"
#import "XYMakeAttrbutiStringTool.h"
#import "XYContentTextView.h"
#import "XYAttrWebViewController.h"

@interface ViewController ()
/** 展示控件 */
@property (weak, nonatomic) IBOutlet XYContentTextView *contentView;
/** 富文本工具 */
@property (nonatomic ,strong) XYMakeAttrbutiStringTool *attrTool;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // testString  [抠鼻屎]从bundle的emoji info.plist内拿出
    NSString *testString =@"[抠鼻屎]aoiasdf你好吗?[抠鼻屎]我不好偶@夏明,你的,#小明#读的的的的按视频否爱上,你能做出来吗,你好[得意地笑]哈大的[纠结],http://www.sina.com,一切都好http://www.baidu.com我的元爱的";
    XYMakeAttrbutiStringTool *attrTool =[[XYMakeAttrbutiStringTool alloc] initWithText:testString fontSize:0 lineSpace:16 paragraphSpace:10 textColor:nil highlightedColor:nil calculateRectWithMaxWidth:self.contentView.frame.size.width];
    self.attrTool = attrTool;
    // url
    self.contentView.specialUrlBeenClickedBlock = ^(NSString *url){
        NSLog(@"url:%@",url);
        XYAttrWebViewController *webController = [[XYAttrWebViewController alloc] init];
        webController.url = url;
        [self presentViewController:webController animated:YES completion:nil];
    };
    // #
    self.contentView.specialJingHaoBeenClickedBlock = ^(NSString *jingHao){
        NSLog(@"jingHao:%@",jingHao);
    };
    // @
    self.contentView.specialAtBeenClickedBlock = ^(NSString *at){
        NSLog(@"at:%@",at);
    };
    self.contentView.attributedText = self.attrTool.attributedText;
}

#pragma mark - 重新设置展示控件的frame
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    CGSize size = self.attrTool.attributedSize;
    CGPoint point = self.contentView.frame.origin;
    self.contentView.frame = (CGRect){point,size};
    self.contentView.backgroundColor = [UIColor lightGrayColor];
}

@end

4.github demon如下:

https://github.com/CoderMaDongJun/RichText

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值