iOS开发时图文混编解决方案

最近在开发项目时,遇到了图文混编的需求,之前对于图文混编也有了解,但是只是找找代码,凑合能用。考虑到团队中后期会经常遇到图文混编的需求,我这边参考网上的博客,整理了一下
参考博客地址:
https://blog.csdn.net/sinat_27706697/article/details/46270939
https://blog.csdn.net/sinat_27706697/article/details/46286717?utm_source=blogxgwz4
目前实现的功能有,自定义文字的大小,字体,颜色,行间距,图文混编。如果需要对某句文字单独处理,可以配置json。剩下的走默认config里面的配置。
使用代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
	CTDisplayView *ctView = [[CTDisplayView alloc] initWithFrame:CGRectMake(0, 200, 300, 0)];
        ctView.backgroundColor = [UIColor whiteColor];
        CTFrameParserConfig *config = [[CTFrameParserConfig alloc] init];
        config.width = 300;
        config.fontSize = 30.0f;
        config.lineSpace = 8.0f;
       config.bgColor = [UIColor redColor];

        CoreTextData *data = [CTFrameParser parseArray:[self array] config:config];
        ctView.data = data;
        ctView.frame = CGRectMake(0, 200, 300, data.height);
        [self.view addSubview:ctView];
    
    }


    - (NSArray *)array
    {
        return @[
            @{@"type":@"txt",
              @"content":@"曾经有一份真挚的爱情摆在我面前,我没有珍惜",
              @"bgColor":@"#f1f1f1",//可以不设置
//              @"size":@"16"
//              @"color":@"#000000"
            },
            @{@"type":@"txt",
              @"content":@"知道失去之后才后悔莫及",
              @"size":@"18"
//              @"color":@"#000000"
            },
            @{@"type":@"txt",
              @"content":@"如果上天能给我再来一次的机会的话,我会对那个女孩说三个字:我爱你",
              @"size":@"24"
//              @"color":@"#000000"
            },
            @{@"type":@"txt",
              @"content":@"如果非要在这份爱上加个期限的话:我希望是一万年",
              @"size":@"18"
//              @"color":@"#000000"
            },
            @{
                  @"type":@"img",
                  @"name":@"icon.png",
                  @"width":@"30",
                  @"height":@"20"
            }
        ];
    }

可以使用pod进行集成

pod 'JKCoreTextKit'

这个库还有很大的升级优化空间,欢迎大家多多批评指正,多多指教。有好的意见和建议可以私信我

更多技术干货文章可以扫描下方二维码:
在这里插入图片描述

发布了231 篇原创文章 · 获赞 110 · 访问量 60万+

猜你喜欢

转载自blog.csdn.net/HHL110120/article/details/103041741
今日推荐