{"id":18270150,"url":"https://github.com/giladno/UICoreTextView","last_synced_at":"2025-04-04T23:31:46.077Z","repository":{"id":6422698,"uuid":"7661222","full_name":"giladno/UICoreTextView","owner":"giladno","description":"Easy to use UIView which supports rendering using CoreText","archived":false,"fork":false,"pushed_at":"2014-07-26T19:18:38.000Z","size":560,"stargazers_count":209,"open_issues_count":4,"forks_count":37,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-11-05T11:52:24.125Z","etag":null,"topics":[],"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":"zlib","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/giladno.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":"2013-01-17T06:40:18.000Z","updated_at":"2023-11-22T23:49:40.000Z","dependencies_parsed_at":"2022-08-06T19:15:33.065Z","dependency_job_id":null,"html_url":"https://github.com/giladno/UICoreTextView","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giladno%2FUICoreTextView","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giladno%2FUICoreTextView/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giladno%2FUICoreTextView/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giladno%2FUICoreTextView/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/giladno","download_url":"https://codeload.github.com/giladno/UICoreTextView/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247266476,"owners_count":20910831,"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":[],"created_at":"2024-11-05T11:38:20.638Z","updated_at":"2025-04-04T23:31:45.058Z","avatar_url":"https://github.com/giladno.png","language":"Objective-C++","readme":"CoreTextView\n==============\n\n\u003cimg src='https://raw.github.com/giladno/UICoreTextView/gh-pages/images/screenshot.png' /\u003e\n\n# Overview\niOS controls (such as [UILabel](http://developer.apple.com/library/ios/#documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/occ/instp/UILabel/attributedText) and [UITextView](http://developer.apple.com/library/ios/#documentation/uikit/reference/uitextview_class/Reference/UITextView.html#//apple_ref/occ/instp/UITextView/attributedText)) already support NSAttributedString, but CoreTextView offers much more than simple styling.\nIf your app needs to render both text \u0026 images, or have some custom rendering on the fly - then CoreTextView is for you.\n\nThere is another great core text library by Oliver Drobnik: \u003ca href='https://github.com/Cocoanetics/DTCoreText'\u003eDTCoreText\u003c/a\u003e. My goal was to create a very tiny and easy to use component (2 files only!) which is meant for simple tasks. If you really need full control of your output, I suggest to take a look at DTCoreText.\n\nCoreTextView contains 2 major components:\n\n* \u003cb\u003eCoreTextView\u003c/b\u003e - UIView based, used to render the string\n* \u003cb\u003eHTMLParser\u003c/b\u003e - HTML parser which generates an instance of NSAttributedString. There is also a category for NSAttributedString, for easy creation of NSAttributedString objects.\n\nExamples\n-\n\nPlease also refer to the demo project for a working demo.\n\n#### Basic styling\n``` objective-c\nm_coreText.attributedString=[NSAttributedString attributedStringWithHTML:@\"\u003cspan font='Papyrus' size='12' color='rgba(255,0,255,0.5)'\u003eThis is a styled string\u003c/span\u003e\"];\n```\n\n#### Custom Rendering\n###### 50x50 Blue circle renderer\n``` objective-c\n@interface BlueCircle : NSObject\u003cHTMLRenderer\u003e\n@end\n\n@implementation BlueCircle\n-(CGSize)size\n{\n\treturn CGSizeMake(50, 50);\n}\n-(void)renderInContext:(CGContextRef)context rect:(CGRect)rect\n{\n\tCGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);\n\tCGContextFillEllipseInRect(context, rect);\n}\n@end\n```\n###### Using the renderer callback\n``` objective-c\nNSString* html=@\"DIV elements generate custom renderers: \u003cdiv /\u003e\";\nm_coreText.attributedString=[NSAttributedString attributedStringWithHTML:html renderer:^id\u003cHTMLRenderer\u003e(NSMutableDictionary* attributes)\n{\n\treturn [[BlueCircle alloc] init];\n}];\n```\n\nStyling\n-\n\nSince CoreTextView was designed mainly to work with custom renderers - passing attributes from the HTML to the callback should be as simple as possible. For that reason, the syntax is based on plain old HTML tags and not modern CSS. The callback receives an instance of NSMutableAttributes, which contains all available attributes of that HTML node.\n\nThe syntax was meant to be as simple as possible and at no point was it designed to follow HTML standards. For that reason, some of the HTML tags/attributes might differ from the original specs (for example, \u0026lt;s\u0026gt; for stroke rather than \u0026lt;stroke\u0026gt;)\n\n### Text\n``` html\n\u003cspan font='ArialMT|Georgia|noorehira|...' size='16'\u003eFont manipulation, we can change font name and size\u003c/font\u003e\n\n\u003cb\u003eBold\u003e\n\u003cu style='none|single|thick|double'\u003eUnderline\u003c/u\u003e\n\u003ci\u003eItalic\u003c/i\u003e\n\u003cs width='3' color='rgb(255,0,0)'\u003eRed stroke\u003c/s\u003e\n\n\u003cspan align='natural|left|right|center|justified'\u003eAligned text\u003c/span\u003e\n\u003cspan direction='rtl|ltr'\u003eRTL or LTR text\u003c/span\u003e\n\u003cspan wrap='word|break-word|clip|ellipsis-head|ellipsis-tail|ellipsis-middle'\u003eWrapped text\u003c/span\u003e\n```\n\n### Colors\n``` html\n\u003cspan color='#FF0000'\u003eStandard HTML colors\u003c/span\u003e\n\u003cspan color='rgb(255,0,0)'\u003eRGB color\u003c/span\u003e\n\u003cspan color='rgba(255,0,0,0.5)'\u003eRGBA color\u003c/span\u003e\n```\n\n### Links\nBy default, all links will render using a blue color and a single underline (which you can override using the \u003ccode\u003ecolor\u003c/code\u003e tag and an embedded \u003ccode\u003eu\u003c/code\u003e tag).\nCoreTextView will try by default to open any link using \u003ccode\u003e[[UIApplication sharedApplication] openURL:url]\u003c/code\u003e. You can prevent this behaviour by returning YES from \u003ccode\u003e-(BOOL)coreTextView:(CoreTextView*)view openURL:(NSURL*)url\u003c/code\u003e delegate.\n\n``` html\n\u003ca href='http://www.google.com/'\u003eGoogle.com\u003c/a\u003e\n\u003ca href='tel:+180012345678'\u003eClick to call\u003c/a\u003e\n```\n\n### Images\nImages can be embedded using the \u003cb\u003ebase64:\u003c/b\u003e prefix or be loaded from disk. Images will be loaded using \u003ccode\u003e[UIImage imageNamed:src]\u003c/code\u003e - unless the \u003cb\u003efile://\u003c/b\u003e scheme is specified.\nFor images, you can also use the \u003ccode\u003evalign='middle'\u003c/code\u003e attribute to center them vertically.\n\n``` html\n\u003cimg src='base64:iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' /\u003e\n\u003cimg width='50' height='50' src='avatar.png' /\u003e\n\u003cimg src='file://..../house.jpg' valign='middle' /\u003e\n```\n\n### Others\n``` html\n\u003cbr\u003e\n\u003chr height='5' color='#FF0000' /\u003e\n```\n\nCustom Renderer\n-\n\nSometimes your app needs to render some content dynamically. One option would be to generate an image and create a new NSAttributedString every time - or you can use a custom renderer.\n\nCustom renderers use a callback, passing a graphic context to draw into. An app can then use it to draw whatever it needs.\n\nTo create a custom renderer, simply use a \u003ccode\u003ediv\u003c/code\u003e tag inside your HTML. You can also set custom attributes which will be passed to the renderer factory. Custom attributes allow you to distinguish between different renderers in the same HTML.\n\nTo be able to create a custom renderer, you'll need to pass a callback to the HTML parser:\n\n``` objective-c\nNSString* html=@\"\u003cdiv type='square' /\u003e\u003cdiv type='circle' /\u003e\";\nm_coreText.attributedString=[NSAttributedString attributedStringWithHTML:html renderer:^id\u003cHTMLRenderer\u003e(NSMutableDictionary* attributes)\n{\n\tif ([attributes[@\"type\"] isEqualToString:@\"circle\"])\n\t{\n\t...\n\t}\n\treturn m_customRenderer;\n}];\n```\n\nA custom renderer can be any object defining the \u003ccode\u003eHTMLRenderer\u003c/code\u003e protocol:\n\n``` objective-c\n@protocol HTMLRenderer\u003cNSObject\u003e\n@required\n@property(nonatomic,readonly) CGSize size;\n@optional\n@property(nonatomic,readonly) CGFloat ascent;\n@property(nonatomic,readonly) CGFloat descent;\n\n@required\n-(void)renderInContext:(CGContextRef)context rect:(CGRect)rect;\n@end\n```\n\nThe only 2 required methods are \u003ccode\u003e-(CGSize\u003esize\u003c/code\u003e and \u003ccode\u003e-(void)renderInContext:(CGContextRef)context rect:(CGRect)rect\u003c/code\u003e, so if we want to draw a simple blue circle, our renderer will be similar to the following implementation:\n\n``` objective-c\n@interface BlueCircle : NSObject\u003cHTMLRenderer\u003e\n@end\n@implementation BlueCircle\n-(CGSize)size\n{\n\treturn CGSizeMake(50, 50);\t// make our circle 50x50 points\n}\n-(void)renderInContext:(CGContextRef)context rect:(CGRect)rect\n{\n\tCGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);\n\tCGContextFillEllipseInRect(context, rect);\n}\n@end\n```\n\nWe then use the parser's factory callback to create a new instance of \u003ccode\u003eBlueCircle\u003c/code\u003e. The new renderer will be retained by the resulting NSAttributedString.\n\n``` objective-c\nm_coreText.attributedString=[NSAttributedString attributedStringWithHTML:@\"Hello \u003cdiv /\u003e World\" renderer:^id\u003cHTMLRenderer\u003e(NSMutableDictionary* attributes)\n{\n\treturn [[BlueCircle alloc] init];\n}];\n```\n\n# Setup\n\nEverything is contained in 2 files only: CoreTextView.mm \u0026 CoreTextView.h.\n\nCoreTextView uses ARC. If your project does not use ARC, you'll need to set the following flag for CoreTextView.mm: \u003ccode\u003e-fobjc-arc\u003c/code\u003e (\u003ca href='http://stackoverflow.com/questions/10523816/how-to-enable-arc-for-a-single-file'\u003eHow to enable ARC for a single file\u003c/a\u003e)\n\nYou'll also need to include \u003ccode\u003elibxml2.dylib\u003c/code\u003e in your project:\n\n* Link your project against \u003ccode\u003elibxml2.dylib\u003c/code\u003e and \u003ccode\u003eCoreText.framework\u003c/code\u003e\n* Under your build settings, add the following path under \u003cb\u003e\"Header Search Paths\"\u003c/b\u003e: \u003ccode\u003e/usr/include/libxml2\u003c/code\u003e\n\n# Credits\n\nCoreTextView was created by \u003ca href='https://github.com/giladno'\u003eGilad Novik\u003c/a\u003e\n\nMany thanks for Oliver Drobnik and his amazing work with \u003ca href='https://github.com/Cocoanetics/DTCoreText'\u003eDTCoreText\u003c/a\u003e.\n\n# License\n\nCoreTextView is licensed under \u003ca href='http://opensource.org/licenses/zlib-license.php'\u003ezlib\u003c/a\u003e license:\n\n    Copyright (c) 2013 Gilad Novik\n\n    This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.\n\n    Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:\n\n    1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.\n\n    2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\n\n    3. This notice may not be removed or altered from any source distribution.\n\nYou do not have to mention CoreTextView in your app, but I'll appreciate if you do so anyway (or at least email me to let me know about your new great app :-)  )\n\n# Usage\n\nUse it, fork it, push updates - enjoy it!\n","funding_links":[],"categories":["etc"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiladno%2FUICoreTextView","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgiladno%2FUICoreTextView","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiladno%2FUICoreTextView/lists"}