{"id":29030937,"url":"https://github.com/hardman/awrichtext","last_synced_at":"2025-06-26T10:02:08.418Z","repository":{"id":56901269,"uuid":"116231592","full_name":"hardman/AWRichText","owner":"hardman","description":"基于CoreText，面向对象，极简，易用，高效，并不仅仅局限于图文混排的富文本排版神器","archived":false,"fork":false,"pushed_at":"2018-08-01T09:57:55.000Z","size":560,"stargazers_count":231,"open_issues_count":8,"forks_count":35,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-06-26T10:01:40.899Z","etag":null,"topics":["ios","ios-app","objective-c","richtext"],"latest_commit_sha":null,"homepage":"","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/hardman.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}},"created_at":"2018-01-04T07:54:07.000Z","updated_at":"2025-01-08T16:08:06.000Z","dependencies_parsed_at":"2022-08-20T18:50:36.100Z","dependency_job_id":null,"html_url":"https://github.com/hardman/AWRichText","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/hardman/AWRichText","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hardman%2FAWRichText","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hardman%2FAWRichText/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hardman%2FAWRichText/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hardman%2FAWRichText/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hardman","download_url":"https://codeload.github.com/hardman/AWRichText/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hardman%2FAWRichText/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262044390,"owners_count":23249744,"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":["ios","ios-app","objective-c","richtext"],"created_at":"2025-06-26T10:00:57.798Z","updated_at":"2025-06-26T10:02:08.408Z","avatar_url":"https://github.com/hardman.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"AWRichText\n--\n\n\u003e 基于CoreText，面向对象，极简，易用，高效，支持精确点击，UIView混排，GIF动图，并不仅仅局限于图文混排的富文本排版神器。\n\u003e\n\u003e 代码地址：https://github.com/hardman/AWRichText  -- 喜欢的同学可以给个star。\n\u003e\n\u003e 接下来会在blog中更新一些具体实现细节。\n\u003e\n\n简述\n--\n很多app中都有聊天功能，图文混排也是常见的需求。\n\niOS原生类：NSAttributedString 就是支持图文混排的。很多应用会用它来实现自己的功能。\n\n但是直接使用它会有很多不方便的地方，大概有下面几个：\n\n1. 太难用，属性那么多，而且使用字典构造，每次用都要查一下文档。更不要说大规模使用了\n2. 不支持GIF动图\n3. 不支持局部精确点击\n4. 不支持UIView与文字进行混排\n\nAWRichText完全解决了这些问题，它的特点如下：\n\n1. 基于 NSAttributedString+CoreText 绘制\n2. 面向对象+链式操作，不需记忆繁多的属性名即可快速生成各种文本效果\n3. 支持GIF 及 任意UIView 的图文混排\n4. 支持精确点击\n\nAWRichText是可以让你在项目中大规模使用的，并不仅仅局限于图文混排的工具。\n\n它适合于如下场景：文字+文字，图片+文字，组件（UIView及其子类）+文字。\n\n因此可出现在：**文档排版**，**聊天排版**，**列表展示**，**广告文案** 等各个位置。\n\n功能演示\n--\n![AWRichText演示](http://upload-images.jianshu.io/upload_images/1334370-0cbdba18bf38519e.gif?imageMogr2/auto-orient/strip)\n\n图中的Demo（直接下载github代码运行即可）包含4部分：\n\n* 第一部分展示了长文本图文混排功能。展示了文字样式，UIView(一个无处安放的按钮)混排，精确点击（蓝紫色字体），GIF动图（小龙）。\n* 第二部分展示了富文本的更多使用方式。可以在任意头像+昵称这种列表中使用，省去动态建立UIImageView和UILabel的麻烦。\n* 第三部分展示了一个简单的的仿斗鱼聊天功能。展示了如何创建复杂的富文本及获取富文本尺寸等功能。\n* 第四部分展示了纯UIView单行排版功能：对于按钮横排的需求很实用，另外点击\"打开DebugFrame\"按钮，会触发线框模式，能够看到每个文字的位置和大小。\n\nDemo中所有元素都是使用AWRichText构造的。\n\n使用方法\n--\n\n1.直接引入文件\n--\n将代码中的 \"RichText\" 文件夹直接拖入你的工程。\n引入头文件 \"AWRichText.h\" 即可使用。\n\n2.使用pod\n--\n在Podfile文件中加入：\n\n```\nsource 'https://github.com/CocoaPods/Specs.git'\nplatform :ios, '8.0'\n\ntarget 'TargetName' do\npod 'AWRichText', '~\u003e 1.0.1'\nend\n```\n然后命令行执行如下命令：\n\n```\npod install\n```\n\n基本用法\n--\n\n```Objective-C\n#include \"AWRichText.h\"\n\n...\n...\n\nAWRichText *richText = [[AWRichText alloc] init];\n\n//创建红色文本hello，文本类型 text和font是必须设置的。\nAWRTTextComponent *rTextComp = [[AWRTTextComponent alloc] init]\n.AWText(@\"hello\")\n.AWColor([UIColor redColor])\n.AWFont([UIFont systemFontOfSize:12])\n.AWPaddingRight(@1);\n[richText addComponent: rTextComp];\n\n//创建蓝色文本world\nAWRTTextComponent *bTextComp = [[AWRTTextComponent alloc] init]\n.AWText(@\" world\")\n.AWColor([UIColor blueColor ])\n.AWFont([UIFont systemFontOfSize:12])\n.AWPaddingRight(@1);\n[richText addComponent:bTextComp];\n\n//创建图片，图片类型也请设置font，否则可能显示异常\nAWRTImageComponent *imgComp = [[AWRTImageComponent alloc] init]\n.AWImage([UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@\"fengtimo.jpg\" ofType:nil]])\n.AWFont([UIFont systemFontOfSize:12])\n.AWBoundsDepend(@(AWRTAttchmentBoundsDependFont))\n.AWAlignment(@(AWRTAttachmentAlignCenter))\n.AWPaddingRight(@1);\n[richText addComponent:imgComp];\n\n//创建UIButton\nUIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 120, 22)];\nbtn.titleLabel.font = [UIFont systemFontOfSize:12];\n[btn setTitle:@\"这是一个button\" forState:UIControlStateNormal];\n[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];\n[btn setTitleColor:[UIColor colorWithRed:170.f/255 green:170.f/255 blue:170.f/255 alpha:1] forState:UIControlStateHighlighted];\n[btn setBackgroundColor:[UIColor colorWithRed:1 green:184.f/255 blue:0 alpha:1]];\nbtn.layer.cornerRadius = 5;\nbtn.layer.borderWidth = 1/[UIScreen mainScreen].scale;\nbtn.layer.borderColor = [UIColor colorWithRed:201.f/255 green:201.f/255 blue:201.f/255 alpha:1].CGColor;\n\n//根据button创建ViewComponent，View类型也请设置font，否则可能显示异常\n//另外 AWRTxxxComponent组件也可以从Pool中取，直接调用addComponentFromPoolWithType:方法。\n//此种方法适合AWRichText的components变化频繁的情况。\n//正常情况使用 alloc init的方式生成即可。\n((AWRTViewComponent *)[richText addComponentFromPoolWithType:AWRTComponentTypeView])\n.AWView(btn)\n.AWFont([UIFont systemFontOfSize:12])\n.AWBoundsDepend(@(AWRTAttchmentBoundsDependContent))\n.AWAlignment(@(AWRTAttachmentAlignCenter));\n\n//创建label，AWRichTextLabel是UILabel的子类\nAWRichTextLabel *awLabel = [richText createRichTextLabel];\n//请务必设置rtFrame属性，设置后会自动计算frame的尺寸\n//宽度为非0，高度为0表示高度自适应。另外若宽度设置特别大，超出文字内容，最终生成的宽度仍然是以文字内容宽度为准。\n//宽度为0表示单行。\n//系统属性numberOfLines无效\nawLabel.rtFrame = CGRectMake(100, 100, 100, 0);\n\nawLabel.backgroundColor = [UIColor colorWithRed:0.9f green:0.9f blue:0.9f alpha:1];\n\n[superView addSubview:awLabel];\n...\n...\n\n```\n\n上述代码效果：\n\n1 . 当rtFrame为 CGRectMake(100,100,100,0)时：\n\n![宽度为100](http://upload-images.jianshu.io/upload_images/1334370-719d4960d69c6173.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)\n\n2 . 当rtFrame为 CGRectMake(100,100,1000,0)时：\n\n![宽度为1000](http://upload-images.jianshu.io/upload_images/1334370-8fca69d2fdcd4035.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)\n\n其他用法及效果实现，详见github中的demo。\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhardman%2Fawrichtext","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhardman%2Fawrichtext","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhardman%2Fawrichtext/lists"}