如何自定義PickerView 以及改變字型大小和字型顏色
首先,我們要向系統給我們的PickerView一樣,定義它的代理方法和資料來源方法。
#import <UIKit/UIKit.h>
@classMyPickerView;
//資料來源方法
@protocol UIMyPickerDataSource <NSObject>
@required
- (NSInteger)numberOfComponentsInMyPickerView:(nonnull MyPickerView *)myPickerView;//包含列數
@required
- (NSInteger)myPickerView:(nonnullMyPickerView
@required
- (nullable NSString *)myPickerView:(nonnullMyPickerView *)myPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
@end
//代理方法
@protocol UIMyPickerDelegate <NSObject>
- (void)myPickerView:(nonnull
@end
@interface MyPickerView : UIView
@property (nonatomic,strong)NSMutableArray *_Nonnull ScrolleViewArray;
@property (nonatomic,assign)_Nonnullid <UIMyPickerDataSource> dataSource;
@property (
@end
然後我們來理一下思路。其次我寫的這個是有五行,三列,每列用了5個scrollview。
#import "MyPickerView.h"
@interface MyPickerView()<UIScrollViewDelegate>
@end
@implementation MyPickerView{
NSArray *_dataArray;
NSInteger _components;
CGFloat _height;
CGFloat _width;
CGFloat _componentWidth;
CGFloat _rowHeight;
NSInteger _flagRow;
NSInteger _lastFlagRow;
CGFloat _ScrollRowHeight;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
self = [superinitWithFrame:frame];
if (self) {
_ScrolleViewArray = [[NSMutableArrayalloc]init];
_flagRow = 0;
_lastFlagRow = 0;
self.userInteractionEnabled =YES;
self.clipsToBounds =YES;
_height = 140;
_ScrollRowHeight =_height/5.00;
_width = frame.size.width-60;
self.backgroundColor = [UIColorwhiteColor];
}
return self;
}
-(void)setDataSource:(id<UIMyPickerDataSource>)dataSource{
_dataArray =@[@"元",@"角",@"分"];
_dataSource = dataSource;
_components = [_dataSourcenumberOfComponentsInMyPickerView:self];
_componentWidth = ((_width-_components-1)*100)/(_components*100.00);
UIView *leftView= [[UIViewalloc]initWithFrame:CGRectMake(0,55, 40, 40)];
leftView.backgroundColor = [UIColorcolorWithRed:191/255.0green:215/255.0blue:242/255.0alpha:1];
leftView.layer.cornerRadius =10;
[self addSubview:leftView];
for (NSInteger i =0; i<_components+1; i++) {
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(30+i*(_componentWidth+1),10, 1, _height)];
label.backgroundColor = [UIColorcolorWithRed:127/255.0green:127/255.0blue:127/255.0alpha:0.8];
[self addSubview:label];
if (i!=_components) {
NSInteger _rows = [_dataSourcemyPickerView:selfnumberOfRowInComponent:i];
//NSLog(@"_rows--%ld",_rows);
UIScrollView *bottomScrollView1 = [[UIScrollViewalloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),5, _componentWidth,20)];
//bottomScrollView1.delegate = self;
bottomScrollView1.tag = 501+i*10;
bottomScrollView1.decelerationRate =0.1;
bottomScrollView1.showsHorizontalScrollIndicator =NO;
bottomScrollView1.showsVerticalScrollIndicator =NO;
bottomScrollView1.bounces = NO;
bottomScrollView1.alwaysBounceHorizontal =NO;
bottomScrollView1.alwaysBounceHorizontal =NO;
bottomScrollView1.userInteractionEnabled =NO;
bottomScrollView1.backgroundColor = [UIColorwhiteColor];
bottomScrollView1.contentSize = CGSizeMake(_componentWidth, 20*_rows);
bottomScrollView1.contentOffset = CGPointMake(0, -40);
[self addSubview:bottomScrollView1];
for (NSInteger j =0; j<_rows; j++) {
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,20*j, _componentWidth,20)];
[bottomScrollView1 addSubview:label];
label.tag = 600+(100*i)+j;
label.alpha = 0.3;
label.font = [UIFontsystemFontOfSize:16];
label.textColor = [UIColorcolorWithRed:150/255.0green:150/255.0blue:150/255.0alpha:1];
label.textAlignment = NSTextAlignmentCenter;
label.text = [_dataSourcemyPickerView:selftitleForRow:j forComponent:i];
label.backgroundColor = [UIColorwhiteColor];
}
UIScrollView *middleScrollView1 =[[UIScrollView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),CGRectGetMaxY(bottomScrollView1.frame),_componentWidth, 30)];
//middleScrollView1.delegate = self;
middleScrollView1.tag = 502+i*10;
middleScrollView1.decelerationRate =0.1;
middleScrollView1.showsHorizontalScrollIndicator =NO;
middleScrollView1.showsVerticalScrollIndicator =NO;
middleScrollView1.bounces = NO;
middleScrollView1.alwaysBounceHorizontal =NO;
middleScrollView1.alwaysBounceHorizontal =NO;
middleScrollView1.userInteractionEnabled =NO;
middleScrollView1.backgroundColor = [UIColorwhiteColor];
middleScrollView1.contentSize = CGSizeMake(_componentWidth, 30*(_rows));
middleScrollView1.contentOffset = CGPointMake(0, -30);
[self addSubview:middleScrollView1];
for (NSInteger j =0; j<_rows; j++) {
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,30*j, _componentWidth,30)];
[middleScrollView1 addSubview:label];
label.tag = 600+(100*i)+j+20;
label.alpha = 0.5;
label.textColor = [UIColorcolorWithRed:150/255.0green:150/255.0blue:150/255.0alpha:1];
label.font = [UIFontsystemFontOfSize:25];
label.textAlignment = NSTextAlignmentCenter;
label.text = [_dataSourcemyPickerView:selftitleForRow:j forComponent:i];
label.backgroundColor = [UIColorwhiteColor];
}
UIScrollView *topScrollView =[[ UIScrollView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),55, _componentWidth,40)];
//topScrollView.delegate = self;
topScrollView.tag = 503+i*10;
topScrollView.backgroundColor = [UIColorcolorWithRed:191/255.0green:215/255.0blue:242/255.0alpha:1];
topScrollView.decelerationRate = 0.1;
topScrollView.showsHorizontalScrollIndicator =NO;
topScrollView.showsVerticalScrollIndicator =NO;
topScrollView.bounces = NO;
topScrollView.userInteractionEnabled =NO;
topScrollView.alwaysBounceHorizontal =NO;
topScrollView.alwaysBounceHorizontal =NO;
topScrollView.contentOffset = CGPointMake(0, 0);
topScrollView.contentSize = CGSizeMake(_componentWidth, 40*_rows);
[self addSubview:topScrollView];
for (NSInteger j =0; j<_rows; j++) {
//NSLog(@"j==%ld",j);
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,40*j, _componentWidth,40)];
[topScrollView addSubview:label];
label.tag = 600+(100*i)+j+40;
label.textColor = [UIColorcolorWithRed:36/255.0green:119/255.0blue:212/255.0alpha:1];
label.font = [UIFontsystemFontOfSize:34];
label.backgroundColor = [UIColorcolorWithRed:191/255.0green:215/255.0blue:242/255.0alpha:0];
label.textAlignment = NSTextAlignmentCenter;
label.text = [_dataSourcemyPickerView:selftitleForRow:jforComponent:i];//[NSString stringWithFormat:@"%ld",label.tag];//
}
UILabel *unitLabel = [[UILabelalloc]initWithFrame:CGRectMake(CGRectGetMaxX(topScrollView.frame)-25,CGRectGetMaxY(topScrollView.frame)-20,20, 20)];
[self addSubview:unitLabel];
unitLabel.text = _dataArray[i];
//unitLabel.backgroundColor = [UIColor whiteColor];
unitLabel.textColor = [UIColorcolorWithRed:36/255.0green:119/255.0blue:212/255.0alpha:1];
unitLabel.font = [UIFontboldSystemFontOfSize:16];
UIScrollView *middleScrollView2 =[[UIScrollView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),CGRectGetMaxY(topScrollView.frame),_componentWidth, 30)];
//middleScrollView2.delegate = self;
middleScrollView2.tag = 504+i*10;
middleScrollView2.decelerationRate =0.1;
middleScrollView2.showsHorizontalScrollIndicator =NO;
middleScrollView2.showsVerticalScrollIndicator =NO;
middleScrollView2.bounces = NO;
middleScrollView2.alwaysBounceHorizontal =NO;
middleScrollView2.alwaysBounceHorizontal =NO;
middleScrollView2.userInteractionEnabled =NO;
middleScrollView2.backgroundColor = [UIColorwhiteColor];
middleScrollView2.contentOffset = CGPointMake(0, 30);
[self addSubview:middleScrollView2];
middleScrollView2.contentSize = CGSizeMake(_componentWidth, 30*(_rows));
for (NSInteger j =0; j<_rows; j++) {
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,30*j, _componentWidth,30)];
[middleScrollView2 addSubview:label];
label.tag = 600+(100*i)+j+60;
label.alpha = 0.5;
label.font = [UIFontsystemFontOfSize:25];
label.textColor = [UIColorcolorWithRed:150/255.0green:150/255.0blue:150/255.0alpha:1];
label.textAlignment = NSTextAlignmentCenter;
label.text = [_dataSourcemyPickerView:selftitleForRow:j forComponent:i];
label.backgroundColor = [UIColorwhiteColor];
}
//27376
UIScrollView *bottomScrollView2 = [[UIScrollViewalloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),CGRectGetMaxY(middleScrollView2.frame),_componentWidth, 20)];
//bottomScrollView2.delegate = self;
bottomScrollView2.tag = 505+i*10;
bottomScrollView2.decelerationRate =0.1;
bottomScrollView2.showsHorizontalScrollIndicator =NO;
bottomScrollView2.showsVerticalScrollIndicator =NO;
bottomScrollView2.bounces = NO;
bottomScrollView2.alwaysBounceHorizontal =NO;
bottomScrollView2.alwaysBounceHorizontal =NO;
bottomScrollView2.userInteractionEnabled =NO;
bottomScrollView2.backgroundColor = [UIColorwhiteColor];
bottomScrollView2.contentOffset = CGPointMake(0, 40);
bottomScrollView2.contentSize = CGSizeMake(_componentWidth, 20*_rows);
[self addSubview:bottomScrollView2];
for (NSInteger j =0; j<_rows; j++) {
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,20*j, _componentWidth,20)];
[bottomScrollView2 addSubview:label];
label.tag = 600+(100*i)+j+80;
label.alpha = 0.3;
label.textColor = [UIColorcolorWithRed:150/255.0green:150/255.0blue:150/255.0alpha:1];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFontsystemFontOfSize:16];
label.text = [_dataSourcemyPickerView:selftitleForRow:j forComponent:i];
label.backgroundColor = [UIColorwhiteColor];
}
UIScrollView *_scrollView= [[UIScrollViewalloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),0, _componentWidth,_height)];
_scrollView.userInteractionEnabled =YES;
[self addSubview:_scrollView];
_scrollView.tag = 506+i*10;
_scrollView.decelerationRate = 0.01;
//_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator =NO;
_scrollView.bounces = NO;
_scrollView.contentSize = CGSizeMake(_componentWidth, 40*(_rows+2.5));
_scrollView.contentOffset = CGPointMake(0, 0);
_scrollView.delegate = self;
NSArray *array = @[bottomScrollView1,middleScrollView1,topScrollView,middleScrollView2,bottomScrollView2];
[_ScrolleViewArray addObject:array];
}
if(i==_components-1){
UIView *rightView= [[UIViewalloc]initWithFrame:CGRectMake(_width+20,55, 40, 40)];
rightView.backgroundColor = [UIColorcolorWithRed:191/255.0green:215/255.0blue:242/255.0alpha:1];
rightView.layer.cornerRadius =10;
[self addSubview:rightView];
}
}
}
- (void)setDelegate:(id<UIMyPickerDelegate>)delegate{
_delegate = delegate;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat offSet = scrollView.contentOffset.y;
CGFloat _offset = offSet/40.00;
_flagRow = (NSInteger)(offSet/40.00+0.5);
if (_flagRow!=_lastFlagRow) {
_lastFlagRow =_flagRow;
// NSLog(@"_flagRow%ld",_flagRow);
}
//NSLog(@"tag_%ld",scrollView.tag);
NSInteger flag = scrollView.tag-500;
NSInteger componentFlag = flag/10;
NSArray *array = (NSArray *)_ScrolleViewArray[componentFlag];
for (NSInteger i=0; i<5; i++) {
UIScrollView *scrollView = (UIScrollView *)array[i];
if (i==0){
scrollView.contentOffset = CGPointMake(0, 20*_offset-40);
}
if(i==1) {
scrollView.contentOffset = CGPointMake(0, 30*_offset-30);
}
if (i==2){
scrollView.contentOffset = CGPointMake(0, offSet);
}
if(i==3) {
scrollView.contentOffset = CGPointMake(0, 30*_offset+30);
}
if(i==4){
scrollView.contentOffset = CGPointMake(0, 20*_offset+40);
}
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"停止拖拽");
[self setUpDelegateWith:scrollView];
相關推薦
如何自定義PickerView 以及改變字型大小和字型顏色
首先,我們要向系統給我們的PickerView一樣,定義它的代理方法和資料來源方法。 #import <UIKit/UIKit.h> @classMyPickerView; //資料來源方法 @protocol UIMyPickerDataSou
同一個label顯示不同的字型大小和字型顏色
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 40)]; [self.view addSubview:label]; //指定位置 NSMutableAttributedString *
iconfont的使用,阿里向量相簿的引用,配置,改變圖示大小和圖示顏色
怎麼使用iconfont首先找到自己想要的圖示,新增到購物車如果你只要一個圖示的話,或者確保之後這個專案不需要其他的圖示的話,可以直接選擇下載程式碼,但是你有多個圖示的話,最好選擇新增至專案無論是新增專案還是直接下載程式碼,下載之後會有一個download.zip包,解壓之後
[紹棠] iOS設定Label上顯示不同字型大小和字型顏色
一, 一個label上顯示不同的字型大小 NSString *needText = @"個人訊息(11)"; [topLabel setAttributedTe
Android AlertDialog修改標題、內容、按鈕的字型大小和字型顏色
“字型要大、顏色要鮮豔”,這話聽著熟悉吧,在日常開發中,往往因為業務的不同、受眾群體的特殊,可能需要我們做出特殊的處理。 今天是對原生AlertDialog做一些大小和顏色的修改。 有兩種方案:
mfc怎麼動態載入時間以及改變字型大小和顏色。
第一次寫 留著備份 以後可能還會用到 動態載入時間 1.在OnInitDialog() 初始化函式中加入 SetTimer(1,1000,NULL); 2.增加訊息函式 OnTimer(UINT nIDEvent) 增加以下函式 CTime
DOM內容操作和自定義、樣式改變
abcdefg result 定義 tel class abcde inner 參數 fun 自定義 function 方法名或函數名(參數1,參數2,、、、) { 方法體; return返回值;(可不寫) } function a
Map集合的遍歷方式以及TreeMap集合儲存自定義物件實現比較的Comparable和Comparator兩種方式
Map集合的特點 1、Map集合中儲存的都是鍵值對,鍵和值是一一對應的 2、一個對映不能包含重複的值 3、每個鍵最多隻能對映到一個值上 Map介面和Collection介面的不同 Map是雙列集合的根介面,Collection是單列集合的根介面 1、Map是雙列的(是雙列集合的根介
java自定義異常以及throw和throws關鍵字
java中內建了很多可能在程式設計時出現的大部分異常。除此之外,使用者可以使用一個類繼承Exception類即可繼承自定義類異常。 在程式中自定義類,大體上分為以下幾個步驟: 建立自定義異常類 在方法中通過throw關鍵字丟擲異常物件。 如果在當前丟擲異常的方法中處理異常,
android開發:在不同解析度,不同螢幕密度dpi的平板上的字型大小和佈局的自適應
最近在這家公司做一些android裝置上的開發,使用了幾款平板進行真機除錯,面對不同解析度,不同螢幕密度dpi的裝置,碰到了一些和虛擬機器不太一樣的情況,在這裡和大家分享一下。 1.一些名詞的簡單理解,後面會有詳細說明 (1)螢幕尺寸:單位英寸(i
springboot下全域性異常處理和自定義異常以及自定義返回值
1.介紹 全域性異常處理,是mvc的一個新特性,避免大量的重複程式碼 自定義異常 , 方便使用,丟擲自己定義好的異常,配合全域性異常處理使用,效果更佳 自定義返回值,類似異常,返回自己定義的返回值 2.全域性異常處理重點知識 1. 新建一
Android 自定義TextView實現文字內容自動調整字型大小以適應TextView的大小
/** * 自定義TextView,文字內容自動調整字型大小以適應TextView的大小 * @author yzp */ public class AutoFitTextView extends TextView { private
iOS UILabel改變指定字型大小和顏色
改變指定字型的顏色,range:NSMakeRange(4, 4)改變字型的位置和個數 self.enrollLabel self.person_name 為建立UILabel的物件名字
自己實現的資料表格控制元件(dataTable),支援自定義樣式和標題資料、ajax等各種自定義設定以及分頁自定義
一、前言 也沒什麼好說的嘛,用了蠻多github上開源的能夠實現dataTable功能的表格外掛,不過都預設繫結樣式啊,資料格式也設定的比較死,所以忍不住自己實現了一個簡單的可自定義樣式和自定義資料返回格式的資料表格外掛,原生js是保留的,後面如果更新新版本的話會去除對jq
php自定義函數: 文件大小轉換成智能形式
elseif pos byte light orm files brush 轉換成 else function format_byte($filesize) { if($filesize >= 1073741824) { $filesize
VS下WPF自定義控件的基本步驟和基本代碼實現
emp don 謝謝 generic 管理器 參數 bubuko 類的屬性 typeof 一、自定義控件的基本步驟: (本示例項目名稱為:W;添加的自定義控件名稱為) 1、 在“解決方案資源管理器”窗口的項目名上: 右擊à添加à新建項(Ctrl+Shift+A)
springboot之自定義屬性以及亂碼三
自定義屬性的使用(讀取配置檔案,在專案啟動的時候根據@Value去配置檔案中獲取屬性) 在建好的springboot專案properties屬性中自定義屬性,如下: 通過@Value獲取自定義屬性 @Value("${name}") 啟動專案:
JS 自定義sleep以及Ajax 自執行函式
<script src="/static/js/jquery-2.1.1.min.js"></script> <script> function sleep(numberMillis) { var now = new D
[UWP]為附加屬性和依賴屬性自定義程式碼段(相容UWP和WPF)
1. 前言 之前介紹過依賴屬性和附加屬性的程式碼段,這兩個程式碼段我用了很多年,一直都幫了我很多。不過這兩個程式碼段我也多年沒修改過,Resharper老是提示我生成的程式碼可以修改,它這麼有誠意,這次就只好從了它,順便簡單介紹下怎麼自定義程式碼段。 2. VisualStudio自帶程式碼段的問題 以
TextView 動態設定字型大小和加粗
//字型大小為16,並且加粗 tv_1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); tv_1.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));