1. 程式人生 > >iOS自定義的UISwitch按鈕

iOS自定義的UISwitch按鈕

轉載地址:http://blog.csdn.net/totogo2010/article/details/8373541

這是一位大牛,喜歡看部落格的新手不要錯過了。

因為專案需要在UISwitch按鈕上寫文字,系統自帶的UISwitch是這樣的:

既不能寫字,也不能改顏色,於是在網上找到了這麼一個自定義的Switch按鈕,具體出處找不見了。記錄一下,怕以後找不見了。

先看下效果圖:


按鈕的樣式很多,可以文字,可以寫多行,文字大小和顏色都可以設定。

看下它的原始碼:

  1. #import <Foundation/Foundation.h>
  2. @interface HMCustomSwitch : UISlider {  
  3.     BOOL on;  
  4.     UIColor *tintColor;  
  5.     UIView *clippingView;  
  6.     UILabel *rightLabel;  
  7.     UILabel *leftLabel;  
  8.     // private member
  9.     BOOL m_touchedSelf;  
  10. }  
  11. @property(nonatomic,getter=isOn) BOOL on;  
  12. @property (nonatomic,retain) UIColor *tintColor;  
  13. @property (nonatomic,retain) UIView *clippingView;  
  14. @property (nonatomic,retain) UILabel *rightLabel;  
  15. @property (nonatomic,retain) UILabel *leftLabel;  
  16. + (HMCustomSwitch *) switchWithLeftText: (NSString *) tag1 andRight: (NSString *) tag2;  
  17. - (void)setOn:(BOOL)on animated:(BOOL)animated;  
.m檔案
  1. #import "HMCustomSwitch.h"
  2. @implementation HMCustomSwitch  
  3. @synthesize on;  
  4. @synthesize tintColor, clippingView, leftLabel, rightLabel;  
  5. +(HMCustomSwitch *)switchWithLeftText:(NSString *)leftText andRight:(NSString *)rightText  
  6. {  
  7.     HMCustomSwitch *switchView = [[HMCustomSwitch alloc] initWithFrame:CGRectZero];  
  8.     switchView.leftLabel.text = leftText;  
  9.     switchView.rightLabel.text = rightText;  
  10.     return [switchView autorelease];  
  11. }  
  12. -(id)initWithFrame:(CGRect)rect  
  13. {  
  14.     if ((self=[super initWithFrame:CGRectMake(rect.origin.x,rect.origin.y,95,27)]))  
  15.     {  
  16.         //      self.clipsToBounds = YES;
  17.         [self awakeFromNib];        // do all setup in awakeFromNib so that control can be created manually or in a nib file
  18.     }  
  19.     return self;  
  20. }  
  21. -(void)awakeFromNib  
  22. {  
  23.     [super awakeFromNib];  
  24.     self.backgroundColor = [UIColor clearColor];  
  25.     [self setThumbImage:[UIImage imageNamed:@"switchThumb.png"] forState:UIControlStateNormal];  
  26.     [self setMinimumTrackImage:[UIImage imageNamed:@"switchBlueBg.png"] forState:UIControlStateNormal];  
  27.     [self setMaximumTrackImage:[UIImage imageNamed:@"switchOffPlain.png"] forState:UIControlStateNormal];  
  28.     self.minimumValue = 0;  
  29.     self.maximumValue = 1;  
  30.     self.continuous = NO;  
  31.     self.on = NO;  
  32.     self.value = 0.0;  
  33.     self.clippingView = [[UIView alloc] initWithFrame:CGRectMake(4,2,87,23)];  
  34.     self.clippingView.clipsToBounds = YES;  
  35.     self.clippingView.userInteractionEnabled = NO;  
  36.     self.clippingView.backgroundColor = [UIColor clearColor];  
  37.     [self addSubview:self.clippingView];  
  38.     [self.clippingView release];  
  39.     NSString *leftLabelText = NSLocalizedString(@"ON","Custom UISwitch ON label. If localized to empty string then I/O will be used");  
  40.     if ([leftLabelText length] == 0)      
  41.     {  
  42.         leftLabelText = @"l";       // use helvetica lowercase L to be a 1. 
  43.     }  
  44.     self.leftLabel = [[UILabel alloc] init];  
  45.     self.leftLabel.frame = CGRectMake(0, 0, 48, 23);  
  46.     self.leftLabel.text = leftLabelText;  
  47.     self.leftLabel.textAlignment = NSTextAlignmentCenter;  
  48.     self.leftLabel.font = [UIFont boldSystemFontOfSize:17];  
  49.     self.leftLabel.textColor = [UIColor whiteColor];  
  50.     self.leftLabel.backgroundColor = [UIColor clearColor];  
  51.     //      self.leftLabel.shadowColor = [UIColor redColor];
  52.     //      self.leftLabel.shadowOffset = CGSizeMake(0,0);
  53.     [self.clippingView addSubview:self.leftLabel];  
  54.     [self.leftLabel release];  
  55.     NSString *rightLabelText = NSLocalizedString(@"OFF","Custom UISwitch OFF label. If localized to empty string then I/O will be used");  
  56.     if ([rightLabelText length] == 0)     
  57.     {  
  58.         rightLabelText = @"O";  // use helvetica uppercase o to be a 0. 
  59.     }  
  60.     self.rightLabel = [[UILabel alloc] init];  
  61.     self.rightLabel.frame = CGRectMake(95, 0, 48, 23);  
  62.     self.rightLabel.text = rightLabelText;  
  63.     self.rightLabel.textAlignment = NSTextAlignmentCenter;  
  64.     self.rightLabel.font = [UIFont boldSystemFontOfSize:17];  
  65.     self.rightLabel.textColor = [UIColor grayColor];  
  66.     self.rightLabel.backgroundColor = [UIColor clearColor];  
  67.     //      self.rightLabel.shadowColor = [UIColor redColor];
  68.     //      self.rightLabel.shadowOffset = CGSizeMake(0,0);
  69.     [self.clippingView addSubview:self.rightLabel];  
  70.     [self.rightLabel release];  
  71. }  
  72. -(void)layoutSubviews  
  73. {  
  74.     [super layoutSubviews];  
  75.     //  NSLog(@"leftLabel=%@",NSStringFromCGRect(self.leftLabel.frame));
  76.     // move the labels to the front
  77.     [self.clippingView removeFromSuperview];  
  78.     [self addSubview:self.clippingView];  
  79.     CGFloat thumbWidth = self.currentThumbImage.size.width;  
  80.     CGFloat switchWidth = self.bounds.size.width;  
  81.     CGFloat labelWidth = switchWidth - thumbWidth;  
  82.     CGFloat inset = self.clippingView.frame.origin.x;  
  83.     //  NSInteger xPos = self.value * (self.bounds.size.width - thumbWidth) - (self.leftLabel.frame.size.width - thumbWidth/2); 
  84.     NSInteger xPos = self.value * labelWidth - labelWidth - inset;  
  85.     self.leftLabel.frame = CGRectMake(xPos, 0, labelWidth, 23);  
  86.     //  xPos = self.value * (self.bounds.size.width - thumbWidth) + (self.rightLabel.frame.size.width - thumbWidth/2); 
  87.     xPos = switchWidth + (self.value * labelWidth - labelWidth) - inset;   
  88.     self.rightLabel.frame = CGRectMake(xPos, 0, labelWidth, 23);  
  89.     //  NSLog(@"value=%f    xPos=%i",self.value,xPos);
  90.     //  NSLog(@"thumbWidth=%f    self.bounds.size.width=%f",thumbWidth,self.bounds.size.width);
  91. }  
  92. - (UIImage *)image:(UIImage*)image tintedWithColor:(UIColor *)tint   
  93. {     
  94.     if (tint != nil)   
  95.     {  
  96.         UIGraphicsBeginImageContext(image.size);  
  97.         //draw mask so the alpha is respected
  98.         CGContextRef currentContext = UIGraphicsGetCurrentContext();  
  99.         CGImageRef maskImage = [image CGImage];  
  100.         CGContextClipToMask(currentContext, CGRectMake(0, 0, image.size.width, image.size.height), maskImage);  
  101.         CGContextDrawImage(currentContext, CGRectMake(0,0, image.size.width, image.size.height), image.CGImage);  
  102.         [image drawAtPoint:CGPointMake(0,0)];  
  103.         [tint setFill];  
  104.         UIRectFillUsingBlendMode(CGRectMake(0,0,image.size.width,image.size.height),kCGBlendModeColor);  
  105.         UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
  106.         UIGraphicsEndImageContext();  
  107.         return newImage;  
  108.     }  
  109.     else
  110.     {  
  111.         return image;  
  112.     }  
  113. }  
  114. -(void)setTintColor:(UIColor*)color  
  115. {  
  116.     

    相關推薦

    ios定義返回按鈕後沒有滑動返回手勢處理方法

    A push B push C在C中不需要加以下程式碼 在A檢視中增加下面程式碼 -(void)viewDidAppear:(BOOL)animated{ [super viewDidApp

    iOS 定義返回按鈕錯位問題(備忘)

    UIButton *button1 = [FGWButtoncreatButtonWithtarget:selfsel:@selector(press)]; UIBarButtonItem *left

    ios 定義返回按鈕時,如何新增手勢返回

    在父檢視上加上如下程式碼: self.navigationItem.hidesBackButton = YES;  self.navigationController.interactivePopGestureRecognizer.delegate = self;  sel

    ios 定義返回按鈕側滑失效完美解決方案

    其實很簡單很簡單,只需要新增下面這一句程式碼即可: self.navigationController.interactivePopGestureRecognizer.delegate = (id)s

    IOS定義UISwitch

    原創Blog,轉載請註明出處 下午的時候閒著無聊,簡單想了想,用三個UILabel來實現這個簡單的自定義UISwitch 效果圖,                                  當然,有些粗糙,後續有時間了我會把介面優化下。直接拿去用估計介面粗糙了點

    iOS定義UISwitch按鈕

    轉載地址:http://blog.csdn.net/totogo2010/article/details/8373541 這是一位大牛,喜歡看部落格的新手不要錯過了。 因為專案需要在UISwitch按鈕上寫文字,系統自帶的UISwitch是這樣的:

    IOS 定義按鈕(代碼實現)+九宮格

    uifont 排列 end uiview height iyu void rec name 在一些下載應用裏整個頁面都是按鈕,有好多好多,但是仔細觀察不難發現他們很有規律。就像下面一樣?? 很有規律的排列在屏幕上,那麽這需要我們怎麽去做能。 正如標題,我們需要了解兩個知

    iOS定義導航按鈕UIBarButtonItem的樣式

    在一個APP中導航的重要性和方便性自然不需要多說了,由於系統的導航用起來實在不怎麼友好,一直想抽個時間把導航學習下 由於投入到工作的時間多些,懶懶散散的一直都是用的時候才去找度娘,一直沒來個總結,前段時間在群裡和別人討論的 時候我說自定義導航不就是隱藏系統的,自己新增一個U

    iOS定義控制元件之下拉列表按鈕

    本文將介紹如何實現一個簡單的下拉列表按鈕,使讀者能夠更進一步掌握UIButton類和UITableView類的使用,瞭解非iOS系統自帶控制元件的初始化方法,學習列表下拉、收起的動畫效果,編寫可重用的DropDownButton類,還可以對此進行優化、擴充套件和整理,便於以

    iOS 定義鍵盤收回按鈕

    效果圖: 1.自定義一個TextField繼承自UITextField: .h檔案 #import <UIKit/UIKit.h> @interface AMPTextField

    iOS 一一 定義cell按鈕的點選事件(通知機制)

    使用通知機制來實現 自定義cell按鈕的點選事件.使用通知機制來實現,沒有使用代理的方式規範. 1. 當點選cell上面的按鈕. 釋出通知 2. 在控制器的viewDidLoad方法中監聽通知.實現監聽通知的方法 3. 移除通知 程式碼如下: ZYOperatio

    iOS-定義單選複選按鈕

    我們先實現單個按鈕,為了複用,不管單選還是複選按鈕都是使用同一個類來實現,為了區別單選還是複選,我們用一個自定義列舉型別CheckButtonStyle屬性style來區別,當其值設定為CheckButtonStyleDefault或CheckButtonStyleBo

    iOS定義可拖動帶點選效果的懸浮按鈕

       實現方法是自定義一個UIView,在UIView上新增拖動手勢(UIPanGestureRecognizer)和點選手勢(UITapGestureRecognizer). - (instancetype) initWithFrame:(CGRect)frame

    ios 定義鍵盤的return鍵以及鍵盤的其他一些屬性

    variable 位置 arch ext ddr gin character 觸發 hone //初始化textfield並設置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20

    導航欄使用UIButton定義返回按鈕的圖片

    idl smi uic uiimage clas ini anim literal 使用 - (void)viewDidLoad { UIButton *backItem = [UIButton buttonWithType:UIButtonTypeCustom];

    iOS定義一些提示控件

    mat cat get -1 from start mask ins sel 代碼如下: .h中的代碼: // // HKUIToolsView.h // HKUIToolsDemo // // Created by isHakan on 2017/7/28

    Jquery mobile 定義 返回按鈕 data-rel="back"

    pla ole view student ajax ati source -i alt data-rel="back" 第一個頁面 主頁面 studentmaster.html 通過下面js腳本跳轉到詳情頁面 window.location.href="student

    iOS - 定義 iPhone 鈴聲

    mar 資料庫 phone 右鍵 還需要 到手 文件名 頁面 fin 1、iPhone 鈴聲格式 iPhone 的來電鈴聲時長限制為 40 秒,短信鈴聲時長限制為 25 秒,且 iOS5 及以上的系統才支持 m4r 格式的短信鈴聲。 2、自定義 iPhone 鈴聲 1

    C#定義Button按鈕控件

    pre span 用戶 gif mov 設置 color 繪制 tex C#自定義Button按鈕控件 在實際開發中經常可以遇到有的控件並不一定可以滿足需要,因此需要自定義開發,這裏做了一個簡單的按鈕控件,特意帖上來,如有不足之處請見諒! 按鈕素材: 這裏截圖

    後臺文章編輯器的可視區域添加定義功能按鈕

    nload rac IT 樣式 admin lte size gist != 有時候我們需要自定義一些樣式或者功能,要麽就是直接在Text區域直接寫html代碼, 還有一種方法就是直接將自定義一些樣式或者功能添加到後臺文章編輯器的 可視區域。具體實現方式如下: 首先在對應的