Skip to content

Commit f2712d1

Browse files
committed
Make the icon and placeholder of ICTokenField IBInspectable
1 parent de446b6 commit f2712d1

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

Example/Example/Main.storyboard

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<rect key="frame" x="0.0" y="269.5" width="600" height="88"/>
1919
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
2020
<fontDescription key="fontDescription" type="system" pointSize="15"/>
21-
<state key="normal" title="Back"/>
21+
<state key="normal" title="Back to Code"/>
2222
<connections>
2323
<action selector="dismiss:" destination="0fq-Xw-Z0G" eventType="touchUpInside" id="Ia6-YR-Zzm"/>
2424
</connections>
@@ -59,6 +59,22 @@
5959
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="nCe-ZG-eEP" id="6tu-Im-VHX">
6060
<rect key="frame" x="0.0" y="0.0" width="600" height="43.5"/>
6161
<autoresizingMask key="autoresizingMask"/>
62+
<subviews>
63+
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="2dP-7v-mWC" customClass="ICTokenField" customModule="ICInputAccessory">
64+
<rect key="frame" x="0.0" y="0.0" width="600" height="43.5"/>
65+
<color key="backgroundColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
66+
<userDefinedRuntimeAttributes>
67+
<userDefinedRuntimeAttribute type="string" keyPath="placeholder" value="ICTokenFIeld"/>
68+
<userDefinedRuntimeAttribute type="image" keyPath="icon" value="icook-iphone-input-search"/>
69+
<userDefinedRuntimeAttribute type="number" keyPath="cornerRadius">
70+
<real key="value" value="5"/>
71+
</userDefinedRuntimeAttribute>
72+
<userDefinedRuntimeAttribute type="number" keyPath="borderWidth">
73+
<real key="value" value="0.0"/>
74+
</userDefinedRuntimeAttribute>
75+
</userDefinedRuntimeAttributes>
76+
</view>
77+
</subviews>
6278
</tableViewCellContentView>
6379
</tableViewCell>
6480
</cells>
@@ -104,4 +120,7 @@
104120
<point key="canvasLocation" x="708" y="377"/>
105121
</scene>
106122
</scenes>
123+
<resources>
124+
<image name="icook-iphone-input-search" width="20" height="20"/>
125+
</resources>
107126
</document>

Source/ICTokenField.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import UIKit
4444
////////////////////////////////////////////////////////////////////////////////
4545

4646

47+
@IBDesignable
4748
public class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDelegate {
4849

4950
/// The receiver’s delegate.
@@ -58,7 +59,7 @@ public class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDele
5859
}
5960

6061
/// The image on the left of text field.
61-
public var icon: UIImage? {
62+
@IBInspectable public var icon: UIImage? {
6263
didSet {
6364
if let icon = icon {
6465
let imageView = UIImageView(image: icon)
@@ -77,7 +78,7 @@ public class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDele
7778
}
7879

7980
/// The placeholder with the default color and font.
80-
public var placeholder: String? {
81+
@IBInspectable public var placeholder: String? {
8182
get {
8283
return attributedPlaceholder?.string
8384
}
@@ -213,6 +214,19 @@ public class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDele
213214
layoutTokenTextField()
214215
}
215216

217+
// MARK: - NSKeyValueCoding
218+
219+
public override func setValue(value: AnyObject?, forUndefinedKey key: String) {
220+
switch value {
221+
case let image as UIImage? where key == "icon":
222+
icon = image
223+
case let text as String? where key == "placeholder":
224+
placeholder = text
225+
default:
226+
break
227+
}
228+
}
229+
216230
// MARK: - UITextFieldDelegate
217231

218232
public func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
@@ -340,8 +354,6 @@ public class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDele
340354
frame = CGRect(x: 0, y: 7, width: UIScreen.mainScreen().bounds.width, height: 30)
341355
}
342356

343-
backgroundColor = UIColor.whiteColor()
344-
345357
addSubview(scrollView)
346358
scrollView.addSubview(inputTextField)
347359
scrollView.translatesAutoresizingMaskIntoConstraints = false

0 commit comments

Comments
 (0)