UISwitch 添加 标签
生活随笔
收集整理的這篇文章主要介紹了
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.mhttp://www.cnblogs.com/madordie/p/4416111.html
?
轉載于:https://www.cnblogs.com/madordie/p/4416111.html
總結
以上是生活随笔為你收集整理的UISwitch 添加 标签的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 10大iOS开发者最喜爱的类库
- 下一篇: 4. 星际争霸之php设计模式--工厂方