SkillAgentSearch skills...

NudeIn

An easy-to-use attributed text view for iOS Apps,use like masonry

Install / Use

/learn @hon-key/NudeIn
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<img src="https://github.com/hon-key/HKAttributedTextView/raw/master/logo.png" width = "100%" />

NudeIn

Build Status Cocoapods Platform Language

NudeIn 是一个基于 UITextView ,书写风格类似于 masonry 的 iOS 端富文本控件,它采用优雅的声明式(链式)方法定义富文本控件,和编程式的不同,它所需的代码量相当短,且非常直观易用。

与此同时,NudeIn 不止于此,它会是一款非常灵性的富文本控件,它会将减少代码冗余提高到极致。比如考虑到一点,富文本里可能会有多于 2 个的风格一致的富文本,也有可能仅仅只是风格部分一致的富文本,比如字体大小不一样,比如颜色不一样。这样的代码如果按照常规去写,可能会出现大量相同的代码段。为了解决这个问题,NudeIn 引入了 模板 ,你可以轻松声明一个模板,应用到任何需要它的组件上,而每个组件甚至可以声明属于自己的属性来覆盖模板上的属性,以达到部分一致的效果。这就是 NudeIn 非常灵活的地方。

相比其他第三方富文本库,NudeIn 将是最符合人类思维方式的,使用它将不会花费你太多的学习成本。如果你有 masonry 经验,你将几乎没有学习成本,如果你没有,也无需担心,它看起来就像是为你的思维方式精心打造的一般,只需稍微看看例子,就可以完全学会使用方法。

NudeIn is an iOS rich text control based on UITextView. It has a writing style similar to masonry. NudeIn uses an elegant declarative (chained) programming to define rich text controls. Unlike programming, it requires a very small amount of code, and it is intuitive and easy to use.

In addition to reducing code redundancy to almost zero, NudeIn is very sensitive and intelligent. If you are writing a code where in the rich text there are more than two rich texts of consistent styles, or two rich texts of partially consistent styles, e.g. of different font sizes or different colors, and if you write it as what we usually do, you may end up having a large number of identical code segments. NudeIn provides a solution. It introduces templates. You can easily declare a template to be applied to any component that needs it. Or each component can even declare its own properties to override the properties on the template to achieve a partially consistent effect. In either way NudeIn is much more flexible than its alternatives.

Compared to other third-party rich text libraries, NudeIn is developed to possess an “intuitive” mind so it is absolutely easy to learn. If you have masonry experience, there is almost no cost of learning. If you don't…No worries! NudeIn is purposely crafted to follow how you think. Just take a look at the guide and you will be all ready to start.

Usage

NudeIn 的用法非常简单明了,这里给出一个非常简单的例子,相信你会被这样的用法惊艳到,一旦用起来就会爱不释手:

1、引入控件

#import "NudeIn.h"

2、声明控件为你的成员变量

@property (nonatomic,strong) NudeIn *attrLabel;

3、Do it yourself

_attrLabel = [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();
}];

3、对声明了 link 属性的部分定义回调


- (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];
        
    }
    
}

结果会是这样:

<img src="https://github.com/hon-key/HKAttributedTextView/raw/master/Screenshots/1.png" width = "50%" />

点击带有 link 属性的部分,将产生回调:

<img src="https://github.com/hon-key/HKAttributedTextView/raw/master/Screenshots/2.png" width = "50%" />

Installation

1、Cocoapods

pod 'NudeIn'

最新 pod 版本:1.2.8

1、新增 asyncMake 和 asyncRemake 方法,现在可以进行异步 make 了。因为 make 任务如果过于繁重,在性能敏感的页面具有一定的影响。如在 tableView 里使用 remake 时,最好使用 asyncRemake 来更新文本。

1.2.7 更新如下:

1、优化了触摸时的逻辑,修复了 makeTemplate 方法在继承时无法独立使用的问题,修复了一个阴影在没有传入 template 时无效的问题

2、makeTemplate 新增了 allText 和 allImage,现在可以对全局进行统一配置

3、新增了一个很酷的功能:自定义方法:

为了让自己的代码更加适应不同工程的开发,能够自定义方法是一件很有意义的事,它不但能够减少代码量,提高复用性,还能让你的NudeIn看起来独一无二,完全属于其所在的工程。这为 NudeIn 提供了非常好的可读性。不过这并不能算是实际意义上的新增,因为它基于 OC 的 Category,所以你完全可以自己通过 Category 去自定义属于自己的方法,这里 NudeIn 只是提供了更加方便,更加好管理的宏定义来实现这个操作。

我们举个例子:

如果你的 app 里有一个属于自己的需求:一种属于该 app 的文字主题色(假如是橙色 Orange ),那么你可能会把这种颜色编辑为宏定义以在工程里大范围重用。但是如果你大范围用到了 NudeIn,使用宏定义的方法可能会稍显麻烦,而如果使用自定义功能,假设你想为该需求定义一个完全属于该工程的方法:themeColor,你将可以得到看起来最为原生的使用体验。

我们首先创建一对cocoa源文件(MyNudeInMethod.h, MyNudeInMethod.m)

在 MyNudeInMethod.h 里添加:

NUDAnounceTextAttributeSet(themeColor);

在 MyNudeInMethod.m 里添加:

NUDMakeTextAttributeSet(themeColor, color([UIColor orangeColor]));

这样我们就完成了对 themeColor 的自定义,我们可以在任何引用了该源文件的源文件里使用它:

nude = [NudeIn make:^(NUDTextMaker *make) {
    make.text(@"text").themeColor().attach();
}];

NudeIn 提供了两种自定义方法的宏,一种就是上面的无参数方法自定义,另一种为单参数方法自定义,该定义方式可在 textExample 里示范。

至于更多参数的自定义方法,只能通过自己实现 Category 的方式去使用,不过为了让一个方法看起来更加易读,添加更多参数其实并不推荐,一个参数我认为基本上是够用的。

最低 iOS 版本: 8.0

2、Copy files

你可以拷贝 master 或者 1.2.7 里 NudeIn 文件夹的所有文件到你的工程里。

master 分支可能包含一些新的功能或者为不稳定版本,如果你在使用过程中遇到问题,欢迎 commit an issue 或者提交 PR。

Indexes

  • Text

    • font 通过大小声明字体,统一使用系统字体

    • fontName 通过字体名以及大小声明字体

    • fontRes 通过 UIFont 声明字体

    • fontStyle 声明字体的风格,如 Bold、Light 等

    • bold 声明字体为 Bold 风格,如果有的话

    • color 声明文字的前景色

    • mark 声明文字的底色

    • hollow 声明文字为镂空

    • solid 声明文字为实心

    • link 声明文字为链接文字

    • _ 声明文字带下划线

    • deprecated 声明文字带删除线

    • skew 声明文字为斜体

    • kern 声明文字的紧凑程度

    • ln 声明文字换行

    • ligature 声明文字为连体字

    • letterpress 声明文字为印刷风格(凸起效果),该属性占用内存较高,谨慎使用

    • vertical 声明文字的垂直偏移

    • stretch 声明文字的水平拉伸程度(产生变形)

    • reverse 声明文字逆序书写

    • shadow 声明文字带默认阴影

    • shadowDirection 声明文字带阴影,并且设定阴影为八个基本方向

    • shadowOffset 声明文字带阴影,并且完全自定义阴影的方向

    • shadowBlur 声明文字带阴影,并自定义阴影的模糊程度

    • shadowColor 声明文字带阴影,并自定义阴影颜色

    • shadowRes 通过 NSShadow 声明并自定义阴影

    • lineSpacing 声明文字的行距

    • lineHeight 声明文字的行高,(最小行高,最大行高,行高倍数)

    • paraSpacing 声明文字每个自然段对其他自然段拉开的点距,(与前自然段拉开的点距,与后自然段拉开的点距)

    • aligment 声明文字的对齐方式,参数为 NUDAlignment

    • indent 声明文字的缩进,(前缩进,后缩进)

    • fl_headIndent 声明文字的首行前缩进,该属性会在首行覆盖 indent 的前缩进属性

    • linebreak 声明文字的断行方式,参数为 NUDLineBreakMode

    • highlight 声明文字的触摸高亮,参数为一个模板的 id

    • tap 声明文字的触摸回调,参数为回调的 target 以及回调方法

  • Image

    • origin 声明图像的偏移,锚点为左下角

    • size 声明图像的大小

    • ln 声明图像换行

    • vertical 声明图像换行

    • aligment 声明图像对齐属性,参数为 NUDAligment

  • Template

    • textTemplate 声明一个 text 模板,以参数 identifier 来标识这个模板以重复使用,其使用方法和 text 一样

    • imageTemplate 声明一个 image 模板,以参数 identifier 来标识这个模板以重复使用,其使用方法和 image 一样

    • allText 声明所有使用 .attach() 的 text 都会被附加的属性,使用 .attachWith(@"") 不受影响

    • allImage 声明所有使用 .attach() 的 image 都会被附加的属性,使用 .attachWith(@"") 不受影响

  • makeTemplate

    • textTemplate 全局声明一个 text 模板,以参数 identifier 来标识这个模板以重复使用,其使用方法和 text 一样

    • imageTemplate 全局声明一个 image 模板,以参数 identifier 来标识这个模板以重复使用,其使用方法和 image 一样

    • allText 全局声明所有使用 .attach() 的 text 都会被附加的属性,使用 .attachWith(@"") 不受影响

    • allImage 全局声明所有使用 .attach() 的 image 都会被附加的属性,使用 .attachWith(@"") 不受影响

Documents

font

font 默认使用系统字体,如果只使用系统字体,它会让你的代码更简洁一些

NudeIn *nude = [NudeIn make:^(NUDTextMaker *make) {
    make.text(@"Github.com").font(32).attach();
}];
<img src="https://github.com/hon-key/HKAttributedTextView/raw/master/Screenshots/font.png" /> <p align="right"><a href="#indexes">back</a></p>

fontName

fontName 使用字体名称来设定字体,字体的名称可以是默认的family名称,也可以是特定粗细的字体名称

NudeIn *nude = [NudeIn make:^(NUDTextMaker *make) {
    make.text(@"Github.com").fontName(@"AmericanTypewriter",32).ln(1).attach();
    make.text(@"Github.com").fontName(@"AmericanTypewriter-Bold",32).attach();
}];
<img src="https://github.com/hon-key/HKAttributedTextView/raw/master/Screenshots/fontName.png" /> <p align="right"><a href="#indexes">back</a></p>

fontRes

fontRes 使用自定义的UIFont来设定字体,这里可以尽你所好

UIFont *font = [UIFont fontWithName:@"AmericanTypewriter" size:32];
UIFont *fontBold = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:32];
NudeIn *nude = [NudeIn make:^(NUDTextMaker *make) {
    make.text(@"Github.com").fontRes(font).ln(1).attach();
    make.text(@"Github.com").fontRes(fontBold).attach();
}];

<img src="https://github.com/hon-key/HKAttributedTextView/raw/master/Screenshots/fontRes.pn

View on GitHub
GitHub Stars461
CategoryDevelopment
Updated3mo ago
Forks73

Languages

Objective-C

Security Score

97/100

Audited on Dec 26, 2025

No findings