Skip to content

Commit

Permalink
Move @IBInspectable to UIDesignable
Browse files Browse the repository at this point in the history
  • Loading branch information
Domas Nutautas committed Jan 26, 2017
1 parent 4ffff23 commit c8f0cc1
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 51 deletions.
13 changes: 3 additions & 10 deletions TextStyle/Source/Extensions/UIButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,9 @@ extension UIButton: TextStylable {

// MARK: Interface Builder

public extension UIButton {
#if TARGET_INTERFACE_BUILDER
@IBInspectable var textStyle: String {
extension UIButton {
var textStyle: String {
get { return "" }
set { set(textStyleIBString: newValue) }
set { set(textStyle: newValue) }
}
#else
internal var textStyle: String {
get { return "" }
set { set(textStyleIBString: newValue) }
}
#endif
}
13 changes: 3 additions & 10 deletions TextStyle/Source/Extensions/UILabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,9 @@ extension UILabel: TextStylable {

// MARK: Interface Builder

public extension UILabel {
#if TARGET_INTERFACE_BUILDER
@IBInspectable var textStyle: String {
extension UILabel {
var textStyle: String {
get { return "" }
set { set(textStyleIBString: newValue) }
set { set(textStyle: newValue) }
}
#else
internal var textStyle: String {
get { return "" }
set { set(textStyleIBString: newValue) }
}
#endif
}
13 changes: 3 additions & 10 deletions TextStyle/Source/Extensions/UITextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,9 @@ extension UITextField: TextStylable {

// MARK: Interface Builder

public extension UITextField {
#if TARGET_INTERFACE_BUILDER
@IBInspectable var textStyle: String {
extension UITextField {
var textStyle: String {
get { return "" }
set { set(textStyleIBString: newValue) }
set { set(textStyle: newValue) }
}
#else
internal var textStyle: String {
get { return "" }
set { set(textStyleIBString: newValue) }
}
#endif
}
13 changes: 3 additions & 10 deletions TextStyle/Source/Extensions/UITextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,9 @@ extension UITextView: TextStylable {

// MARK: Interface Builder

public extension UITextView {
#if TARGET_INTERFACE_BUILDER
@IBInspectable var textStyle: String {
extension UITextView {
var textStyle: String {
get { return "" }
set { set(textStyleIBString: newValue) }
set { set(textStyle: newValue) }
}
#else
internal var textStyle: String {
get { return "" }
set { set(textStyleIBString: newValue) }
}
#endif
}
27 changes: 25 additions & 2 deletions TextStyle/Source/IBInspectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,31 @@ public protocol IBInspectable {
}

extension TextStylable {
func set(textStyleIBString string: String) {
guard let textStyle = (TextStyle.self as? IBInspectable.Type)?.stylesDictionary[string] else { return }
func set(textStyle key: String) {
guard let textStyle = (TextStyle.self as? IBInspectable.Type)?.stylesDictionary[key] else { return }
set(textStyle: textStyle)
}
}

/**
Makes your text styles accessible from Interface Builder.

## Implementation

@IBDesignable public class UILabelDesignable: UILabel, IBTextStylable {
@IBInspectable public var textStyle: String {
get { return "" }
set { set(textStyleIBString: newValue) }
}
}

*/
public protocol IBTextStylable: TextStylable {
var textStyle: String { get set }
func set(textStyleIBString textStyleString: String)
}
public extension IBTextStylable {
func set(textStyleIBString textStyleString: String) {
set(textStyle: textStyleString)
}
}
29 changes: 25 additions & 4 deletions TextStyle/Source/UIDesignable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,36 @@
//

import UIKit
import TextStyle

/// `UILabel` that renders in Interface Builder as an `@IBDesignbale`
@IBDesignable public class UILabelDesignable: UILabel {}
@IBDesignable public class UILabelDesignable: UILabel, IBTextStylable {
@IBInspectable public var textStyle: String {
get { return "" }
set { set(textStyleIBString: newValue) }
}
}

/// `UITextField` that renders in Interface Builder as an `@IBDesignbale`
@IBDesignable public class UITextFieldDesignable: UITextField {}
@IBDesignable public class UITextFieldDesignable: UITextField, IBTextStylable {
@IBInspectable public var textStyle: String {
get { return "" }
set { set(textStyleIBString: newValue) }
}
}

/// `UITextView` that renders in Interface Builder as an `@IBDesignbale`
@IBDesignable public class UITextViewDesignable: UITextView {}
@IBDesignable public class UITextViewDesignable: UITextView, IBTextStylable {
@IBInspectable public var textStyle: String {
get { return "" }
set { set(textStyleIBString: newValue) }
}
}

/// `Button` that renders in Interface Builder as an `@IBDesignbale`
@IBDesignable public class UIButtonDesignable: UIButton {}
@IBDesignable public class UIButtonDesignable: UIButton, IBTextStylable {
@IBInspectable public var textStyle: String {
get { return "" }
set { set(textStyleIBString: newValue) }
}
}
10 changes: 5 additions & 5 deletions TextStyleExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="TextStyle" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="7oE-pw-Z5X" customClass="UILabelDesignable" customModule="TextStyle">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="TextStyle" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="7oE-pw-Z5X" customClass="UILabelDesignable" customModule="TextStyleExample" customModuleProvider="target">
<rect key="frame" x="16" y="28" width="343" height="21.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
Expand All @@ -31,7 +31,7 @@
<userDefinedRuntimeAttribute type="string" keyPath="textStyle" value="header"/>
</userDefinedRuntimeAttributes>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Helps you manage text styles in your app." textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="505-Fq-rz2" customClass="UILabelDesignable" customModule="TextStyle">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Helps you manage text styles in your app." textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="505-Fq-rz2" customClass="UILabelDesignable" customModule="TextStyleExample" customModuleProvider="target">
<rect key="frame" x="16" y="57.5" width="343" height="19.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
Expand All @@ -40,7 +40,7 @@
<userDefinedRuntimeAttribute type="string" keyPath="textStyle" value="body"/>
</userDefinedRuntimeAttributes>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Still in beta" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="FgL-GY-FZ6" customClass="UILabelDesignable" customModule="TextStyle">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Still in beta" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="FgL-GY-FZ6" customClass="UILabelDesignable" customModule="TextStyleExample" customModuleProvider="target">
<rect key="frame" x="16" y="85" width="343" height="14.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
Expand All @@ -49,13 +49,13 @@
<userDefinedRuntimeAttribute type="string" keyPath="textStyle" value="footer"/>
</userDefinedRuntimeAttributes>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Yoc-Ct-220">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Yoc-Ct-220" customClass="UILabelDesignable" customModule="TextStyleExample" customModuleProvider="target">
<rect key="frame" x="16" y="107.5" width="343" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="AttrubutedLabel" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="BGk-XY-H1b">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="AttrubutedLabel" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="BGk-XY-H1b" customClass="UILabelDesignable" customModule="TextStyleExample" customModuleProvider="target">
<rect key="frame" x="16" y="136" width="343" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
Expand Down

0 comments on commit c8f0cc1

Please sign in to comment.