热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

IBInspectable创建下拉列表和更好的组织-IBInspectableCreatingaDropdownandbetterOrganization

Inshort,Iwouldliketocreatean@IBInspectablepropertythatallowsyoutoselectfromalistof

In short, 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. Also if there is a way to create dividers and better organize the IBInspectables I would like to know if this is possible too. In my example, I would like to create regex strings for a phone number so that when I go to the storyboard I can just select the "phone number" item in a drop down menu instead of entering a regex string.

简而言之,我想创建一个@IBInspectable属性,允许您在Storyboard中从下拉菜单中选择一些内容。此外,如果有办法创建分隔线并更好地组织IBInspectables我想知道这是否也可行。在我的示例中,我想为电话号码创建正则表达式字符串,以便当我转到故事板时,我可以在下拉菜单中选择“电话号码”项,而不是输入正则表达式字符串。

Currently I have subclassed a TextField so that I can add even more IBInspectables to it like regex (which you can see in the picture). So as it stands this is what I have for my subclassed UITextField:

目前我已经将一个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

3 个解决方案

#1


7  

As far as organization, You can organize it with dividers by naming your properties so that they have the same prefix.

就组织而言,您可以通过命名属性来使用分隔符来组织它,以便它们具有相同的前缀。

@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! = ""

Renders as

呈现为

IB

#2


6  

There is no support of any lists or arrays yet.

目前还不支持任何列表或数组。

Currently the following types support @IBInspectable

目前,以下类型支持@IBInspectable

  • Int
  • 诠释
  • CGFloat
  • CGFloat的
  • Double
  • String
  • Bool
  • 布尔
  • CGPoint
  • CGPoint
  • CGSize
  • CGSize
  • CGRect
  • 的CGRect
  • UIColor
  • 的UIColor
  • UIImage
  • 的UIImage

Here is a code with all available IBInspectable's:

这是一个包含所有可用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")!

And it looks in IB like this:

它在IB看起来像这样:

enter image description here

#3


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

我想创建一个@IBInspectable属性,允许您在Storyboard中从下拉菜单中选择一些内容

As far as I know, lists (arrays) are not supported yet.
Suported types so far are:
Int
CGFloat
Double
String
Bool
CGPoint
CGSize
CGRect
UIColor
UIImage

据我所知,目前还不支持列表(数组)。到目前为止支持的类型是:Int CGFloat Double String Bool CGPoint CGSize CGRect UIColor UIImage

Also if there is a way to create dividers and better organize the IBInspectables
I don't think that such thing is possible. But maybe someone has a workaround.

此外,如果有办法创建分隔线并更好地组织IBInspectables,我不认为这样的事情是可能的。但也许有人有一个解决方法。

I recommend you to watch WWDC Session 411 - What's New in Interface Builder.

我建议你观看WWDC Session 411 - Interface Builder中的新功能。


推荐阅读
author-avatar
Rianbow_小渊渊设
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有