{"id":18270908,"url":"https://github.com/hsjunnesson/UIViewDrawRectBlock","last_synced_at":"2025-04-05T01:30:55.158Z","repository":{"id":56925166,"uuid":"3112733","full_name":"hsjunnesson/UIViewDrawRectBlock","owner":"hsjunnesson","description":"A category on UIView to create an instance with a block for its drawRect.","archived":false,"fork":false,"pushed_at":"2013-11-15T09:15:08.000Z","size":120,"stargazers_count":142,"open_issues_count":0,"forks_count":13,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-01T20:07:31.172Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hsjunnesson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-01-05T19:47:15.000Z","updated_at":"2024-06-13T09:41:00.000Z","dependencies_parsed_at":"2022-08-21T06:20:11.820Z","dependency_job_id":null,"html_url":"https://github.com/hsjunnesson/UIViewDrawRectBlock","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/hsjunnesson%2FUIViewDrawRectBlock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hsjunnesson%2FUIViewDrawRectBlock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hsjunnesson%2FUIViewDrawRectBlock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hsjunnesson%2FUIViewDrawRectBlock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hsjunnesson","download_url":"https://codeload.github.com/hsjunnesson/UIViewDrawRectBlock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247276043,"owners_count":20912286,"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:56.814Z","updated_at":"2025-04-05T01:30:54.742Z","avatar_url":"https://github.com/hsjunnesson.png","language":"Objective-C","readme":"# UIView+DrawRectBlock\n\nTired of subclassing UIView just to implement drawRect?\n\nThis is a category on UIView to create an instance with a block for its drawRect.\n\n\n## How do I use this?\n\nSomething like this:\n\n    - (void)viewDidLoad {\n        [self.view addSubview:[UIView viewWithFrame:CGRectMake(20, 40, 60, 60)\n                                      drawRectBlock:^(UIView *drawRectView, CGRect rect) {\n                                          CGContextRef c = UIGraphicsGetCurrentContext();\n                                          CGContextSetFillColorWithColor(c, [UIColor blueColor].CGColor);\n                                          CGContextFillRect(c, rect);\n                                      }]];\n    }\n\nBut, of course, always be careful around blocks. This setup makes it really easy to create a circular reference chain.\nSay you want to access a property on the viewcontroller, like that color, so you do something like this:\n\n    - (void)viewDidLoad {\n        [self.view addSubview:[UIView viewWithFrame:CGRectMake(20, 40, 60, 60)\n                                      drawRectBlock:^(UIView *drawRectView, CGRect rect) {\n                                          CGContextRef c = UIGraphicsGetCurrentContext();\n                                          CGContextSetFillColorWithColor(c, self.fillColor.CGColor);\n                                          CGContextFillRect(c, rect);\n                                      }]];\n    }\n\nNow the block will retain the viewcontroller, the viewcontroller will retain the view and the view will retain the block, which in turn retains the view...\n\nIf you're using iOS 5 and ARC, you can do something like this:\n\n    - (void)viewDidLoad {\n        __weak MyViewController *weakRef = self;\n        [self.view addSubview:[UIView viewWithFrame:CGRectMake(20, 40, 60, 60)\n                                      drawRectBlock:^(UIView *drawRectView, CGRect rect) {\n                                          CGContextRef c = UIGraphicsGetCurrentContext();\n                                          CGContextSetFillColorWithColor(c, weakRef.fillColor.CGColor);\n                                          CGContextFillRect(c, rect);\n                                      }]];\n    }\n\n\n## License\n\nCopyright (C) 2012 Hans Sjunnesson\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies\nof the Software, and to permit persons to whom the Software is furnished to do\nso, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n","funding_links":[],"categories":["etc"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhsjunnesson%2FUIViewDrawRectBlock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhsjunnesson%2FUIViewDrawRectBlock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhsjunnesson%2FUIViewDrawRectBlock/lists"}