NudeIn *textLabel = [NudeIn make:^(NUDTextMaker *make) {
make.text(@"this is a ").font(14).color([UIColor blackColor]).attach();
make.text(@"BlueLink").font(17).color([UIColor blueColor]).link(self,@selector(linkHandler:)).attach();
make.text(@", and this is a ").font(14).color([UIColor blackColor]).attach();
make.text(@"RedLink").font(17).color([UIColor redColor]).link(self,@selector(linkHandler:)).attach();
}];
- (void)linkHandler:(NUDAction *)action {
if ([action isKindOfClass:[NUDLinkAction class]]) {
NUDLinkAction *linkAction = (NUDLinkAction *)action;
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:linkAction.string message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertController animated:YES completion:nil];
}
}
不仅如此,我们发现上面的代码有不少冗余的地方,比如:this is a和, and this is a字体大小一样,BlueLink和RedLink字体大小一样,那我们还可以这么写:
NudeIn *textLabel = [NudeIn make:^(NUDTextMaker *make) {
make.textTemplate(@"regular").font(14).attach();
make.textTemplate(@"link").link(self,@selector(linkHandler:)).font(17).attach();
make.text(@"this is a ").nud_attachWith(@"regular");
make.text(@"BlueLink").color([UIColor blueColor]).nud_attachWith(@"link");
make.text(@", and this is a ").nud_attachWith(@"regular");
make.text(@"RedLink").color([UIColor redColor]).nud_attachWith(@"link");
}];
项目推荐
项目名称: NudeIn
项目地址: https://github.com/hon-key/Nudeln
项目后续更新计划: 计划持续完善,添加更多可靠实用的功能,让它造福 ios 开发者们。
项目描述: - 你是否在为每次想写富文本的时候就要面对原生API那又长又绕的语法而感到无语呢? - 你是否还在为仅仅因为文字中有一个地方要添加一部分颜色就要用一大段代码来写NSAttributedString呢? - 你是否还在为了封装NSAttributedString而让它尽量提高你自己的效率却又为它的通用性不够而感到苦恼呢? - 你当然可以不用 NSAttributedString 而使用多个UILabel来拼凑以达到效果,但是那样做有时候会不会觉得一言难尽? - 现在你无需担心这一切,NudeIn强大到足以代替UILabel,它让你写富文本比写UILabel的普通文本还要简单。如果你用过masonry语法,那么你将深刻感受到NudeIn给你带来的高效,易读,易维护。
推荐理由: 该开源库本是我在日常开发时为了方便自己编写小而杂的富文本而开发的,随着越来越完善,我发现我甚至直接用NudeIn来代替UILabel开发所有的文本标签,可见该库使用上确实有不少强大的地方,所以我意识到我应该将它分享给更多的人。
示例代码:
比如以截图里的效果,使用 NudeIn 你可以这么去定义它:
不仅如此,我们发现上面的代码有不少冗余的地方,比如:
this is a
和, and this is a
字体大小一样,BlueLink
和RedLink
字体大小一样,那我们还可以这么写:这种写法,它降低了我们重复定义相同属性富文本的工作量,在该例子上可能没什么太大必要,但特别应用在一些规模庞大且重复率较高的富文本需求上时,它将发挥出强大的力量。