QMUIButton
@interface QMUIButton : UIButton
提供以下功能:
- 支持让文字和图片自动跟随 tintColor 变化(系统的 UIButton 默认是不响应 tintColor 的)
- highlighted、disabled 状态均通过改变整个按钮的alpha来表现,无需分别设置不同 state 下的 titleColor、image。alpha 的值可在配置表里修改 ButtonHighlightedAlpha、ButtonDisabledAlpha。
- 支持点击时改变背景色颜色(highlightedBackgroundColor)
- 支持点击时改变边框颜色(highlightedBorderColor)
- 支持设置图片相对于 titleLabel 的位置(imagePosition)
- 支持设置图片和 titleLabel 之间的间距,无需自行调整 titleEdgeInests、imageEdgeInsets(spacingBetweenImageAndTitle)
Warning
QMUIButton 重新定义了 UIButton.titleEdgeInests、imageEdgeInsets、contentEdgeInsets 这三者的布局逻辑,sizeThatFits: 里会把 titleEdgeInests 和 imageEdgeInsets 也考虑在内(UIButton 不会),以使这三个接口的使用更符合直觉。-
子类继承时重写的方法,一般不建议重写 initWithXxx
Declaration
Objective-C
- (void)didInitialize;
Swift
func didInitialize()
-
让按钮的文字颜色自动跟随tintColor调整(系统默认titleColor是不跟随的)
默认为NODeclaration
Objective-C
@property (nonatomic) BOOL adjustsTitleTintColorAutomatically;
Swift
var adjustsTitleTintColorAutomatically: Bool { get set }
-
让按钮的图片颜色自动跟随tintColor调整(系统默认image是需要更改renderingMode才可以达到这种效果)
默认为NODeclaration
Objective-C
@property (nonatomic) BOOL adjustsImageTintColorAutomatically;
Swift
var adjustsImageTintColorAutomatically: Bool { get set }
-
等价于 adjustsTitleTintColorAutomatically = YES & adjustsImageTintColorAutomatically = YES & tintColor = xxx
Warning
不支持传 nilDeclaration
Objective-C
@property (nonatomic, strong) UIColor *_Nonnull tintColorAdjustsTitleAndImage;
Swift
var tintColorAdjustsTitleAndImage: UIColor { get set }
-
是否自动调整highlighted时的按钮样式,默认为YES。
当值为YES时,按钮highlighted时会改变自身的alpha属性为ButtonHighlightedAlphaDeclaration
Objective-C
@property (nonatomic) BOOL adjustsButtonWhenHighlighted;
Swift
var adjustsButtonWhenHighlighted: Bool { get set }
-
是否自动调整disabled时的按钮样式,默认为YES。
当值为YES时,按钮disabled时会改变自身的alpha属性为ButtonDisabledAlphaDeclaration
Objective-C
@property (nonatomic) BOOL adjustsButtonWhenDisabled;
Swift
var adjustsButtonWhenDisabled: Bool { get set }
-
设置按钮点击时的背景色,默认为nil。
Warning
不支持带透明度的背景颜色。当设置highlightedBackgroundColor时,会强制把adjustsButtonWhenHighlighted设为NO,避免两者效果冲突。See
adjustsButtonWhenHighlightedDeclaration
Objective-C
@property (nonatomic, strong, nullable) UIColor *highlightedBackgroundColor;
Swift
var highlightedBackgroundColor: UIColor? { get set }
-
设置按钮点击时的边框颜色,默认为nil。
Warning
当设置highlightedBorderColor时,会强制把adjustsButtonWhenHighlighted设为NO,避免两者效果冲突。See
adjustsButtonWhenHighlightedDeclaration
Objective-C
@property (nonatomic, strong, nullable) UIColor *highlightedBorderColor;
Swift
var highlightedBorderColor: UIColor? { get set }
-
设置按钮里图标和文字的相对位置,默认为QMUIButtonImagePositionLeft
可配合imageEdgeInsets、titleEdgeInsets、contentHorizontalAlignment、contentVerticalAlignment使用Declaration
Objective-C
@property (nonatomic) QMUIButtonImagePosition imagePosition;
Swift
var imagePosition: QMUIButtonImagePosition { get set }
-
设置按钮里图标和文字之间的间隔,会自动响应 imagePosition 的变化而变化,默认为0。
系统默认实现需要同时设置 titleEdgeInsets 和 imageEdgeInsets,同时还需考虑 contentEdgeInsets 的增加(否则不会影响布局,可能会让图标或文字溢出或挤压),使用该属性可以避免以上情况。Warning
会与 imageEdgeInsets、 titleEdgeInsets、 contentEdgeInsets 共同作用。Declaration
Objective-C
@property (nonatomic) CGFloat spacingBetweenImageAndTitle;
Swift
var spacingBetweenImageAndTitle: CGFloat { get set }