2014-11-04 54 views
20

總之,我想創建一個@IBInspectable屬性,該屬性允許您在故事板中的下拉菜單中選擇事件列表。此外,如果有辦法創建分隔線和更好地組織IBInspectables我想知道這是否可能。在我的示例中,我想爲電話號碼創建正則表達式字符串,以便當我訪問故事板時,可以在下拉菜單中選擇「電話號碼」項目,而不是輸入正則表達式字符串。IBInspectable創建下拉菜單和更好的組織

目前,我有一個子類TextField,這樣我可以更IBInspectables添加到它像正則表達式(你可以在圖片中看到)。所以,因爲它代表這就是我對我的子類UITextField

@IBDesignable public class FRM_TextField: UITextField { 


@IBInspectable public var regex : String? 

public var isValid : Bool{ 
    if let unwrappedRegex = regex{ 
     let applied_regex_expression = NSRegularExpression.regularExpressionWithPattern(unwrappedRegex, options: nil, error: nil) 

     let numberOfMatches = applied_regex_expression?.numberOfMatchesInString(text, options: nil, range: NSMakeRange(0, countElements(text))) 


     if(numberOfMatches > 0){ 
       return true 
     }else{ 
       return false 
     } 
    } 
    return false 
} 

    public required init(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
} 

    public override init(){ 
    super.init(); 
} 

    public override init(frame: CGRect) { 
    super.init(frame: frame) 
    } 
} 

Regex Toolbar

+1

對於人們來到這裏後,你可以即興/湊合着用,用戶可以打開和關閉幾個布爾屬性。這並不理想,但它是一種選擇。 – hhanesand 2015-08-21 01:24:47

+0

@Jordan Hochstetler爲你的問題+1是否有解決方案? – Rakesh 2018-01-03 11:11:09

回答

1

I would like to create an @IBInspectable property that allows you to select from a list of things in a drop down menu when you are in Storyboards

據我所知,列表(陣列)尚不支持。
支載類型到目前爲止是:
Int
CGFloat
Double
String
Bool
CGPoint
CGSize
CGRect
UIColor
UIImage

Also if there is a way to create dividers and better organize the IBInspectables
我不認爲這樣的事情是可能的。但也許有人有一個解決方法。

我建議你看WWDC Session 411 - What's New in Interface Builder

6

尚不支持任何列表或數組。

目前,下列類型支持@IBInspectable

  • 詮釋
  • CGFloat的
  • 字符串
  • 布爾
  • CGPoint
  • CGSize
  • 的CGRect
  • 的UIColor
  • 的UIImage

這裏是所有可用IBInspectable的代碼:

@IBInspectable var integer: NSInteger = 10 
    @IBInspectable var float: CGFloat = 10 
    @IBInspectable var double: Double = 10 
    @IBInspectable var string: String = "string" 
    @IBInspectable var bool: Bool = true 
    @IBInspectable var point: CGPoint = CGPointMake(1, 0) 
    @IBInspectable var rect: CGRect = CGRectMake(0, 0, 100, 100) 
    @IBInspectable var color: UIColor = UIColor.redColor() 
    @IBInspectable var size: CGSize = CGSizeMake(100, 100) 
    @IBInspectable var image: UIImage = UIImage(named: "Logo")! 

它看起來在IB是這樣的:

enter image description here

+0

[Apple文檔](http://help.apple.com/xcode/mac/8.0/#/devf60c1c514)支持的類型:'您可以將IBInspectable屬性添加到類聲明,類擴展或類別中的任何屬性類型:布爾值,整數或浮點數,字符串,本地化字符串,矩形,點,大小,顏色,範圍和零。 – 2016-11-24 01:52:56

5

由於就組織而言,你可以用分裂來組織它rs命名您的屬性,以便它們具有相同的前綴。

@IBInspectable var ValText : Bool! = false 
@IBInspectable var ValEmail : Bool! = false 
@IBInspectable var ValCreditCard : Bool! = false 
@IBInspectable var Positives : Bool! = false 
@IBInspectable var Money : Bool! = false 
@IBInspectable var Phone : Bool! = false 
@IBInspectable var ZipCode : Bool! = false 
@IBInspectable var Street : Bool! = false 
@IBInspectable var IPAddress : Bool! = false 
@IBInspectable var MAC : Bool! = false 
@IBInspectable var AlphaNum : Bool! = false 
@IBInspectable var AlphaNumSpaces : Bool! = false 
@IBInspectable var AlphaNumNoSpaces : Bool! = false 
@IBInspectable var URL : Bool! = false 
@IBInspectable var ValidationType : String! = "" 

呈現爲

IB