UISwitch 添加 标签

给UISwitch添加一个标签。左右滑动时候出现开关标签内容。

代码:

//
//  UISwitch+JGLabel.h
//  JGSwitch
//
//  Created by sl on 15/4/11.
//  Copyright (c) 2015年 Madordie. All rights reserved.
//
//
//  说明:
//      1.给UISwitch添加开关标签
//      2.可根据需要调整标签的相关属性
//      3.类别实现,不用更改代码即可添加
//
//
//  代码出处:http://www.cnblogs.com/madordie/
//
//  思路:
//      1.找到对应View添加两个Label
//

#import <UIKit/UIKit.h>
@interface UISwitch (JGLabel)
/**
*  设置开关标题文字
*      1.根据自己需要设置字体内容、大小、字体颜色等属性。
*      2.不建议在此设置背景颜色,如需设置,调用父类方法设置。
*      3.根据需要可手动调整Label的frame,以使你的Label更加美观。
*/
#pragma mark - 标题
- (UILabel *)onTitle;
- (UILabel *)offTitle;
@end
/**
*备注:
*      1.offTitle对齐方式默认右对齐。
*      2.onTitle默认左对齐
*      3.么有了
*/
//示例代码:
/*
//初始化属性,可IB定制
UISwitch *mySwitch = [[UISwitch alloc] init];
mySwitch.center = self.view.center;
mySwitch.transform = CGAffineTransformMakeScale(2.0f, 2.0f);
[self.view addSubview:mySwitch];
//设置标题属性(当作UILabel使用)
mySwitch.offTitle.text = @"off";
mySwitch.onTitle.text  =@"on";
*/

 

//
//  UISwitch+JGLabel.m
//  JGSwitch
//
//  Created by sl on 15/4/11.
//  Copyright (c) 2015年 Madordie. All rights reserved.
//

#import "UISwitch+JGLabel.h"
#import <objc/runtime.h>
static const void *onTitleKey = &onTitleKey;
static const void *offTitleKey = &offTitleKey;
#define JGGetOnTitle                objc_getAssociatedObject(self, onTitleKey )
#define JGGetOffTitle               objc_getAssociatedObject(self, offTitleKey)
#define JGSetOnTitle(JLabel)        objc_setAssociatedObject(self, onTitleKey,  JLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC)
#define JGSetOffTitle(JLabel)       objc_setAssociatedObject(self, offTitleKey, JLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC)
@implementation UISwitch (JGLabel)
#pragma mark - getter
- (UILabel *)onTitle {
UILabel *_onTitle = JGGetOnTitle;
if (_onTitle) {
return _onTitle;
}
_onTitle = [self JGMakeLabel];
[self.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIView *view = obj;
if ([view isKindOfClass:NSClassFromString(@"_UISwitchInternalViewNeueStyle1")]) {
[view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIView *aview = obj;
if ([aview isKindOfClass:[UIImageView class]]) {
[view insertSubview:_onTitle belowSubview:aview];
}
}];
}
}];
JGSetOnTitle(_onTitle);
return _onTitle;
}
- (UILabel *)offTitle {
UILabel *_offTitle = JGGetOffTitle;
if (_offTitle) {
return _offTitle;
}
_offTitle = [self JGMakeLabel];
[self insertSubview:_offTitle atIndex:0];
[_offTitle setTextAlignment:NSTextAlignmentRight];
JGSetOffTitle(_offTitle);
return _offTitle;
}
#pragma mark - tools
- (UILabel *)JGMakeLabel {
UILabel *label = [[UILabel alloc] init];
[label setBackgroundColor:[UIColor clearColor]];
label.textColor = [UIColor blackColor];
CGRect frame = self.bounds;
frame.origin.x = 4;
frame.size.width -= 8;
label.frame = frame;
return label;
}
@end
UISwitch+JGLabel.m

http://www.cnblogs.com/madordie/p/4416111.html

 

转载于:https://www.cnblogs.com/madordie/p/4416111.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值