因為專案需要在uiswitch按鈕上寫文字,系統自帶的uiswitch是這樣的:
既不能寫字,也不能改顏色,於是在網上找到了這麼乙個自定義的switch按鈕,具體出處找不見了。記錄一下,怕以後找不見了。
先看下效果圖:
按鈕的樣式很多,可以文字,可以寫多行,文字大小和顏色都可以設定。
看下它的原始碼:
#import @inte***ce hmcustomswitch : uislider
@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;
.m檔案
#import "hmcustomswitch.h"
@implementation hmcustomswitch
@synthesize on;
@synthesize tintcolor, clippingview, leftlabel, rightlabel;
+(hmcustomswitch *)switchwithlefttext:(nsstring *)lefttext andright:(nsstring *)righttext
-(id)initwithframe:(cgrect)rect
return self;
}-(void)awakefromnib
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)
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
- (uiimage *)image:(uiimage*)image tintedwithcolor:(uicolor *)tint
else }
-(void)settintcolor:(uicolor*)color }
- (void)seton:(bool)turnon animated:(bool)animated;
if (on)
else
if (animated) }
- (void)seton:(bool)turnon
- (void)endtrackingwithtouch:(uitouch *)touch withevent:(uievent *)event
- (void)touchesbegan:(nsset*)touches withevent:(uievent*)event
- (void)touchesended:(nsset*)touches withevent:(uievent*)event
}-(void)dealloc
@end
看**可以知道,其實它是通過繼承uislider控制項實現的,uislider的左右分別是個uilabel,當yes的時候,滑塊滑到了最右邊,no的時候滑到了最左邊。
如何在**中使用它呢?很簡單:
- (void)loadview
-(void)switchflipped:(hmcustomswitch*)switchview
容芳志 (
本文遵循「署名-非商業用途-保持一致」創作公用協議
ios自定義字型
提供字型檔案路徑,返回所需要字型 複製 uifont customfontwithpath nsstring path size cgfloat size 這樣就不需要在plist設定任何東西,只需要得到字型庫檔案的路徑,就可以取出對應的字型。上面的方法對於ttf otf的字型都有效,但是對於ttc...
iOS 自定義鬧鐘
ios 自定義鬧鐘 需求 從伺服器獲取到一組時間,都設定成鬧鈴,預設是每天迴圈不可修改,同時可以自定義鬧鈴,可選擇週幾迴圈。可以設定開啟關閉或者刪除。一.固定鬧鐘,每天迴圈。很簡單,將需要設定成鬧鐘的時間放入乙個陣列中,通過for迴圈設定成對應的本地推送,詳情見下面的 和注釋。void addloc...
IOS 自定義協議
在自定義協議的標頭檔案 h中 protocol nsdelegate optional 可選 void onoption nsstring pstr required 必須 void onrequired nsstring pstr end inte ce nsclass nsobject id d...