最近在開發項目時,遇到了圖文混編的需求,之前對於圖文混編也有了解,但是隻是找找代碼,湊合能用。考慮到團隊中後期會經常遇到圖文混編的需求,我這邊參考網上的博客,整理了一下
參考博客地址:
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'
這個庫還有很大的升級優化空間,歡迎大家多多批評指正,多多指教。有好的意見和建議可以私信我
更多技術乾貨文章可以掃描下方二維碼: