521xueweihan / HelloGitHub

:octocat: 分享 GitHub 上有趣、入门级的开源项目。Share interesting, entry-level open source projects on GitHub.
https://hellogithub.com
90.2k stars 9.55k forks source link

NudeIn:一款强大的iOS富文本编程工具 #334

Closed hon-key closed 5 years ago

hon-key commented 5 years ago

项目推荐

比如以截图里的效果,使用 NudeIn 你可以这么去定义它:


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字体大小一样,BlueLinkRedLink字体大小一样,那我们还可以这么写:


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");
}];

这种写法,它降低了我们重复定义相同属性富文本的工作量,在该例子上可能没什么太大必要,但特别应用在一些规模庞大且重复率较高的富文本需求上时,它将发挥出强大的力量。

521xueweihan commented 5 years ago

@hon-key 独乐乐不如众乐乐,感谢为开源社区贡献力量!👍

521xueweihan commented 5 years ago

@hon-key 您推荐的项目,已成功发布HelloGitHub 第 33 期,并把您添加到了贡献者列表中。

欢迎继续推荐如此优秀的项目、告诉其他小伙伴加入到 HelloGitHub 项目中。谢谢 🙏