How to Create an Ibinspectable of Type Enum

@IBInspectable with enum?

That's not possible (for now). You can only use those types that you see in User Defined Runtime Attributes section.

From Apple's doc:

You can attach the IBInspectable attribute to any property in a class declaration, class extension, or category for any type that’s supported by the Interface Builder defined runtime attributes: boolean, integer or floating point number, string, localized string, rectangle, point, size, color, range, and nil.

Property @IBInspectable cannot be marked

You need to add @objc for the enum:

@objc public enum Mode: Int {
...
}

@objc public enum Direction: Int {
...
}

You do need the Int base type, that can't be "blank" if it is an @objc style.

Swift UIFont IBInspectable - is it possible?

Idea

You can use Int enums to select one of the certain fonts.

Details

xCode 8.2.1, Swift 3

Code

enum FontType: Int

import UIKit
enum FontType: Int {
case Default = 0, Small, Large

var fount: UIFont {
switch self {
case .Default:
return UIFont.systemFont(ofSize: 17)
case .Small:
return UIFont.systemFont(ofSize: 12)
case .Large:
return UIFont.systemFont(ofSize: 24)
}
}

static func getFont(rawValue: Int) -> UIFont {
if let fontType = FontType(rawValue: rawValue) {
return fontType.fount
}
return FontType.Default.fount
}
}

class View: UIView

import UIKit
@IBDesignable
class View: UIView {

private var label: UILabel!

@IBInspectable var textFont:Int = 0

override func draw(_ rect: CGRect) {
super.draw(rect)
label = UILabel(frame: CGRect(x: 20, y: 20, width: 120, height: 40))
label.text = "Text"
label.textColor = .black
label.font = FontType.getFont(rawValue: textFont)
addSubview(label)
}

}

Main.storyboard

Sample Image

Results

Sample Image


Sample Image


Sample Image


Sample Image

Custom Class of UIButton and show it in StoryBoard having enum also

You need to specify your UIButton inherited class as IBDesignable and to add a NSInteger IBInspectable property that will be analyzed by InterfaceBuilder in order to be displayed.

However, Interface Builder can not render a enum type IBInspectable property; that's why the only way to achieve this is an NSInteger property.

Finally you will select a integer value in interface builder for your custom button (0, 1 ... YourEnumMaxValue) that corresponds to the enum value. At last, you just need to implement some code to render your button following the choosen value in IB.

More on LiveRendering : Creating a Custom View That Renders in Interface Builder

Create a OptionSet that is usable with IBInspectable

So I did find a way to be able to use it by writing some sort of adapter.

I'm pretty sure it can be done better and if anyone has a way to do so don't hesitate to provide your solution but this is how I did it right now

public struct Corners: OptionSet {
private enum Corner: Int, CustomStringConvertible {
case TopLeft=1
case TopRight=2
case BottomLeft=4
case BottomRight=8
case All=16

public var description: String {
var shift = 0
while (rawValue.hashValue >> shift != 1) { shift += 1 }
return ["topleft", "topright", "bottomleft", "bottomright", "all"][shift]
}
}
public let rawValue: Int
public init(rawValue: Int) { self.rawValue = rawValue }
private init(_ shape: Corner) { self.rawValue = shape.rawValue }

static let TopLeft = Corners(Corner.TopLeft)
static let TopRight = Corners(Corner.TopRight)
static let BottomLeft = Corners(Corner.BottomLeft)
static let BottomRight = Corners(Corner.BottomRight)
static let All = [TopLeft, TopRight, BottomLeft, BottomRight]
}

// Needed to split the string that's provided in the @IBInspectable. and remove any possible spaces the user introduced
extension String {
func getStrings() -> [String] {
var stringArray: [String] = []
let strings = self.characters.split{$0 == ","}.map(String.init)
for s in strings {
let string = s.removeSpaces()
stringArray.append(string)
}
return stringArray
}

func removeSpaces() -> String {
if self.characters.first == " " {
var copy = self
copy.characters.removeFirst()
return copy.removeSpaces()
} else {
return self
}
}
}

Then my @IBInspectable looks like this

var corners = [Corners.TopLeft]
@IBInspectable public var onCorners: String = "" {
willSet {
corners = []
for s in newValue.lowercased().getStrings() {
switch s {
case "topleft":
corners.append(Corners.TopLeft)
case "topright":
corners.append(Corners.TopRight)
case "bottomleft":
corners.append(Corners.BottomLeft)
case "bottomright":
corners.append(Corners.BottomRight)
case "all":
corners = Corners.All
default:
return
}
}
}
didSet {
// Do your logic here
}
}

@IBInspectable list of UIColors?

Sadly, I don't think you can do that. The IB interface for desigables is quite limited. You can only have single values of a limited number of types. I don't think there is any provision for arrays of things, which is what it sounds like you want.

You might be able to create more than one type of gradient view, each of which takes a different fixed number of colors (e.g. TwoColorGradientView, ThreeColorGradientView, FourColorGraidentView, and FiveColorGradientView). My guess is that a handful of classes would meet the vast majority of your needs.



Related Topics



Leave a reply



Submit