码迷,mamicode.com
首页 > 其他好文 > 详细

LessonUIViewSubClass

时间:2015-03-19 10:04:03      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

//

//  AppDelegate.m

//  LessonUIViewSubClass

//

//  Created by lanouhn on 15/3/18.

//  Copyright (c) 2015年 lanouhn. All rights reserved.

//

 

#import "AppDelegate.h"

 

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

 

- (void)dealloc {

    [_window release];

    [super dealloc];

}

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor yellowColor];

    [self.window makeKeyAndVisible];

    

    /*

    //UILabel, 标签视图, 主要用于显示文字

    //UILabel继承于UIView

    

    //1.创建

    UILabel *userLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 335, 40)];

    //2.设置属性

//    userLabel.backgroundColor = [UIColor redColor];

    //设置文本

    userLabel.text = @"小胖选美大赛现场投票计数:";

    //设置文本颜色

    userLabel.textColor = [UIColor blackColor];

    //设置字体样式

    //系统文字

//    userLabel.font = [UIFont systemFontOfSize:40];

    //系统文字加粗

    userLabel.font = [UIFont boldSystemFontOfSize:40];

    //自定义文字样式

//    userLabel.font = [UIFont fontWithName:@"黑体" size:<#(CGFloat)#>]

    //设置行数, 默认值是1, 0代表无限行

    userLabel.numberOfLines = 1;

    //设置文字的对齐方式

    userLabel.textAlignment = NSTextAlignmentCenter;

    //设置阴影

//    userLabel.shadowColor = [UIColor redColor];

    //设置阴影偏移

//    userLabel.shadowOffset = CGSizeMake(10, 10);

    //字体大小适应label宽度

    userLabel.adjustsFontSizeToFitWidth = YES;

    //设置大小, 缩放比率

    userLabel.minimumScaleFactor = 0.5;

    

    //3.添加父视图

    [self.window addSubview:userLabel];

    //4.释放

    [userLabel release];

    

    

    //UITextField, 文本输入框, 用于输入显示文字

    //1.创建

    UITextField *userTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 80, 335, 40)];

    //2.设置属性

    userTextField.backgroundColor = [UIColor grayColor];

    //设置输入的文本框

//    userTextField.text = @"请输入用户名";

    //设置占位符

    userTextField.placeholder = @"请输入用户名";

    //文本的颜色

    userTextField.textColor = [UIColor greenColor];

    //设置文字样式

    userTextField.font = [UIFont systemFontOfSize:40];

    //设置对齐方式

    userTextField.textAlignment = NSTextAlignmentCenter;

    //设置输入框样式

//    userTextField.borderStyle = UITextBorderStyleRoundedRect;

    //设置背景图

//    userTextField.background = [UIImage imageNamed:@"haha"];

    //是否允许用户输入

    userTextField.enabled = YES;

    //开始编辑文本时,清空内容

    userTextField.clearsOnBeginEditing = YES;

    //清空按钮

    userTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

    //安全设置

    userTextField.secureTextEntry = YES;

    //设置弹出键盘样式

    userTextField.keyboardType = UIKeyboardTypeDefault;

    //设置return按钮的样式

    userTextField.returnKeyType = UIReturnKeyDone;

    //设置代理

    userTextField.delegate = self;

    //自定义键盘

    UIView *keyBoardView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 300)];

    keyBoardView.backgroundColor = [UIColor grayColor];

    userTextField.inputView = keyBoardView;

    [keyBoardView release];

    //设置leftView

    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

    leftView.backgroundColor = [UIColor redColor];

    userTextField.leftView = leftView;

    [leftView release];

    //设置leftView的显示模式

    userTextField.leftViewMode = UITextFieldViewModeAlways;

    

    //3.添加父视图

    [self.window addSubview:userTextField];

    //4.释放

    [userTextField release];

    

    //command + k 弹出键盘

    //command + s 截屏

    

    //创建一个中心视图

    UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    centerView.center = self.window.center;

    centerView.backgroundColor = [UIColor colorWithRed:0.800 green:1000 blue:0.400 alpha:1.000];

    //设置边框

    centerView.layer.borderWidth = 10;

    centerView.layer.borderColor = [UIColor greenColor].CGColor;

    //设置圆角弧度

    centerView.layer.cornerRadius = 50;

    //设置阴影颜色

    centerView.layer.shadowColor = [UIColor blackColor].CGColor;

    //设置阴影偏移

    centerView.layer.shadowOffset = CGSizeMake(10, 10);

    //设置阴影不透明度

    centerView.layer.shadowOpacity = 1;

    //设置超出边界部分是否显示

    centerView.clipsToBounds = YES;

    

    [self.window addSubview:centerView];

    [centerView release];

    

    //添加一个视图

    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];

    subView.backgroundColor = [UIColor redColor];

    [centerView addSubview:subView];

    [subView release];

    

    //UIButton,按钮类, 用于响应和处理一些事件

    //1.创建

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    //2.属性设置

    button.frame = CGRectMake(20, 140, 335, 40);

    button.backgroundColor = [UIColor blackColor];

    //设置字体大小

    button.titleLabel.font = [UIFont systemFontOfSize:35];

    //设置文字

    [button setTitle:@"正常状态" forState:UIControlStateNormal];

    [button setTitle:@"高亮状态" forState:UIControlStateHighlighted];

    [button setTitle:@"选中状态" forState:UIControlStateSelected];

    [button setTitle:@"不可选状态" forState:UIControlStateDisabled];

    //控件状态

    //UIControlStateNormal 正常状态

    //UIControlStateHighlighted 高亮状态

    //UIControlStateSelected 选中状态

    //UIControlStateDisabled 不可选状态

    

    //设置按钮是否选中

//    button.selected = YES;

    //设置按钮是否可用

//    button.enabled = NO;

    //设置字体颜色

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    //设置图片

//    [button setImage:[UIImage imageNamed:@"石兰"] forState:UIControlStateNormal];

    //设置背景图片

    [button setBackgroundImage:[UIImage imageNamed:@"石兰"] forState:UIControlStateNormal];

    //关联事件(方法)

    //参数1:目标, 谁来调用方法

    //参数2:事件, 关联的方法名

    //参数3:触发事件

//    [button addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];

    [button addTarget:self action:@selector(pressButtonWithAlertView) forControlEvents:UIControlEventTouchUpInside];

    //按照响应事件移除

//    [button removeTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];

    //3.添加到父视图

    [self.window addSubview:button];

    //4.千万不要释放

    [self pressButton];

    [self pressButtonWithAlertView];

    */

    //创建一个label

    UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 335, 40)];

    aLabel.backgroundColor = [UIColor grayColor];

    aLabel.tag = 100;

    aLabel.textAlignment = NSTextAlignmentCenter;

    [self.window addSubview:aLabel];

    [aLabel release];

    

    //创建一个textField

    UITextField *aTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 80, 335, 40)];

    aTextField.backgroundColor = [UIColor greenColor];

    aTextField.tag = 200;

    aTextField.borderStyle = UITextBorderStyleRoundedRect;

    aTextField.textAlignment = NSTextAlignmentLeft;

    [self.window addSubview:aTextField];

    [aTextField release];

    

    //创建一个button

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeSystem];

    aButton.frame = CGRectMake(20, 140, 200, 40);

    [aButton setTitle:@"上去" forState:UIControlStateNormal];

    aButton.tag = 300;

    aButton.backgroundColor = [UIColor redColor];

    aButton.titleLabel.font = [UIFont systemFontOfSize:30];

    [self.window addSubview:aButton];

    [aButton addTarget:self action:@selector(up) forControlEvents:UIControlEventTouchUpInside];

    

    [self up];

    

    return YES;

}

 

- (void)up {

    //通过tag值找输入框

    UITextField *textField = (UITextField *)[self.window viewWithTag:200];

    //获取输入框内容

    NSLog(@"%@", textField.text);

    //通过tag值找到label

    UILabel *label = (UILabel *)[self.window viewWithTag:100];

    //设置标签内容

    if (label.text.length == 0) {

        label.text = textField.text;

    } else {

        label.text = [NSString stringWithFormat:@"%@%@", label.text, textField.text];

    }

    //清空输入框的内容

    textField.text = nil;

    //label不能长于30字符

    if (label.text.length > 30) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"字符串太长" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];

        [alertView show];

        [alertView release];

    }

}

 

- (void)pressButton {

    NSLog(@"Hello World!");

}

 

- (void)pressButtonWithAlertView {

    //UIAlertView, 警告框

    //1.创建

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您有一万元到账" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

    

    //2.显示

    [alertView show];

    //释放

    [alertView release];

 

}

 

- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

 

- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

 

- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

 

- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

 

- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

 

#pragma mark - UITextFieldDelegate

 

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    //打印当前方法名

    NSLog(@"%s",__FUNCTION__);

    //键盘的消失

    [textField resignFirstResponder];

    return YES;

}

 

@end

 

LessonUIViewSubClass

标签:

原文地址:http://www.cnblogs.com/xiaoxuetongxie/p/4349460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!