UIButtonExtension
预览
此包包含三个组件
- UIRadioButton
- UICheckboxModern
- UICheckboxClassic
用法
安装
- 选项 1:将
pod 'UIButtonExtension', '~> 0.2'
添加到您的 Podfile 中。
UIRadioButton
UIRadioButton
实例默认具有一些属性。您可以根据需要自定义这些属性。要更改按钮大小,只需更改实例的 height
属性。
为了实现常见的单选按钮功能,即点击一个按钮禁用其余按钮,您可以使用 relate
函数将这些按钮在数组内关联起来。
import UIButtonExtension
let radioButton1 = UIRadioButton()
let radioButton2 = UIRadioButton()
let radioButton3 = UIRadioButton()
let radioButton4 = UIRadioButton()
radioButton1.relate(otherUIRadioButtons: [radioButton2, radioButton3]) //these four buttons are now related and only one can be active at a time.
由于 UIRadioButton
是 UIView
类型的,要使用 addTarget
方法,必须在 radioButton 内使用 button
。
import UIButtonExtension
let radioButton1 = UIRadioButton()
radioButton1.button.addTarget(self, action: #selector(example(_:)), for: .touchUpInside)
UIRadioButton
由两部分组成,外圈(类型为 Button
)和内圈(选中时显示)。要自定义按钮的一些属性,可以使用
import UIButtonExtension
let radioButton = UIRadioButton()
radioButton.color = .red //changes the color of the outer circle to red.
radioButton.selectedColor = .green //changes the color of the inner circle to green
radioButton.borderWidth = 5 //changes the width of the outer circle to 5
radioButton.cornerRadius = 20 //changes the cornerRadius of the button to 20. (no longer a circle)
radioButton.selectedSize = 0.8 //changes the ratio of the inner circle size to the outer circle, only available from 0 to 1
UIRadioButton
可以接受字符串以在四个角落显示。
import UIButtonExtension
let radioButton = UIRadioButton()
radioButton.leftText(text: String) // adds a label to the left of the button and adds the inserted
radioButton.rightText(text: String) // adds a label to the right of the button and adds the inserted
radioButton.topText(text: String) // adds a label to the top of the button and adds the inserted
radioButton.bottomText(text: String) // adds a label to the bottom of the button and adds the inserted
默认情况下,文本将具有动态字体大小,这将适应按钮的 height
和默认系统 font
以及 color
。要更改这些,可以使用这些
import UIButtonExtension
let radioButton = UIRadioButton()
radioButton.fontSize = 25 // change the size of the font
radioButton.font = UIFont(name: "Example", size: 20) // change the font of the text
radioButton.textColor = .blue // change the color of the text
UIRadioButton
的每个实例都将具有随机生成的 id
(NSUUID),您可以更改它。每个实例默认也是动画的。
import UIButtonExtension
let radioButton = UIRadioButton()
print(radioButton.id) //prints the current radioButton id
radioButton.id = "lorem Ipson" //changes the radioButton id to "lorem Ipson"
radioButton.animate = false //disables the selection animation
要了解按钮的当前状态,以查看是否选中,可以使用 isSelected
。
import UIButtonExtension
let radioButton = UIRadioButton()
if radioButton.isSelected == true {
//do stuff here
}
UICheckboxModern
UICheckboxModern
实例默认具有一些属性。您可以按需自定义这些属性。要更改按钮的大小,只需更改实例的 height
。在现代样式下点击时,框会被颜色填充,勾号位于容器框内。《code>UICheckboxModern 由两部分组成,包含框和选中时的勾号。要自定义按钮的一些属性,可以使用
let checkboxModern = UICheckboxModern()
checkboxModern.color = .red //changes the color of the containing box to red.
checkboxModern.selectedColor = .green //changes the color of the checkmark background to green
checkboxModern.borderWidth = 5 //changes the width of the containing box to 5
checkboxModern.checkedImage = UIImage(named: "how dare you change my default image.png") //changes the check mark image to what ever you like...
checkboxModern.cornerRadius = 5 //changes the corner radius of the containing box, can change value to make it a circle
UICheckboxModern
可以接收 String
以在四个角落显示。
import UIButtonExtension
let checkboxModern = UICheckboxModern()
checkboxModern.leftText(text: String) // adds a label to the left of the button and adds the inserted
checkboxModern.rightText(text: String) // adds a label to the right of the button and adds the inserted
checkboxModern.topText(text: String) // adds a label to the top of the button and adds the inserted
checkboxModern.bottomText(text: String) // adds a label to the bottom of the button and adds the inserted
默认情况下,文本将具有动态字体大小,这将适应按钮的 height
和默认系统 font
以及 color
。要更改这些,可以使用这些
import UIButtonExtension
let checkboxModern = UICheckboxModern()
checkboxModern.fontSize = 25 // change the size of the font
checkboxModern.font = UIFont(name: "Example", size: 20) // change the font of the text
checkboxModern.textColor = .blue // change the color of the text
UICheckboxModern
的每个实例都将具有随机生成的 id
(NSUUID),您可以更改它。每个实例默认也是动画的。
import UIButtonExtension
let checkboxModern = UICheckboxModern()
print(checkboxModern.id) //prints the current checkboxModern id
checkboxModern.id = "lorem Ipson" //changes the checkboxModern id to "lorem Ipson"
checkboxModern.animate = false //disables the selection animation
要了解按钮的当前状态以查看是否选中,可以使用 isSelected
。请记住,《code>isSelected 是只读属性,并且仅在按钮按下时改变。
import UIButtonExtension
let checkboxModern = UICheckboxModern()
if checkboxModern.isSelected == true {
//do stuff here
}
UICheckboxClassic
UICheckboxClassic
实例默认具有一些属性。您可以按需自定义这些属性。要更改按钮的大小,只需更改实例的 height
。在类型 2 下点击时,勾号会稍微弹出框架以创建其独特的视觉风格。请注意,如果按钮的宽度和高度不相等,则勾号的位置将基于 X 轴改变。《code>UICheckboxClassic 由两部分组成,包含框和选中时的勾号。要自定义按钮的一些属性,可以使用
import UIButtonExtension
let checkboxClassic = UICheckboxClassic()
checkboxClassic.color = .red //changes the color of the containing box to red.
checkboxClassic.selectedColor = .green //changes the color of the checkmark background to green
checkboxClassic.borderWidth = 5 //changes the width of the containing box to 5
checkboxClassic.checkedImage = UIImage(named: "how dare you change my default image.png") //changes the check mark image to what ever you like...
checkboxClassic.cornerRadius = 5 //changes the corner radius of the containing box, can change value to make it a circle
UICheckBoxClassic
可以接收 String
以在四个角落显示。
import UIButtonExtension
let checkboxClassic = UICheckboxClassic()
checkboxClassic.leftText(text: String) // adds a label to the left of the button and adds the inserted
checkboxClassic.rightText(text: String) // adds a label to the right of the button and adds the inserted
checkboxClassic.topText(text: String) // adds a label to the top of the button and adds the inserted
checkboxClassic.bottomText(text: String) // adds a label to the bottom of the button and adds the inserted
默认情况下,文本将具有动态字体大小,这将适应按钮的 height
和默认系统 font
以及 color
。要更改这些,可以使用这些
import UIButtonExtension
let checkboxClassic = UICheckboxClassic()
checkboxClassic.fontSize = 25 // change the size of the font
checkboxClassic.font = UIFont(name: "Example", size: 20) // change the font of the text
checkboxClassic.textColor = .blue // change the color of the text
每个 UICheckboxClassic
实例都将有一个随机生成的 id
(NSUUID),您可以更改它。每个实例默认也是动画的。
import UIButtonExtension
let checkboxClassic = UICheckboxClassic()
print(checkboxClassic.id) //prints the current checkboxClassic id
checkboxClassic.id = "lorem Ipson" //changes the checkboxClassic id to "lorem Ipson"
checkboxClassic.animate = false //disables the selection animation
要了解按钮的当前状态以查看是否选中,可以使用 isSelected
。请记住,《code>isSelected 是只读属性,并且仅在按钮按下时改变。
import UIButtonExtension
let checkboxClassic = UICheckboxClassic()
if checkboxClassic.isSelected == true {
//do stuff here
}
联系我
这是我第一次创建库,请多多担待
电子邮件:[email protected]
Twitter:@shadow_of_arman
Instagram:shadowofarman