iOS自定義的UISwitch按鈕
轉載地址:http://blog.csdn.net/totogo2010/article/details/8373541
這是一位大牛,喜歡看部落格的新手不要錯過了。
因為專案需要在UISwitch按鈕上寫文字,系統自帶的UISwitch是這樣的:
既不能寫字,也不能改顏色,於是在網上找到了這麼一個自定義的Switch按鈕,具體出處找不見了。記錄一下,怕以後找不見了。
先看下效果圖:
按鈕的樣式很多,可以文字,可以寫多行,文字大小和顏色都可以設定。
看下它的原始碼:
-
#import <Foundation/Foundation.h>
- @interface HMCustomSwitch : UISlider {
- BOOL on;
- UIColor *tintColor;
- UIView *clippingView;
- UILabel *rightLabel;
- UILabel *leftLabel;
- // private member
- BOOL m_touchedSelf;
- }
- @property(nonatomic,getter=isOn) BOOL on;
-
@property (nonatomic,retain) UIColor *tintColor;
- @property (nonatomic,retain) UIView *clippingView;
- @property (nonatomic,retain) UILabel *rightLabel;
- @property (nonatomic,retain) UILabel *leftLabel;
- + (HMCustomSwitch *) switchWithLeftText: (NSString *) tag1 andRight: (NSString *) tag2;
-
- (void)setOn:(BOOL)on animated:(BOOL)animated;
- #import "HMCustomSwitch.h"
- @implementation HMCustomSwitch
- @synthesize on;
- @synthesize tintColor, clippingView, leftLabel, rightLabel;
- +(HMCustomSwitch *)switchWithLeftText:(NSString *)leftText andRight:(NSString *)rightText
- {
- HMCustomSwitch *switchView = [[HMCustomSwitch alloc] initWithFrame:CGRectZero];
- switchView.leftLabel.text = leftText;
- switchView.rightLabel.text = rightText;
- return [switchView autorelease];
- }
- -(id)initWithFrame:(CGRect)rect
- {
- if ((self=[super initWithFrame:CGRectMake(rect.origin.x,rect.origin.y,95,27)]))
- {
- // self.clipsToBounds = YES;
- [self awakeFromNib]; // do all setup in awakeFromNib so that control can be created manually or in a nib file
- }
- return self;
- }
- -(void)awakeFromNib
- {
- [super awakeFromNib];
- self.backgroundColor = [UIColor clearColor];
- [self setThumbImage:[UIImage imageNamed:@"switchThumb.png"] forState:UIControlStateNormal];
- [self setMinimumTrackImage:[UIImage imageNamed:@"switchBlueBg.png"] forState:UIControlStateNormal];
- [self setMaximumTrackImage:[UIImage imageNamed:@"switchOffPlain.png"] forState:UIControlStateNormal];
- self.minimumValue = 0;
- self.maximumValue = 1;
- self.continuous = NO;
- self.on = NO;
- self.value = 0.0;
- self.clippingView = [[UIView alloc] initWithFrame:CGRectMake(4,2,87,23)];
- self.clippingView.clipsToBounds = YES;
- self.clippingView.userInteractionEnabled = NO;
- self.clippingView.backgroundColor = [UIColor clearColor];
- [self addSubview:self.clippingView];
- [self.clippingView release];
- NSString *leftLabelText = NSLocalizedString(@"ON","Custom UISwitch ON label. If localized to empty string then I/O will be used");
- if ([leftLabelText length] == 0)
- {
- leftLabelText = @"l"; // use helvetica lowercase L to be a 1.
- }
- self.leftLabel = [[UILabel alloc] init];
- self.leftLabel.frame = CGRectMake(0, 0, 48, 23);
- self.leftLabel.text = leftLabelText;
- self.leftLabel.textAlignment = NSTextAlignmentCenter;
- self.leftLabel.font = [UIFont boldSystemFontOfSize:17];
- self.leftLabel.textColor = [UIColor whiteColor];
- self.leftLabel.backgroundColor = [UIColor clearColor];
- // self.leftLabel.shadowColor = [UIColor redColor];
- // self.leftLabel.shadowOffset = CGSizeMake(0,0);
- [self.clippingView addSubview:self.leftLabel];
- [self.leftLabel release];
- NSString *rightLabelText = NSLocalizedString(@"OFF","Custom UISwitch OFF label. If localized to empty string then I/O will be used");
- if ([rightLabelText length] == 0)
- {
- rightLabelText = @"O"; // use helvetica uppercase o to be a 0.
- }
- self.rightLabel = [[UILabel alloc] init];
- self.rightLabel.frame = CGRectMake(95, 0, 48, 23);
- self.rightLabel.text = rightLabelText;
- self.rightLabel.textAlignment = NSTextAlignmentCenter;
- self.rightLabel.font = [UIFont boldSystemFontOfSize:17];
- self.rightLabel.textColor = [UIColor grayColor];
- self.rightLabel.backgroundColor = [UIColor clearColor];
- // self.rightLabel.shadowColor = [UIColor redColor];
- // self.rightLabel.shadowOffset = CGSizeMake(0,0);
- [self.clippingView addSubview:self.rightLabel];
- [self.rightLabel release];
- }
- -(void)layoutSubviews
- {
- [super layoutSubviews];
- // NSLog(@"leftLabel=%@",NSStringFromCGRect(self.leftLabel.frame));
- // move the labels to the front
- [self.clippingView removeFromSuperview];
- [self addSubview:self.clippingView];
- CGFloat thumbWidth = self.currentThumbImage.size.width;
- CGFloat switchWidth = self.bounds.size.width;
- CGFloat labelWidth = switchWidth - thumbWidth;
- CGFloat inset = self.clippingView.frame.origin.x;
- // NSInteger xPos = self.value * (self.bounds.size.width - thumbWidth) - (self.leftLabel.frame.size.width - thumbWidth/2);
- NSInteger xPos = self.value * labelWidth - labelWidth - inset;
- self.leftLabel.frame = CGRectMake(xPos, 0, labelWidth, 23);
- // xPos = self.value * (self.bounds.size.width - thumbWidth) + (self.rightLabel.frame.size.width - thumbWidth/2);
- xPos = switchWidth + (self.value * labelWidth - labelWidth) - inset;
- self.rightLabel.frame = CGRectMake(xPos, 0, labelWidth, 23);
- // NSLog(@"value=%f xPos=%i",self.value,xPos);
- // NSLog(@"thumbWidth=%f self.bounds.size.width=%f",thumbWidth,self.bounds.size.width);
- }
- - (UIImage *)image:(UIImage*)image tintedWithColor:(UIColor *)tint
- {
- if (tint != nil)
- {
- UIGraphicsBeginImageContext(image.size);
- //draw mask so the alpha is respected
- CGContextRef currentContext = UIGraphicsGetCurrentContext();
- CGImageRef maskImage = [image CGImage];
- CGContextClipToMask(currentContext, CGRectMake(0, 0, image.size.width, image.size.height), maskImage);
- CGContextDrawImage(currentContext, CGRectMake(0,0, image.size.width, image.size.height), image.CGImage);
- [image drawAtPoint:CGPointMake(0,0)];
- [tint setFill];
- UIRectFillUsingBlendMode(CGRectMake(0,0,image.size.width,image.size.height),kCGBlendModeColor);
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newImage;
- }
- else
- {
- return image;
- }
- }
- -(void)setTintColor:(UIColor*)color
- {
-
相關推薦
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代碼, 還有一種方法就是直接將自定義一些樣式或者功能添加到後臺文章編輯器的 可視區域。具體實現方式如下: 首先在對應的