如何创建像IBInspectable属性一样的段控件?

在此处输入图像描述 我想创建自定义控件,如段控件但我无法理解如何创建这种Segment IBInspectable属性。 我的意思是它的元素根据Segments增加。 据我所知, @IBInspectable没有数组支持。

你不能创建那种类型的@IBInspectable (但)……

您可以将String变量定义为@IBInspectable var,并向其添加多行。 然后让didSet方法将字符串拆分成一个内部使用的数组(例如)…

在此处输入图像描述

这些方面的东西:

 private var internalTextArray: [String]? @IBInspectable var segments: String = "" { didSet { internalTextArray = segments.components(separatedBy: "\n") // do something with the split-up lines of text } } 

@IBInspectable属性由user-defined runtime attribute支持,该user-defined runtime attribute尚不支持Segment数据类型。 所以我认为Storyboard不支持你想要的function。