IOS基本孔控件(UIView UITextField UILable UIButton,控件組合,分類)

UIView

    //視圖使用UIVIEW類來表示,UIVIEW是系統已提供好的類,可以拿來直接使用,UIVIEW在UIKIt框架裏面.
    //第一步 創建視圖(此刻不會顯示)
    UIView *aView=[[UIView alloc]initWithFrame:CGRectMake(40, 70, 240, 240)];
    //第二部 添加到window上進行顯示
    [self.window addSubview:aView];
    //第三步 視圖進行相關設置(UIVIEW 默認是 透明色也就是clearColor)
    [aView setBackgroundColor:[UIColor blueColor]];
    aView.backgroundColor=[UIColor clearColor];
    aView.layer.borderWidth=5;
    aView.layer.cornerRadius=120;
    //第四部 內存管理(釋放)
    [aView release];
    
    /*學習要講究方法,流程性的東西最有指導性作用的.上面列出 了所有MVC中V使用流程.記得流程勝過背代碼*/
    //MVC裏的V一般不提供便利構造器方法,只提供初始化方法.
    //子視圖以父視圖座標爲原點
    //同爲子視圖後加在前面(子視圖加在父視圖前面)
    UIView * userView=[[UIView alloc]initWithFrame:CGRectMake(20, 50, 50, 30)];
    userView.backgroundColor=[UIColor clearColor];
    userView.layer.borderWidth=1;
    userView.layer.cornerRadius=10;
    [aView  addSubview:userView];
    [userView release];



UILable

    UILabel用於顯示文本的控件 集成於uiview.相比uiview ,uilable類添加了空間的外觀,提供了顯示文字的功能
    使用UI步驟
    1.創建控件(alloc 初始化)
    注意:只要是空間,都需要設置frame  2.優先使用本類的初始化方法,如果沒有,使用父類繼承過初始化方法,以此類推.
    UILabel *useNameLable=[[UILabel alloc]initWithFrame:CGRectMake(30, 230, 70, 25)];
    
//    2.對控件進行設置(枚舉值 自己試試)
    useNameLable.backgroundColor=[UIColor clearColor];
//    useNameLable.font=[UIFont italicSystemFontOfSize:20];
    useNameLable.layer.borderWidth=1;
    useNameLable.layer.cornerRadius=4;
    useNameLable.text=@" 用戶名:";//    _useNameText=[[UITextField alloc]initWithFrame:CGRectMake(120, 230, 130, 25)];

//    3.添加到父視圖
    [loginContainerView addSubview:useNameLable];
//    4.釋放

    [useNameLable release];
    


UITextFild
//定義成屬性
//    _useNameText=[[UITextField alloc]initWithFrame:CGRectMake(120, 230, 130, 25)];

//    _useNameText.backgroundColor=[UIColor clearColor];
//    [loginContainerView addSubview:_useNameText];
//    _useNameText.layer.borderWidth=1;
//    _useNameText.layer.cornerRadius=4;
//    //指定輸入框佔位
//    _useNameText.placeholder=@"請輸入用戶名";
//    //輸入爲大寫或首字母大寫等等
//    _useNameText.autocapitalizationType=UITextAutocapitalizationTypeAllCharacters;
//    //是否自動糾正
//    _useNameText.autocorrectionType=UITextAutocorrectionTypeYes;
//    //useNameText.borderStyle=[UITextBorderStyleRoundedRect];
//    //改變樣式
//    _useNameText.borderStyle=UITextBorderStyleRoundedRect;
//    _useNameText.delegate=self;


//定義一個passWordText屬性
//    _passWordText=[[UITextField alloc]initWithFrame:CGRectMake(120, 280, 130, 25)];
//    _passWordText.backgroundColor=[UIColor clearColor];
//    [loginContainerView addSubview:_passWordText];
//    _passWordText.layer.borderWidth=1;設置邊框
//    _passWordText.layer.cornerRadius=4;//設置圓角
//    _passWordText.adjustsFontSizeToFitWidth=10;
//    _passWordText.placeholder=@"請輸入密碼";
//    _passWordText.secureTextEntry=YES;//設置輸入的文字顯示爲圓點,
//    _passWordText.delegate=self;//代理


UIButton
初始化
//    UIButton * confirmButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
//    confirmButton.frame=CGRectMake(30,350 , 50, 20);
設置屬性
//    confirmButton.backgroundColor=[UIColor clearColor];
//    [loginContainerView addSubview:confirmButton];
////    confirmButton.backgroundColor=[UIColor orangeColor];
//    [confirmButton setTitle:@"登陸" forState:UIControlStateNormal];
//    confirmButton.layer.borderWidth=1;
//    confirmButton.layer.cornerRadius=4;
////    [confirmButton setTitle:@"login" forState:UIControlStateHighlighted];//添加事件
//    [confirmButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];





下面是寫了一些組合控件

UIlable和UITextField組合
@interface CustomTextField : UIView <UITextFieldDelegate>
@property(nonatomic ,strong)UILabel *descriptionlable;
@property(nonatomic,strong)UITextField *textField;

-(id)initWithFrame:(CGRect)frame description:(NSString *)description textField:(NSString *)textField;

@end

.m文件
-(id)initWithFrame:(CGRect)frame description:(NSString *)description textField:(NSString *)textField;
{
    self = [super initWithFrame:frame];
    if (self) {
        
        CGFloat width=frame.size.width;
        CGFloat height=frame.size.height;
        _descriptionlable=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, width/3, height)];
        _descriptionlable.textAlignment=NSTextAlignmentRight;
        _descriptionlable.backgroundColor=[UIColor grayColor];
        _descriptionlable.text=description;
        _descriptionlable.layer.borderWidth=1;
        _descriptionlable.layer.cornerRadius=5;
        _descriptionlable.alpha=0.6;
        
        [self addSubview:_descriptionlable];
        _textField=[[UITextField alloc]initWithFrame:CGRectMake(width/3, 0, width-width/3, height)];
        _textField.borderStyle=UITextBorderStyleRoundedRect;
        _textField.text=textField;
        _textField.alpha=0.6;
       _textField.backgroundColor=[UIColor grayColor];
        _textField.layer.borderWidth=1;
        _textField.layer.cornerRadius=5;
        _textField.delegate=self;
        [self addSubview:_textField];
        
        // Initialization code
    }
    return self;
}



UIButoon分類
@interface UIButton (CategoryButton)
+ (UIButton *)roundRectButtonWithFrame:(CGRect)frame title:(NSString *)title target:(id)target action:(SEL)action;

+ (UIButton *)buttonWithType:(UIButtonType)type frame:(CGRect)frame title:(NSString *)title target:(id)target action:(SEL)action;
@end



#import "UIButton+CategoryButton.h"

@implementation UIButton (CategoryButton)
+ (UIButton *)roundRectButtonWithFrame:(CGRect)frame title:(NSString *)title target:(id)target action:(SEL)action
{
    
    return [UIButton buttonWithType:UIButtonTypeRoundedRect frame:frame title:title target:target action:action];
    
}

+ (UIButton *)buttonWithType:(UIButtonType)type frame:(CGRect)frame title:(NSString *)title target:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:type];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    button.layer.borderWidth=1;
    button.layer.contentsScale=1;
    return button;
    
}
@end









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章