├── .gitignore ├── CustomView.md ├── CustomView.podspec ├── CustomViewKit ├── AutoLayoutExtension.swift ├── CustomViewKit.h ├── CustomViewLayer.swift ├── CustomViewLayerStyle.swift ├── CustomViewLight.swift ├── CustomViewSource.swift ├── Info.plist ├── LICENSE ├── Reflect.swift ├── StringExtension.swift ├── ViewSources.swift └── ViewStyle.swift ├── CustomViewKitExample ├── PodTest.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata ├── PodTest.xcworkspace │ └── contents.xcworkspacedata ├── PodTest │ ├── AppDelegate.swift │ ├── Base.lproj │ │ ├── LaunchScreen.xib │ │ └── Main.storyboard │ ├── CV1.swift │ ├── CV2.swift │ ├── CV2.xib │ ├── CustomViewKitExtension.swift │ ├── Images.xcassets │ │ └── AppIcon.appiconset │ │ │ └── Contents.json │ ├── Info.plist │ └── ViewController.swift ├── PodTestTests │ ├── Info.plist │ └── PodTestTests.swift ├── Podfile ├── Podfile.lock └── Pods │ ├── CustomView │ ├── CustomViewKit │ │ ├── AutoLayoutExtension.swift │ │ ├── CustomViewLayer.swift │ │ ├── CustomViewLayerStyle.swift │ │ ├── CustomViewLight.swift │ │ ├── CustomViewSource.swift │ │ ├── Reflect.swift │ │ ├── StringExtension.swift │ │ ├── ViewSources.swift │ │ └── ViewStyle.swift │ ├── LICENSE │ └── README.md │ ├── Manifest.lock │ ├── Pods.xcodeproj │ └── project.pbxproj │ └── Target Support Files │ ├── Pods-CustomView │ ├── Info.plist │ ├── Pods-CustomView-Private.xcconfig │ ├── Pods-CustomView-dummy.m │ ├── Pods-CustomView-prefix.pch │ ├── Pods-CustomView-umbrella.h │ ├── Pods-CustomView.modulemap │ └── Pods-CustomView.xcconfig │ └── Pods │ ├── Info.plist │ ├── Pods-acknowledgements.markdown │ ├── Pods-acknowledgements.plist │ ├── Pods-dummy.m │ ├── Pods-environment.h │ ├── Pods-frameworks.sh │ ├── Pods-resources.sh │ ├── Pods-umbrella.h │ ├── Pods.debug.xcconfig │ ├── Pods.modulemap │ └── Pods.release.xcconfig ├── CustomViewObjc ├── CustomViewObjc-Prefix.pch ├── CustomViewObjc.h ├── CustomViewObjetiveC.h ├── CustomViewObjetiveC.m ├── CustomViewObjetiveC.xib ├── CustomViewTemplate.h ├── CustomViewTemplate.m ├── CustomWithXibLiveRenderKVC.h ├── CustomWithXibLiveRenderKVC.m ├── CustomWithXibLiveRenderKVC.xib ├── Info.plist ├── ViewSource.h └── ViewSource.m ├── CustomViewObjcTests ├── CustomViewObjcTests.m └── Info.plist ├── CustomViewSwift ├── CustomViewSwift.h ├── CustomViewTemplate.swift ├── CustomViewWithSwift.swift ├── Info.plist ├── LiveRenderKVC.swift ├── LiveRenderKVC.xib └── ViewSource.swift ├── CustomViewSwiftTests ├── CustomViewSwiftTests.swift └── Info.plist ├── CustomViewTest.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ ├── xcshareddata │ │ └── CustomViewTest.xccheckout │ └── xcuserdata │ │ ├── APP559.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── yume.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ ├── APP559.xcuserdatad │ ├── xcdebugger │ │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ │ ├── CustomViewObjc.xcscheme │ │ ├── CustomViewSwift.xcscheme │ │ ├── CustomViewTest.xcscheme │ │ └── xcschememanagement.plist │ └── yume.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── CustomViewObjc.xcscheme │ ├── CustomViewSwift.xcscheme │ ├── CustomViewTest.xcscheme │ └── xcschememanagement.plist ├── CustomViewTest ├── Base.lproj │ └── Main.storyboard ├── Custom.h ├── Custom.m ├── CustomViewTest-Info.plist ├── CustomViewTest-Prefix.pch ├── Images.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── LaunchImage.launchimage │ │ └── Contents.json ├── MyPlayground.playground │ ├── contents.xcplayground │ ├── section-1.swift │ └── timeline.xctimeline ├── TemplateView1.h ├── TemplateView1.m ├── TemplateView1.xib ├── en.lproj │ ├── Gpro_icon(512x512).png │ └── InfoPlist.strings ├── main.m ├── yumeAppDelegate.h ├── yumeAppDelegate.m ├── yumeViewController.h ├── yumeViewController.m └── zh-Hant.lproj │ ├── InfoPlist.strings │ └── Main.strings ├── CustomViewTestTests ├── CustomViewTestTests-Info.plist ├── CustomViewTestTests.m ├── en.lproj │ └── InfoPlist.strings └── zh-Hant.lproj │ └── InfoPlist.strings ├── CustomView_LiveRender_KVC.md ├── KVC.md ├── LICENSE ├── LiveRender.md ├── README.md ├── WWDC_2014_Session_401.md └── images ├── i1.png ├── i2.png ├── i3.png └── i5.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io 2 | 3 | ### OSX ### 4 | .DS_Store 5 | .AppleDouble 6 | .LSOverride 7 | 8 | # Icon must end with two \r 9 | Icon 10 | 11 | 12 | # Thumbnails 13 | ._* 14 | 15 | # Files that might appear on external disk 16 | .Spotlight-V100 17 | .Trashes 18 | 19 | # Directories potentially created on remote AFP share 20 | .AppleDB 21 | .AppleDesktop 22 | Network Trash Folder 23 | Temporary Items 24 | .apdisk 25 | 26 | 27 | ### Xcode ### 28 | build/ 29 | *.pbxuser 30 | !default.pbxuser 31 | *.mode1v3 32 | !default.mode1v3 33 | *.mode2v3 34 | !default.mode2v3 35 | *.perspectivev3 36 | !default.perspectivev3 37 | xcuserdata 38 | *.xccheckout 39 | *.moved-aside 40 | DerivedData 41 | *.xcuserstate 42 | 43 | 44 | ### Objective-C ### 45 | # Xcode 46 | # 47 | build/ 48 | *.pbxuser 49 | !default.pbxuser 50 | *.mode1v3 51 | !default.mode1v3 52 | *.mode2v3 53 | !default.mode2v3 54 | *.perspectivev3 55 | !default.perspectivev3 56 | xcuserdata 57 | *.xccheckout 58 | *.moved-aside 59 | DerivedData 60 | *.hmap 61 | *.ipa 62 | *.xcuserstate 63 | 64 | # CocoaPods 65 | # 66 | # We recommend against adding the Pods directory to your .gitignore. However 67 | # you should judge for yourself, the pros and cons are mentioned at: 68 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 69 | # 70 | # Pods/ 71 | -------------------------------------------------------------------------------- /CustomView.md: -------------------------------------------------------------------------------- 1 | # Custom View With xib (IOS 7 & 8) 2 | 3 | 中文版:[IOS Custom View Part 1](http://yume190.github.io/2014/09/26/IOS-Custom-View-Part-1/) 4 | 5 | Related Documents 6 | 7 | * [CustomView.h](CustomViewTest/TemplateView1.h) 8 | * [CustomView.m](CustomViewTest/TemplateView1.m) 9 | * [CustomView.xib](CustomViewTest/TemplateView1.xib) 10 | 11 | ### Step 1 12 | * Create a CustomView class inherit from UIView.(ex:CustomView.h & CustomView.m) 13 | * Then create xib file with same name.(ex:CustomView.xib) 14 | 15 | ### Step 2 16 | * Click the xib file 17 | * Find the File's Owner and click it 18 | * command + option + 3 19 | * Input your Custom Class name.(ex:CustomView) 20 | 21 | ### Step 3 22 | * Implement `initWithCoder` method 23 | 24 |
- (void) setup{
25 | NSString *nibName = NSStringFromClass([self class]);
26 | UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
27 | [nib instantiateWithOwner:self options:nil];
28 | //Add the view loaded from the nib into self.
29 | [self addSubview:self.view];
30 | }
31 |
32 | - (id)initWithCoder:(NSCoder *)aDecoder{
33 | self = [super initWithCoder:aDecoder];
34 |
35 | if (self) {
36 | [self setup];
37 | }
38 | return self;
39 | }
40 |
41 |
42 | ### Step 4 (Optional)
43 | * If you want to input your parameter at storyboard.
44 | * Drag a UIView to your storyboard.
45 | * Click it -> command + option + 3 -> input your Custom Class name
46 | * Find the "User denfined Runtime Attributes"
47 | * enter the parameter you want
48 |
49 | ex: String vTitle = "the title"
50 |
51 | | Key Path | Type | Value |
52 | | :------------ |:--------------|:--------|
53 | |vTitle |String |the title|
54 |
55 | * Create a property in CustomView.h
56 |
57 | @property (assign) NSString* vTitle;
58 |
59 | * Implement `awakeFromNib` in CustomView.m
60 |
61 | - (void)awakeFromNib {
62 | labelTitle.text = vTitle;
63 | }
64 |
--------------------------------------------------------------------------------
/CustomView.podspec:
--------------------------------------------------------------------------------
1 | Pod::Spec.new do |s|
2 | s.name = 'CustomView'
3 | s.version = '1.4'
4 | s.license = 'MIT'
5 | s.summary = "A Light weight Lib, custom a view with xib & live render"
6 | s.homepage = 'https://github.com/yume190/CustomView'
7 | s.authors = { 'yume190' => 'yume190@gmail.com' }
8 | s.social_media_url = "https://www.facebook.com/yume190"
9 | s.source = { :git => 'https://github.com/yume190/CustomView.git', :tag => s.version }
10 | #s.ios.deployment_target = '8.0'
11 | s.platform = :ios, "8.0"
12 | s.source_files = 'CustomViewKit/*.swift'
13 | s.requires_arc = true
14 | end
15 |
16 |
--------------------------------------------------------------------------------
/CustomViewKit/AutoLayoutExtension.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AutoLayoutExtension.swift
3 | // CustomViewTest
4 | //
5 | // Created by Yume on 2015/3/14.
6 | // Copyright (c) 2015年 yume. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | public struct LayoutAttribute {
12 | public var view:UIView?
13 | public var attribute:NSLayoutAttribute
14 | public var multiplier:CGFloat
15 | public var constant:CGFloat
16 |
17 | init(view:UIView, attribute: NSLayoutAttribute, constant:CGFloat = 0, multiplier:CGFloat = 1.0){
18 | self.view = view;
19 | self.attribute = attribute
20 | self.multiplier = multiplier
21 | self.constant = constant;
22 | }
23 |
24 | }
25 |
26 | public extension UIView {
27 |
28 | func addConstraints (constraints: NSLayoutConstraint...){
29 | for constraint in constraints {
30 | self.addConstraint(constraint)
31 | }
32 | }
33 |
34 | public var width : LayoutAttribute {
35 | get { return LayoutAttribute(view: self, attribute : .Width); }
36 | }
37 |
38 | public var height : LayoutAttribute {
39 | get { return LayoutAttribute(view: self, attribute : .Height); }
40 | }
41 |
42 | public var leading : LayoutAttribute {
43 | get { return LayoutAttribute(view: self, attribute : .Leading); }
44 | }
45 |
46 | public var trailing : LayoutAttribute {
47 | get { return LayoutAttribute(view: self, attribute : .Trailing); }
48 | }
49 |
50 | public var top : LayoutAttribute {
51 | get { return LayoutAttribute(view: self, attribute : .Top); }
52 | }
53 |
54 | public var bottom : LayoutAttribute {
55 | get { return LayoutAttribute(view: self, attribute : .Bottom); }
56 | }
57 |
58 | public var centerX : LayoutAttribute {
59 | get { return LayoutAttribute(view: self, attribute : .CenterX); }
60 | }
61 |
62 | public var centerY : LayoutAttribute {
63 | get { return LayoutAttribute(view: self, attribute : .CenterY); }
64 | }
65 |
66 | }
67 |
68 | public func == (left: LayoutAttribute, right: LayoutAttribute) -> NSLayoutConstraint {
69 | var layoutConstraint = NSLayoutConstraint(item:left.view!,
70 | attribute: left.attribute, relatedBy: NSLayoutRelation.Equal, toItem: right.view,
71 | attribute: right.attribute, multiplier: right.multiplier, constant: right.constant)
72 |
73 | return layoutConstraint
74 | }
75 |
76 | public func >= (left: LayoutAttribute, right: LayoutAttribute) -> NSLayoutConstraint {
77 | var layoutConstraint = NSLayoutConstraint(item: left.view!,
78 | attribute: left.attribute, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: right.view, attribute: right.attribute,
79 | multiplier: right.multiplier, constant: right.constant);
80 | return layoutConstraint
81 | }
82 |
83 | public func <= (left: LayoutAttribute, right: LayoutAttribute) -> NSLayoutConstraint {
84 | var layoutConstraint = NSLayoutConstraint(item: left.view!,
85 | attribute: left.attribute, relatedBy: NSLayoutRelation.LessThanOrEqual, toItem: right.view, attribute: right.attribute,
86 | multiplier: right.multiplier, constant: right.constant);
87 | return layoutConstraint
88 | }
89 |
90 | public func == (left: LayoutAttribute, right: CGFloat) -> NSLayoutConstraint {
91 | var layoutConstraint = NSLayoutConstraint(item:left.view!,
92 | attribute: left.attribute, relatedBy: NSLayoutRelation.Equal, toItem: nil,
93 | attribute: .NotAnAttribute, multiplier: 1.0, constant: right)
94 |
95 | return layoutConstraint
96 | }
97 |
98 | public func <= (left: LayoutAttribute, right: CGFloat) -> NSLayoutConstraint {
99 | var layoutConstraint = NSLayoutConstraint(item:left.view!,
100 | attribute: left.attribute, relatedBy: NSLayoutRelation.LessThanOrEqual, toItem: nil,
101 | attribute: .NotAnAttribute, multiplier: 1.0, constant: right)
102 |
103 | return layoutConstraint
104 | }
105 |
106 | public func >= (left: LayoutAttribute, right: CGFloat) -> NSLayoutConstraint {
107 | var layoutConstraint = NSLayoutConstraint(item:left.view!,
108 | attribute: left.attribute, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: nil,
109 | attribute: .NotAnAttribute, multiplier: 1.0, constant: right)
110 |
111 | return layoutConstraint
112 | }
113 |
114 | // ------------------------------------------
115 |
116 | infix operator *+ {
117 | associativity left
118 | precedence 150
119 | }
120 |
121 | public func *+(left: LayoutAttribute, right: (multiplier:CGFloat, contant:CGFloat)) -> LayoutAttribute {
122 | var layoutAttribute = LayoutAttribute(view:left.view!, attribute: left.attribute, constant: right.contant, multiplier:right.multiplier)
123 | return layoutAttribute
124 | }
125 |
126 | public func *(left: LayoutAttribute, right: CGFloat) -> LayoutAttribute {
127 | var layoutAttribute = LayoutAttribute(view:left.view!, attribute: left.attribute, constant: 0, multiplier:right)
128 | return layoutAttribute
129 | }
130 |
131 | public func +(left: LayoutAttribute, right: CGFloat) -> LayoutAttribute {
132 | var layoutAttribute = LayoutAttribute(view:left.view!, attribute: left.attribute, constant: right, multiplier:left.multiplier)
133 | return layoutAttribute
134 | }
135 |
136 | public func -(left: LayoutAttribute, right: CGFloat) -> LayoutAttribute {
137 | var layoutAttribute = LayoutAttribute(view:left.view!, attribute: left.attribute, constant: -right, multiplier:left.multiplier)
138 | return layoutAttribute
139 | }
140 |
141 | infix operator <~ {
142 | associativity left
143 | precedence 125
144 | }
145 |
146 | public func <~ (left: NSLayoutConstraint, right: UILayoutPriority) -> NSLayoutConstraint {
147 | left.priority = right
148 | return left
149 | }
150 |
151 | infix operator <- {
152 | associativity right
153 | precedence 90
154 | }
155 |
156 | public func <- (left: UIView, right: NSLayoutConstraint) -> UIView {
157 | left.addConstraint(right)
158 | return left
159 | }
160 |
--------------------------------------------------------------------------------
/CustomViewKit/CustomViewKit.h:
--------------------------------------------------------------------------------
1 | //
2 | // CustomViewKit.h
3 | // CustomViewKit
4 | //
5 | // Created by Yume on 2015/3/22.
6 | // Copyright (c) 2015年 yume. All rights reserved.
7 | //
8 |
9 | #import
5 | - (void) setup{
13 | NSString *nibName = NSStringFromClass([self class]);
14 |
15 | //Bundle Identifier can be find at Target -> Your Framework -> Bundle Identifier
16 | NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:@"com.yume190.CustomViewObjc"];
17 |
18 | UINib *nib = [UINib nibWithNibName:nibName bundle:frameworkBundle];
19 |
20 | [nib instantiateWithOwner:self options:nil];
21 | //Add the view loaded from the nib into self.
22 | [self addSubview:self.view];
23 | }
24 |
25 |
26 | ## custom view + KVC
27 |
28 | Related Documents
29 |
30 | * [ViewSource.m](CustomViewObjc/ViewSource.m)
31 | * [CustomWithXibLiveRenderKVC.h](CustomViewObjc/CustomWithXibLiveRenderKVC.h)
32 | * [CustomWithXibLiveRenderKVC.m](CustomViewObjc/CustomWithXibLiveRenderKVC.m)
33 | * and storyboard
34 |
35 |
--------------------------------------------------------------------------------
/KVC.md:
--------------------------------------------------------------------------------
1 | Key Value Coding
2 | ======
3 |
4 | * My blog(Ver. Chinese)
5 | * [IOS KVC](http://yume190.github.io/categories/IOS/KVC/)
6 |
7 | * IOS Dev Lib
8 |
9 | * [Key-Value Coding Programming Guide](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html#//apple_ref/doc/uid/10000107-SW1)
10 |
11 | * Struct to NSValue
12 |
13 | * [How to wrap a Struct into NSObject](http://stackoverflow.com/questions/5691881/how-to-wrap-a-struct-into-nsobject)
14 |
15 | * [What's the best way to put a c-struct in an NSArray?](http://stackoverflow.com/questions/4516991/whats-the-best-way-to-put-a-c-struct-in-an-nsarray)
16 |
17 | * bou.io
18 |
19 | * A very nice KVC tutorial, and KVC Custom Collections Operators[Key-Value Coding: Custom Operators](http://bou.io/KVCCustomOperators.html#fn:3)
20 | * KVC Open Source using `x.kvc[]`[An alternative syntax for KVC](http://bou.io/KVCTrampoline.html)
21 | * [Several Approaches to Higher-Order Functions with Objective-C Collections](http://bou.io/HigherOrderFunctionsInObjectiveC.html)
22 | * [Filtering NSArray With Key-Value Coding](http://bou.io/FilteringNSArrayWithKeyValueCoding.html)
23 |
24 | * Implement KVC
25 | * [Let's Build Key-Value Coding](https://www.mikeash.com/pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html)
26 |
27 | * KVC tutorial
28 | * [Objective-C Lesson 13: Key-Value Coding](http://www.binpress.com/tutorial/objectivec-lesson-13-keyvalue-coding/79)
29 | * [Learn Objective-C: Advanced KVC](http://www.binpress.com/tutorial/learn-objectivec-advanced-kvc/80)
30 |
31 | * Nice PPT[Intro to Cocoa KVC/KVO and Bindings](http://www.slideshare.net/sergio.acosta/intro-to-cocoa-kvckvo-and-bindings-presentation)
32 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2014 CustomView (yume190@gmail.com)
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in
11 | all copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 | THE SOFTWARE.
--------------------------------------------------------------------------------
/LiveRender.md:
--------------------------------------------------------------------------------
1 | Live Render(IOS Dev Library)
2 | ==========
3 |
4 | 中文版:[IOS Custom View Part 2](http://yume190.github.io/2014/09/26/IOS-Custom-View-Part-2/)
5 |
6 | * [Creating a Custom View that Renders in Interface Builder](https://developer.apple.com/library/prerelease/ios/recipes/xcode_help-IB_objects_media/CreatingaLiveViewofaCustomObject.html#//apple_ref/doc/uid/TP40014224-CH41-SW1)
7 | * By using the IBInspectable attribute to declare variables as inspectable properties, you allow Interface Builder to quickly rerender your custom view as you change the values of these properties in the Attributes inspector. You can attach the IBInspectable attribute to any property in a class declaration, class extension, or category for any type that’s supported by Interface Builder’s defined runtime attributes: __boolean, integer or floating point number, string, localized string, rectangle, point, size, color, range, and nil__.
8 | * If you need to create code for a custom view that runs only in Interface Builder, call that code from the method prepareForInterfaceBuilder. For example, while designing an app that uses the iPhone camera, you might want to draw an image that represents what the camera might capture. Although its compiled for runtime, code called from `prepareForInterfaceBuilder` never gets called except by Interface Builder at design time.
9 | * You can use the preprocessor macro `TARGET_INTERFACE_BUILDER` to specify code for inclusion with or exclusion from your custom view class.
10 |
11 | ### Quick Link
12 |
13 | * [Live Render Ver.Objetive-c (IOS 8)](#liveRenderObjc)
14 | * [Live Render Ver.Swift (IOS 8)](#liveRenderSwift)
15 | * Swift live render(use didset)[How to make awesome UI components in iOS 8 using Swift and XCode 6](http://www.weheartswift.com/make-awesome-ui-components-ios-8-using-swift-xcode-6/)
16 |
17 | --
18 |
19 | # Live Render Ver.Objetive-c (IOS 8)
20 |
21 | Related Documents
22 |
23 | * [CustomView.h](CustomViewObjc/CustomViewObjetiveC.h)
24 | * [CustomView.m](CustomViewObjc/CustomViewObjetiveC.m)
25 |
26 | ### Step 1 __Very Important__
27 | * Create a framework.
28 | * File -> New -> Target...
29 | * IOS -> Framework & Library -> Cocoa Touch Framework
30 |
31 | ### Step 2
32 | * Create a CustomView class inherit from UIView
33 |
34 | ### Step 3
35 | * Add `IB_DESIGNABLE` to your class
36 |
37 | #import <UIKit/UIKit.h>
38 | IB_DESIGNABLE
39 | @interface YourCustomVIew : UIView
40 | @end
41 |
42 |
43 | ### Step 4
44 | * Add some property with __`IBInspectable`__
45 |
46 | #import <UIKit/UIKit.h>
47 | IB_DESIGNABLE
48 | @interface YourCustomVIew : UIView
49 | @property (nonatomic) IBInspectable NSInteger lineWidth;
50 | @property (nonatomic) IBInspectable UIColor *fillColor;
51 | @property (nonatomic) IBInspectable UIColor *labelColor;
52 | @property (nonatomic) IBInspectable NSString *labelText;
53 | @property (nonatomic) IBInspectable CGRect labelRect;
54 | @end
55 |
56 |
57 | ### Step 5 (Branch 1)
58 | * Implement `drawRect` method
59 |
60 | - (void)drawRect:(CGRect)rect
61 | {
62 | UILabel *l =[[UILabel alloc] initWithFrame:_labelRect];
63 | l.text = _labelText;
64 | l.textColor = _labelColor;
65 | [self addSubview:l];
66 |
67 | // Drawing code
68 | CGContextRef context = UIGraphicsGetCurrentContext();
69 | CGRect myFrame = self.bounds;
70 |
71 | // Set the line width to 10 and inset the rectangle by
72 | // 5 pixels on all sides to compensate for the wider line.
73 | CGContextSetLineWidth(context, _lineWidth);
74 | CGRectInset(myFrame, 5, 5);
75 |
76 | [_fillColor set];
77 | UIRectFrame(myFrame);
78 | }
79 |
80 |
81 | ### Step 5 (Branch 2)
82 |
83 | * Implement `initWithCoder` method & `initWithFrame` method
84 |
85 | @interface CustomViewObjetiveC(){
86 | dispatch_once_t onceTokenViewLiveRendering;
87 | }
88 | @property (nonatomic,strong) UILabel *label;
89 | @end
90 |
91 | @implementation CustomViewObjetiveC
92 |
93 | - (instancetype)initWithCoder:(NSCoder *)aDecoder{
94 | self = [super initWithCoder:aDecoder];
95 |
96 | if (self) {
97 | [self setup];
98 | }
99 | return self;
100 | }
101 |
102 | - (instancetype)initWithFrame:(CGRect)frame{
103 | self = [super initWithFrame:frame];
104 |
105 | if (self) {
106 | [self setup];
107 | }
108 | return self;
109 | }
110 |
111 | -(void)setup{
112 | _label = [UILabel new];
113 | [self addSubview:_label];
114 | self.backgroundColor = [UIColor whiteColor];
115 | }
116 | @end
117 |
118 |
119 | ### Step 6 (Branch 2)
120 |
121 | * implement live render methods(`prepareForInterfaceBuilder` & `drawRect`)
122 |
123 | PS : some strange with live render methods.(I will explain at [Live Render Flow](#liveRenderFlow))
124 |
125 | -(void)prepareForInterfaceBuilder{
126 | [self viewLiveRendering];
127 | }
128 |
129 | - (void)drawRect:(CGRect)rect{
130 | #ifndef TARGET_INTERFACE_BUILDER
131 | [self viewLiveRendering];
132 | #endif
133 | }
134 |
135 | -(void)viewLiveRendering{
136 | dispatch_once(&onceTokenViewLiveRendering, ^{
137 | _label.frame = _labelRect;
138 | _label.text = _labelText;
139 | _label.textColor = _labelColor;
140 | });
141 | }
142 |
143 |
144 | --
145 |
146 | # Live Render Ver.Swift (IOS 8)
147 |
148 | Related Documents
149 |
150 | * [CustomViewSwift.swift](CustomViewSwift/CustomViewWithSwift.swift)
151 |
152 | ### Step 1 __Very Important__
153 | * Create a framework.
154 | * File -> New -> Target...
155 | * IOS -> Framework & Library -> Cocoa Touch Framework
156 |
157 | ### Step 2
158 | * Create a CustomView class inherit from UIView
159 |
160 | ### Step 3
161 | * Add `@IBDesignable` to your class
162 |
163 | import UIKit
164 | @IBDesignable
165 | class CustomViewWithSwift: UIView {
166 | }
167 |
168 |
169 | ### Step 4
170 | * Add instance variable and implement constructor. `init(coder : NSCoder)`
171 |
172 | import UIKit
173 | @IBDesignable
174 | class CustomViewWithSwift: UIView {
175 |
176 | @IBInspectable var labelColor: UIColor
177 | @IBInspectable var labelText: NSString = ""
178 | @IBInspectable var labelRect: CGRect
179 |
180 | init(coder : NSCoder) {
181 | labelColor = UIColor.blackColor()
182 | //labelText = NSString()
183 | labelRect = CGRect(x:0.0,y: 0.0,width: 200.0,height: 40.0)
184 |
185 | super.init(coder:coder)
186 | }
187 | }
188 |
189 |
190 | ### Step 5
191 | * Override `drawRect` method
192 |
193 | override func drawRect(rect: CGRect)
194 | {
195 | var l:UILabel = UILabel()
196 | l.text = labelText
197 | l.frame = labelRect
198 | l.textColor = labelColor
199 | addSubview(l)
200 | }
201 |
202 |
203 | # Live Render Flow
204 |
205 | | | APP running | `Debug Selected Views`