iOS开发使用UIControl封装@”上下文”控件

释放双眼,带上耳机,听听看~!

 

写在前面:

为什么要是用UIControl进行控件封装,而不是使用UIView进行封装?
请参照http://blog.csdn.net/rookiejin/article/details/74639294


直接上代码、.h文件

#import <UIKit/UIKit.h>
/** 上文下文组件 */
@interface RjxTopAndBottomTitleView : UIControl
/** 顶部Label */
@property (nonatomic, strong) UILabel *topLabel;
/** 底部Label */
@property (nonatomic, strong) UILabel *bottomLabel;
/** 顶部和底部间隙 */
@property (nonatomic, assign) CGFloat spaceBetweenTopAndBottomLabel;
/**
* @description 赋值
* @param topText 顶部文本内容 可以为空
* @Param BottomText 底部文本内容 可以为空
*/
- (void)setTopLabelWithTopText:(NSString *)topText BottomText:(NSString*)bottomText;
/**
* @description 顶部文本text对应state
* @param title 顶部文本内容 可以为空
* @Param state UIControlState 枚举类型 状态值
*/
- (void)setTopTitle:(NSString *)title forState:(UIControlState)state;
/**
* @description 顶部文本textColor对应state
* @param color 顶部文本color
* @Param state UIControlState 枚举类型 状态值
*/
- (void)setTopTitleColor:(UIColor *)color forState:(UIControlState)state;
/**
* @description 底部文本text对应state
* @param title 底部文本内容 可以为空
* @Param state UIControlState 枚举类型 状态值
*/
- (void)setBottomTitle:(NSString *)title forState:(UIControlState)state;
/**
* @description 底部文本textColor对应state
* @param color 底部文本color
* @Param state UIControlState 枚举类型 状态值
*/
- (void)setBottomTitleColor:(UIColor *)color forState:(UIControlState)state;
@end
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/** 用于存储不同state下的样式变化 */
@interface RjxTopAndBottomTitleModel : NSObject
@property (nonatomic, strong) NSString *topTitleLabelText;
@property (nonatomic, strong) UIColor *topTitleTextColor;
@property (nonatomic, strong) NSString *bottomTitleLabelText;
@property (nonatomic, strong) UIColor *bottomTitleTextColor;
@end

.m文件

#import "RjxTopAndBottomTitleView.h"
static NSString *const activatekey = @"activate";
static NSString *const deactivatekey = @"deactivate";
@interface RjxTopAndBottomTitleView ()
/** 记录label相关属性,以及对应的state的值 */
@property (nonatomic,strong) NSMutableDictionary *labelPropertyDic;
/** 控件的间隙 */
@property (nonatomic,strong) NSLayoutConstraint *betweenLabelSpaceConstraint;
/** 纵向 */
@property (nonatomic, strong) NSMutableDictionary *contentVerticalAlignmentDic;
/** 横向 */
@property (nonatomic, strong) NSMutableDictionary *contentHorizontalAlignmentDic;
@property (nonatomic, strong) UIView *contentView;
@end
@implementation RjxTopAndBottomTitleView
/**
初始化
*/
- (instancetype)init
{
self = [super init];
if (self) {
[self doSetUpControl];
}
return self;
}
/**
初始化界面
*/
- (void)doSetUpControl
{
_spaceBetweenTopAndBottomLabel = 3.0f;
[self addSubview:self.contrainView];
self.contrainView.backgroundColor = [UIColor clearColor];
[self.contrainView addCenterX:0 toView:self];
[self.contrainView addCenterY:0 toView:self];
NSLayoutConstraint *constraint_top_greater = [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0];
NSLayoutConstraint *constraint_left_greater = [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
NSLayoutConstraint *constraint_bottom_less = [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationLessThanOrEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
NSLayoutConstraint *constraint_right_less = [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationLessThanOrEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0];
[self addConstraints:@[
constraint_top_greater,
constraint_left_greater,
constraint_bottom_less,
constraint_right_less,
]];
[self.contentView addSubview:self.topLabel];
[self.topLabel topToView:self.contentView withSpace:0];
[self.topLabel addCenterX:0 toView:self.contentView];
[self.contentView addConstraints:@[
[NSLayoutConstraint constraintWithItem:self.topLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1 constant:0],
[NSLayoutConstraint constraintWithItem:self.topLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1 constant:0]
]];
[self.contentView addSubview:self.bottomLabel];
[self.bottomLabel bottomToView:self.contentView withSpace:0];
[self.bottomLabel addCenterX:0 toView:self.contentView];
[self.contentView addConstraints:@[
[NSLayoutConstraint constraintWithItem:self.bottomLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1 constant:0],
[NSLayoutConstraint constraintWithItem:self.bottomLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1 constant:0]
]];
//
self.betweenLabelSpaceConstraint = [self.topLabel bottomToView:self.bottomLabel withSpace:self.spaceBetweenTopAndBottomLabel];
NSLayoutConstraint *contentView_centerX = [self.contentView addCenterX:0 toView:self];
NSLayoutConstraint *contentView_centerY = [self.contentView addCenterY:0 toView:self];
NSLayoutConstraint *contentView_left = [self.contentView leftToView:self withSpace:0];
NSLayoutConstraint *contentView_right = [self.contentView rightToView:self withSpace:0];
NSLayoutConstraint *contentView_top = [self.contentView topToView:self withSpace:0];
NSLayoutConstraint *contentView_bottom = [self.contentView bottomToView:self withSpace:0];
[self.contentVerticalAlignmentDic addEntriesFromDictionary:@{
@(UIControlContentVerticalAlignmentCenter) : contentView_centerY,
@(UIControlContentVerticalAlignmentTop) : contentView_top,
@(UIControlContentVerticalAlignmentBottom) : contentView_bottom
}];
[self.contentHorizontalAlignmentDic addEntriesFromDictionary:@{
@(UIControlContentHorizontalAlignmentCenter) : contentView_centerX,
@(UIControlContentHorizontalAlignmentLeft) : contentView_left,
@(UIControlContentHorizontalAlignmentRight) : contentView_right
}];
self.contentVerticalAlignment = self.contentVerticalAlignment;
self.contentHorizontalAlignment = self.contentHorizontalAlignment;
}
- (void)setContentVerticalAlignment:(UIControlContentVerticalAlignment)contentVerticalAlignment
{
[super setContentVerticalAlignment:contentVerticalAlignment];
[self setUpContentHorizontalAlignment];
}
- (void)setContentHorizontalAlignment:(UIControlContentHorizontalAlignment)contentHorizontalAlignment
{
[super setContentHorizontalAlignment:contentHorizontalAlignment];
[self setUpContentVerticalAlignment];
}
/***
*设置contentview 的位置 九宫格 九种位置 6大属性决定
*/
- (void)setUpContentHorizontalAlignment
{
//全部设置为不激活状态
[NSLayoutConstraint deactivateConstraints:self.contentHorizontalAlignmentDic.allValues];
NSMutableArray *activeConstraints = [[NSMutableArray alloc] init];
//横向
if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentFill) {
[activeConstraints addObject:[self.contentHorizontalAlignmentDic objectForKey:@(UIControlContentHorizontalAlignmentLeft)]];
[activeConstraints addObject:[self.contentHorizontalAlignmentDic objectForKey:@(UIControlContentHorizontalAlignmentRight)]];
}else{
[activeConstraints addObject:[self.contentHorizontalAlignmentDic objectForKey:@(self.contentHorizontalAlignment)]];
}
[NSLayoutConstraint activateConstraints:activeConstraints];
}
/***
*设置contentview 的位置 九宫格 九种位置 6大属性决定
*/
- (void)setUpContentVerticalAlignment
{
[NSLayoutConstraint deactivateConstraints:self.contentVerticalAlignmentDic.allValues];
//全部设置为不激活状态
NSMutableArray *activeConstraints = [[NSMutableArray alloc] init];
//纵向
if (self.contentVerticalAlignment == UIControlContentVerticalAlignmentFill) {
[activeConstraints addObject:[self.contentVerticalAlignmentDic objectForKey:@(UIControlContentVerticalAlignmentTop)]];
[activeConstraints addObject:[self.contentVerticalAlignmentDic objectForKey:@(UIControlContentVerticalAlignmentBottom)]];
}else{
[activeConstraints addObject:[self.contentVerticalAlignmentDic objectForKey:@(self.contentVerticalAlignment)]];
}
[NSLayoutConstraint activateConstraints:activeConstraints];
}
-(void)setSpaceBetweenTopAndBott4omLabel:(CGFloat)spaceBetweenTopAndBottomLabel
{
if (spaceBetweenTopAndBottomLabel == _spaceBetweenTopAndBottomLabel) {
return;
}
_spaceBetweenTopAndBottomLabel = spaceBetweenTopAndBottomLabel;
self.betweenLabelSpaceConstraint.constant = _spaceBetweenTopAndBottomLabel;
}
/**
赋值
*/
- (void)setTopLabelWithTopText:(NSString *)topText BottomText:(NSString*)bottomText
{
/// 如果执行赋值方法,就默认sate = UIControlStateNormal
self.topLabel.text = topText;
self.bottomLabel.text = bottomText;
RjxTopAndBottomTitleModel *Model = [[RjxTopAndBottomTitleModel alloc] init];
Model.topTitleLabelText = topText;
Model.topTitleTextColor = [UIColor blackColor];
Model.bottomTitleLabelText = bottomText;
Model.bottomTitleTextColor = [UIColor blackColor];
[self.labelPropertyDic setObject:Model forKey:@(UIControlStateNormal)];
}
/**
顶部文本text对应state
*/
- (void)setTopTitle:(NSString *)title forState:(UIControlState)state
{
if (title != nil) {
if (state == UIControlStateNormal) {
self.topLabel.text = title;
}
RjxTopAndBottomTitleModel *model = [self getValueFromDicPropertyforKey:state];
model.topTitleLabelText = title;
}
}
/**
顶部文本textColor对应state
*/
- (void)setTopTitleColor:(UIColor *)color forState:(UIControlState)state
{
if (color) {
if (state == UIControlStateNormal)
{
self.topLabel.textColor = color;
}
RjxTopAndBottomTitleModel *model = [self getValueFromDicPropertyforKey:state];
model.topTitleTextColor = color;
}
}
/**
底部文本text对应state
*/
- (void)setBottomTitle:(NSString *)title forState:(UIControlState)state
{
if (title != nil) {
if (state == UIControlStateNormal)
{
self.bottomLabel.text = title;
}
RjxTopAndBottomTitleModel *model = [self getValueFromDicPropertyforKey:state];
model.bottomTitleLabelText = title;
}
}
/**
底部文本textColor对应state
*/
- (void)setBottomTitleColor:(UIColor *)color forState:(UIControlState)state
{
if (color) {
if (state == UIControlStateNormal)
{
self.bottomLabel.textColor = color;
}
RjxTopAndBottomTitleModel *model = [self getValueFromDicPropertyforKey:state];
model.bottomTitleTextColor = color;
}
}
/**
取消高亮
*/
-(void)setHighlighted:(BOOL)highlighted
{
}
- (void)setSelected:(BOOL)selected
{
[super setSelected:selected];
UIControlState state = UIControlStateNormal;
if (selected) {
state = UIControlStateSelected;
}else{
state = UIControlStateNormal;
}
RjxTopAndBottomTitleModel *topModel = [self getValueFromDicPropertyforKey:state];
self.topLabel.text = topModel.topTitleLabelText;
self.topLabel.textColor = topModel.topTitleTextColor;
self.bottomLabel.text = topModel.bottomTitleLabelText;
self.bottomLabel.textColor = topModel.bottomTitleTextColor;
}
- (RjxTopAndBottomTitleModel *)getValueFromDicPropertyforKey:(UIControlState)state
{
RjxTopAndBottomTitleModel *model = nil;
if ([self.labelPropertyDic objectForKey:@(state)]) {
model =[self.labelPropertyDic objectForKey:@(state)];
}else{
model = [[RjxTopAndBottomTitleModel alloc] init];
[self.labelPropertyDic setObject:model forKey:@(state)];
}
return model;
}
#pragma mark -- 懒加载
-(UIView *)contrainView
{
if (!_contentView) {
_contentView = [[UIView alloc] init];
}
return _contentView;
}
- (UILabel *)topLabel
{
if (!_topLabel) {
_topLabel = [[UILabel alloc] init];
_topLabel.font = ___THEME_FONT(15, YES);
}
return _topLabel;
}
- (UILabel *)bottomLabel
{
if (!_bottomLabel) {
_bottomLabel = [[UILabel alloc] init];
_bottomLabel.font = ___THEME_FONT(13, NO);
}
return _bottomLabel;
}
- (NSMutableDictionary *)contentVerticalAlignmentDic
{
if (!_contentVerticalAlignmentDic) {
_contentVerticalAlignmentDic = [NSMutableDictionary dictionary];
}
return _contentVerticalAlignmentDic;
}
- (NSMutableDictionary *)contentHorizontalAlignmentDic
{
if (!_contentHorizontalAlignmentDic) {
_contentHorizontalAlignmentDic = [NSMutableDictionary dictionary];
}
return _contentHorizontalAlignmentDic;
}
- (NSMutableDictionary *)labelPropertyDic
{
if (!_labelPropertyDic) {
_labelPropertyDic = [NSMutableDictionary dictionary];
}
return _labelPropertyDic;
}
@end
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@implementation RjxTopAndBottomTitleModel
@end

人已赞赏
iOS文章

iOS开发截取图片的上面部分作为缩略图

2020-1-24 14:27:06

iOS文章

iOS开发后台持续定位

2020-1-24 16:45:20

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索