寫在開頭
標題中列出的4個控件是學習UI過程中最先接觸到的幾個控件,經常看系統頭文件或者API的童鞋應該發現了,這四個控件都沒有初始化方法! 這是多么神奇的一件事!那么它們如何初始化? 或者說如何創建它們?
OC三大特性之一的繼承是這一切的元兇。
UILabel,UIIMageView都是直接繼承于UIView,UIButton和UITextField繼承于UIControll,UIControll繼承于UIView,UIView為我們提供了一個初始化的方法:
- (instancetype)initWithFrame:(CGRect)aRect
也就是說,這4個控件都可以通過上述方法來初始化,以下對4個控件的初始化將不再贅述
一. UIButton
1. UIButton提供了一個構造器方法:
+ (instancetype)buttonWithType:(UIButtonType)buttonType
其中的buttonType參數是一個枚舉值,如下所示:
typedef enum {
UIButtonTypeCustom = 0, 自定義風格
UIButtonTypeSystem, 系統的風格
UIButtonTypeRoundedRect, 圓角矩形
UIButtonTypeDetailDisclosure, 藍色小箭頭按鈕,主要做詳細說明用
UIButtonTypeInfoLight, 亮色感嘆號
UIButtonTypeInfoDark, 暗色感嘆號
UIButtonTypeContactAdd, 加號添加按鈕
} UIButtonType;
2. 接下來再說說UIButton的構成
titleLabel Property
imageView Property
這兩個屬性相信寶寶們都注意到了,titleLabel屬性表示按鈕的標題,這個很容易理解,可是UIButton的imageView屬性是個什么鬼!這就要說到UIButton的神奇構成了,UIButton是由imageView和label構成的,具體的請看下圖:
現在來詳細說一下這張圖
藍色的部分代碼如下:
button.backgroundColor = [UIColor cyanColor];//給UIButton設置背景顏色
這里又要提到神奇的繼承啦,沒錯,UIButton的backgroundColor屬性是從UIView一路繼承下來的,UIView作為UILabel和UIImageView的爸爸,UIButton和UITextField的爺爺,有沒有感覺它很無解......
紅色的文字本分就是一個UILabel,給UIButton設置標題的代碼如下:
[button setTitle:@"這里是label哦" forState:UIControlStateNormal];
文字左邊的圖片就是鋪在UIButton的imageView上面的,代碼如下:
[button setImage:imageObject forState:UIControlStateNormal];
3. 設置UIButton的標題字體大小
button.titleLabel.font = [UIFont systemFontOfSize:30];
4. UIButton點擊事件
相信很多童鞋都聽說過Button點擊事件,可是UIButton的頭文件里并沒有相關的方法,這是怎么回事呢?對的,沒錯,神奇的繼承又來了,UIButton繼承于UIControll,UIControll的頭文件中有相關方法的聲明,即:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
也就是說,UIButton可以通過這個方法來添加點擊事件,同時也只有UIControll的子類才有這個添加事件的方法。
二. UILabel(負責顯示文本)
1. UILabel 的屬性
// 1.text文本
label.text = @"這就是一個UILabel";
// 2.textColor文本顏色
label.textColor = [UIColor redColor];
// 3.font 設置字體
label.font = [UIFont systemFontOfSize:30];
// 文本加粗
label.font = [UIFont boldSystemFontOfSize:15];
// 4.textAlignment 文本對齊方式, 默認是左對齊
label.textAlignment = NSTextAlignmentCenter;
// 文本對齊方式:
typedef enum NSTextAlignment : NSUInteger {
NSTextAlignmentLeft = 0,左對齊
NSTextAlignmentCenter = 1,居中
NSTextAlignmentRight = 2,右對齊
NSTextAlignmentJustified = 3,在一個段落的最后一行自然對齊
NSTextAlignmentNatural = 4,
} NSTextAlignment;
// 5.numberOfLines 默認為1行
// 不確定行數時, 給0
// 6.換行模式
enum {
NSLineBreakByWordWrapping = 0,以單詞為顯示單位顯示,后面部分省略不顯示,默認
NSLineBreakByCharWrapping,以字符為顯示單位顯示,后面部分省略不顯示
NSLineBreakByClipping,剪切與文本寬度相同的內容長度,后半部分被刪除
NSLineBreakByTruncatingHead,開頭省略,顯示尾部文字內容
NSLineBreakByTruncatingTail,結尾省略,顯示開頭的文字內容
NSLineBreakByTruncatingMiddle 中間省略,顯示頭尾的文字內容
};
typedef NSUInteger NSLineBreakMode
// 7.shadowColor 陰影
newLabel.shadowColor = [UIColor blackColor];
// shadowOffset 陰影偏移
newLabel.shadowOffset = CGSizeMake(1, 1);
三. UITextField
1. UITextField基本屬性
textField.backgroundColor = [UIColor redColor];/**< 該屬性默認是nil */
// 1.text 給文本框賦初值
// textField.text = @"你好";
// 2.textColor 文本框初值的文字顏色
textField.textColor = [UIColor yellowColor];
// 3.UITextBorderStyleLine邊框有邊緣線
textField.borderStyle = UITextBorderStyleLine;
// 4.UITextBorderStyleRoundedRect邊緣帶圓角
textField.borderStyle = UITextBorderStyleRoundedRect;
// UITextBorderStyle枚舉值:
typedef enum UITextBorderStyle : NSInteger {
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
} UITextBorderStyle;
// 5.placeholder占位符
// 當textField不為空(.text屬性有值)時, placeHolder是不顯示的
textField.placeholder = @"這是牛逼的文本框";
// 6.clearsOnBeginEditing開始編輯時清空文本框的輸入(輸錯密碼時)
textField.clearsOnBeginEditing = YES;
// 7.secureTExtEntry 密文輸入(BOOL值)(輸密碼)
textField.secureTextEntry = YES;
// 8.leftView/rightView 左右視圖
// 9.leftViewMode
textField.leftView = view;
textField.leftViewMode = UITextFieldViewModeAlways;
//10.輸入框中是否有個叉號,在什么時候顯示,用于一次性刪除輸入框中的內容
text.clearButtonMode = UITextFieldViewModeAlways;
typedef enum {
UITextFieldViewModeNever, 從不出現
UITextFieldViewModeWhileEditing, 編輯時出現
UITextFieldViewModeUnlessEditing, 除了編輯外都出現
UITextFieldViewModeAlways 一直出現
} UITextFieldViewMode;
//11.是否糾錯
text.autocorrectionType = UITextAutocorrectionTypeNo;
typedef enum {
UITextAutocorrectionTypeDefault, 默認
UITextAutocorrectionTypeNo, 不自動糾錯
UITextAutocorrectionTypeYes, 自動糾錯
} UITextAutocorrectionType;
//12.再次編輯就清空
text.clearsOnBeginEditing = YES;
2. UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; //控制是否允許輸入,返回值是NO的時候,禁止輸入;
- (void)textFieldDidBeginEditing:(UITextField *)textField; //文本框已經開始編輯
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; //是否結束輸入,返回值是YES時候,允許停止輸入,并且釋放第一響應;返回NO的時候,則相反;
- (void)textFieldDidEndEditing:(UITextField *)textField;
- (BOOL)textFieldShouldClear:(UITextField *)textField; //當清空按鈕時候調用,返回是NO的時候,忽視清空
- (BOOL)textFieldShouldReturn:(UITextField *)textField; //當點擊return按鈕時候調用該方法,返回是NO的時候,不做任何響應;
3. UITextField設置圓角
// 出了利用屬性中的borderStyle來是文本框四個角變成圓角外,還可以用UIBezierPath和CAShapeLayer來畫圓角
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 155, 40)];
textField.backgroundColor = [UIColor lightGrayColor];
textField.placeholder = @"這里是UITextField哦";
// 設置placeHolder的顏色
[textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
// 這個方法的好處是可以將UITextField的任意一個角畫成圓角
// 設置UITextField的左上角和右下角為圓角,并設置了圓角的弧度
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:textField.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.path = maskPath.CGPath;
maskLayer.frame = textField.bounds;
textField.layer.mask = maskLayer;
效果圖如下: