{"id":22358899,"url":"https://github.com/rztime/rzcolorful","last_synced_at":"2025-04-07T07:11:50.130Z","repository":{"id":62452363,"uuid":"98318306","full_name":"rztime/RZColorful","owner":"rztime","description":"NSAttributedString富文本的方法集合，以及简单优雅的使用其多种属性","archived":false,"fork":false,"pushed_at":"2025-03-17T09:03:18.000Z","size":2869,"stargazers_count":72,"open_issues_count":7,"forks_count":14,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-31T06:02:57.105Z","etag":null,"topics":["attributedtext","nsattributedstring","uilabel","uitextfield","uitextview"],"latest_commit_sha":null,"homepage":null,"language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rztime.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-25T14:55:28.000Z","updated_at":"2025-03-17T09:03:21.000Z","dependencies_parsed_at":"2025-03-17T07:31:47.465Z","dependency_job_id":"091cc637-986c-463d-80b2-2cdd6feb03b6","html_url":"https://github.com/rztime/RZColorful","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rztime%2FRZColorful","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rztime%2FRZColorful/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rztime%2FRZColorful/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rztime%2FRZColorful/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rztime","download_url":"https://codeload.github.com/rztime/RZColorful/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247608153,"owners_count":20965952,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["attributedtext","nsattributedstring","uilabel","uitextfield","uitextview"],"created_at":"2024-12-04T15:18:15.654Z","updated_at":"2025-04-07T07:11:50.100Z","avatar_url":"https://github.com/rztime.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RZColorful\n\n[![CI Status](https://img.shields.io/travis/rztime/RZColorful.svg?style=flat)](https://travis-ci.org/rztime/RZColorful)\n[![Version](https://img.shields.io/cocoapods/v/RZColorful.svg?style=flat)](https://cocoapods.org/pods/RZColorful)\n[![License](https://img.shields.io/cocoapods/l/RZColorful.svg?style=flat)](https://cocoapods.org/pods/RZColorful)\n[![Platform](https://img.shields.io/cocoapods/p/RZColorful.svg?style=flat)](https://cocoapods.org/pods/RZColorful)\n\n## Example\n\nTo run the example project, clone the repo, and run `pod install` from the Example directory first.\n\n## Requirements\n\n## Installation\n\nRZColorful is available through [CocoaPods](https://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod 'RZColorful'\n```\n\n## Author\n\nrztime, rztime@vip.qq.com QQ群：580839749\n\n## License\n\nRZColorful is available under the MIT license. See the LICENSE file for more info.\n\n\n# RZColorful\niOS NSAttributedString 富文本方法 (图文混排、多样式文本); 文本超行，自动添加\"折叠\"、\"全部\"; markdown 转 html;\n支持UILabel、UITextView、UITextField中可点击带链接的文本\n\n## 更新日志\n[更新日志](https://github.com/rztime/RZColorful/blob/master/UpdateLog.md)\n\nswift版本[RZColorfulSwift](https://github.com/rztime/RZColorfulSwift)\n\nUITextView实现的富文本编辑器[RZRichTextView](https://github.com/rztime/RZRichTextView)\n\n* NSAttributedString 的多样化设置(文字字体、颜色、阴影、段落样式、url、下划线，以及图文混排等等)\n* 添加UITextField、UITextView、UILabel的attributedText的富文本设置。\n* 扩展：添加一个刷新界面时保持文本框焦点的方法 [demo查看](https://github.com/rztime/ContinueFirsterResponder)\n* 富文本方法内容可单独抽出来,在下边这个文件夹中\n```\n#import \"NSAttributedString+RZColorful.h\"\n```\n\n## 关于RZColorful\n* 支持UILabel、UITextView、UITextField的attributedText的设置。\n* 支持获取NSAttributedString中的图片\n* 支持 HTML 与 NSAttributedString互换（支持图片）\n* 支持 markdown 文档转换为html\n* 包含的属性快捷设置：\n    * 段落样式\n    * 阴影\n    * 文本字体、颜色\n    * 文本所在区域对应的背景颜色\n    * 连体字\n    * 字间距\n    * 删除线、下划线，及其线条颜色\n    * 描边，及其颜色\n    * 斜体字\n    * 拉伸\n    * 上下标\n    * 书写方向（从左到右，从右到左）\n    * 通过html源码加载富文本\n    * 通过url添加图片到富文本\n\n## How to use\n* 请在需要使用的地方加上\n\n```objc\n#import \"RZColorful.h\"\n```\n\n* 主要的功能：\n    * RZColorfulConferrer \n        * text                              -- 添加文本\n        * htmlText                          -- 添加html源码\n        * image                             -- 添加图片 \n        * imageByUrl                        -- 添加图片（通过图片的URL添加）\n        * paragraphStyle                    -- 全局的段落样式\n        * shadow                            -- 全局的阴影样式\n        \n    * RZColorfulAttribute           -- 设置文本的所有的属性\n    * RZImageAttachment         -- 设置图片的所有的属性\n\n### 图示说明\n\n\u003cp align=\"center\" \u003e\n    \u003cimg src=\"line.png\" title=\"支持的功能结构\"\u003e\n\u003c/p\u003e\n\n### 讲解\n\n*  `RZColorfulConferrer` 中 `paragraphStyle` 和 `shadow` 属于全局的设置，在block中任何位置都可设置\n* 文字和图片都可设置`paragraphStyle` 和 `shadow`，对齐方式是`paragraphStyle` 的`alignment`，设置后，当前行文字图片的全局样式将被此覆盖，即全局样式无效，不影响其他行。\n\n    \n### 基本的简单使用方法\n```objc\n    [cell.textLabel rz_colorfulConfer:^(RZColorfulConferrer * _Nonnull confer) {\n        confer.paragraphStyle.paragraphSpacing(15); // 设置段落之间的行距\n\n        confer.text(@\"日常用处:(图片+标题+描述)\\n\").font(rzFont(17)).textColor([UIColor redColor]);\n\n        confer.appendImage([UIImage imageNamed:@\"test.jpg\"]).bounds(CGRectMake(0, -2, 15, 15)); // 图片\n        confer.text(@\" 姓        名: \").font(rzFont(15)).textColor(RGBA(151, 151, 151, 11));\n        confer.text(@\"rztime\").font(rzFont(15)).textColor(RGBA(51, 51, 51, 1));\n\n        confer.text(@\"\\n\");\n        confer.appendImage([UIImage imageNamed:@\"test.jpg\"]).bounds(CGRectMake(0, -2, 15, 15));\n        confer.text(@\" 时        间: \").font(rzFont(15)).textColor(RGBA(151, 151, 151, 11));\n        confer.text([NSString stringWithFormat:@\"%@\", [NSDate new]]).font(rzFont(15)).textColor(RGBA(51, 51, 51, 1));\n\n        confer.text(@\"\\n\");\n        confer.appendImage([UIImage imageNamed:@\"test.jpg\"]).bounds(CGRectMake(0, -2, 15, 15));\n        confer.text(@\" 当次消费: \").font(rzFont(15)).textColor(RGBA(151, 151, 151, 11));\n        confer.text(@\"￥\").font(rzFont(15)).textColor(RGBA(51, 51, 51, 1));\n        confer.text(@\"100\").font(rzFont(15)).textColor(RGBA(251, 51, 51, 1));\n        confer.text(@\"元\").font(rzFont(15)).textColor(RGBA(51, 51, 51, 1));\n    }];\n```\n效果如下\n\u003cp align=\"center\" \u003e\n    \u003cimg src=\"image_1.jpg\" title=\"日常列表中常用展示方法\"\u003e\n\u003c/p\u003e\n\n### 段落样式、阴影（局部与全局统一的区别）\n如果设置有局部样式，则全局样式无效\n```objc\n    [cell.textLabel rz_colorfulConfer:^(RZColorfulConferrer * _Nonnull confer) {\n        confer.paragraphStyle.lineSpacing(5).paragraphSpacingBefore(5).alignment(NSTextAlignmentCenter); // 段落全局样式\n        confer.shadow.color(RGBA(255, 0, 0, 0.3)).offset(CGSizeMake(1, 1));   // 阴影全局\n        \n        // 此部分显示全局样式的风格 （红色阴影，居中对齐，段落行距等）\n        confer.appendImage([UIImage imageNamed:@\"test.jpg\"]).bounds(CGRectMake(0, -2, 15, 15));\n        confer.text(@\" 姓名: \").font(rzFont(15)).textColor(RGBA(151, 151, 151, 11));\n        confer.text(@\"rztime\").font(rzFont(15)).textColor(RGBA(51, 51, 51, 1));\n        \n        confer.text(@\"\\n\");\n        confer.appendImage([UIImage imageNamed:@\"test.jpg\"]).bounds(CGRectMake(0, -2, 15, 15));\n        confer.text(@\" 时间: \").font(rzFont(15)).textColor(RGBA(151, 151, 151, 11));\n        confer.text([NSString stringWithFormat:@\"%@\", [NSDate new]]).font(rzFont(15)).textColor(RGBA(51, 51, 51, 1));\n        \n        // 此部分显示全局样式的风格 （居中对齐，段落行距等）  阴影将被局部覆盖（灰色）\n        confer.text(@\"\\n地址: \").font(rzFont(15)).textColor(RGBA(151, 151, 151, 11)).paragraphStyle.paragraphSpacingBefore(20).and.shadow.color(GRAY(151)).offset(CGSizeMake(3, 3));;\n        confer.text(@\"成都-软件园\").font(rzFont(15)).textColor(RGBA(51, 51, 51, 1)).shadow.color(GRAY(151)).offset(CGSizeMake(3, 3));\n        \n        // 此部分段落样式被局部覆盖  阴影显示全局的\n        confer.text(@\"\\n爱好: \").font(rzFont(15)).textColor(RGBA(151, 151, 151, 11)).paragraphStyle.paragraphSpacingBefore(20);\n        confer.text(@\"游山、\").font(rzFont(15)).textColor(RGBA(151, 51, 51, 1));\n        confer.text(@\"玩水、\").font(rzFont(10)).textColor(RGBA(51, 151, 51, 1));\n        confer.text(@\"听歌、\").font(rzFont(18)).textColor(RGBA(51, 51, 151, 1));\n        confer.text(@\"美食、\").font(rzFont(17)).textColor(RGBA(51, 151, 51, 1));\n        confer.text(@\"看电影、\").font(rzFont(16)).textColor(RGBA(151, 51, 51, 1));\n        confer.text(@\"撸代码、\").font(rzFont(15)).textColor(RGBA(51, 151, 51, 1));\n        confer.text(@\"等等\\n\\n\").font(rzFont(15)).textColor(RGBA(251, 51, 51, 1));\n    }];\n```\n效果如下\n\u003cp align=\"center\" \u003e\n    \u003cimg src=\"image_2.png\" title=\"日常列表中常用展示方法\"\u003e\n\u003c/p\u003e\n\n通过url加载图片\n```objc\n    [cell.textLabel rz_colorfulConfer:^(RZColorfulConferrer * _Nonnull confer) {\n        confer.appendImageByUrl(@\"http://pic28.photophoto.cn/20130830/0005018667531249_b.jpg\").bounds(CGRectMake(0, 0, 200, 0)).paragraphStyle.alignment(NSTextAlignmentLeft); // 宽或高为0时，即自动宽/高按照图片比例来\n    }];\n```\n通过html源码加载文本\n```objc\n[cell.textLabel rz_colorfulConfer:^(RZColorfulConferrer * _Nonnull confer) {\n    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];\n    NSString *filePath =[resourcePath stringByAppendingPathComponent:@\"test.html\"];\n    NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];\n    confer.htmlText(htmlstring);\n}];\n```\n\n给UILabel添加富文本可点击功能  给文本添加tapActionByLable属性\n```objc \n[_label rz_colorfulConfer:^(RZColorfulConferrer * _Nonnull confer) {\n    confer.text(@\"可点击的：\").font([UIFont systemFontOfSize:17]).textColor(UIColor.blackColor);\n    confer.text(@\"文本\").font([UIFont systemFontOfSize:17]).textColor(UIColor.redColor).tapActionByLable(@\"1\");\n}];\n[_label rz_tapAction:^(UILabel * _Nonnull label, NSString * _Nonnull tapActionId, NSRange range) {\n    NSLog(@\"%@\", tapActionId); // print: 1\n}];\n```\n\n给UILabel添加超行之后的折叠、展开功能\n```objc\nUILabel\n\n/// 设置富文本（超过行数后，自动追加“展开” “收起”）\n/// @param attr 原文\n/// @param line 最大显示行数\n/// @param width 最大显示宽度，这个宽度用于计算文本行\n/// @param fold 当前是否折叠\n/// @param allText 超过了行数之后，折叠状态显示的文本 如”展开“  需要给文本设置NSTapActionByLabel属性  (tapActionByLable)\n/// @param foldText 超过行数之后，全部展开状态显示的文本  如”收起“  需要给文本设置NSTapActionByLabel属性 (tapActionByLable)\n- (void)rz_setAttributedString:(NSAttributedString * _Nullable)attr maxLine:(NSInteger)line maxWidth:(CGFloat)width isFold:(BOOL)fold showAllText:(NSAttributedString *_Nullable)allText showFoldText:(NSAttributedString *_Nullable)foldText;\n```\n\n# 备注：\n* 多种属性使用名请参考对应的文件。\n* UILabel、UITextFile是同样的使用方法。\n* 在UILabel、UITextFiled上url点击方法无效。\n* 在UITextView中若要设置文本和图片的点击事件（即对文本和图片设置附带URL的属性），请先设置其editable = NO, 并实现代理。\n    * 设置了URL属性的文本，内部自带蓝色和下划线，若要去掉，可设置 `textView.linkTextAttributes = @{};`\n\n```objc\n// 实现下列方法 （二选一、或都可以实现，当都返回YES时，可能会打开safari浏览器）\ntextView.rzDidTapTextView = ^BOOL(id  _Nullable tapObj) {\n    NSString *url = tapObj;\n    if ([tapObj isKindOfClass:[NSURL class]]) {\n        url = [(NSURL *)tapObj absoluteString];\n    }\n    url = url.rz_decodedString;\n            \n    NSLog(@\"rzDidTapTextView：tapObj:%@  \\n如果url中包含了有中文,URL将会进行编码，所以请rz_decodedString解码之后查看:%@\", tapObj, url);\n    return NO;\n};\n\n- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {\n    NSLog(@\"URL:%@\", URL);\n    // return NO;则不跳转，这里可以做一些基本判断在执行是否跳转浏览器打开url\n    return YES; \n}\n```\n\n## 注意\n\n* 尽管我已经在代码中已经处理过（弱）引用问题，但是在实际运用写入text时，还是请尽量检查避免循环引用\n\n\n## 最后\n* 在使用过程中，如果您发现有什么问题，欢迎向我反馈，谢谢\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frztime%2Frzcolorful","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frztime%2Frzcolorful","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frztime%2Frzcolorful/lists"}