UIday02~04:UITextField UIButton UIdelegate 自定义视图 容器视图控制器

UIday02~04总结:

1、UILabel

2、UITextField 

3、UIButton 

4、UIdelegate 

5、程序的启动流程

6、自定义视图 

7、容器视图控制器  UIViewController

8、检测屏幕旋转 

9、处理内存警告 

10、事件的基本概念 

11、触摸的基本概念 

12、响应者链


1、UILabel


//UILable是iPhone界面最基本的控件,主要用来显示文本信息。
//·常用属性和方法有:
//1、创建
CGRect rect = CGRectMake(100, 200, 50, 50);
UILabel *label = [[UILabel alloc] initWithFrame:rect];

//2、text //设置和读取文本内容,默认为nil
label.text = @”文本信息”; //设置内容
NSLog(@”%@”, label.text); //读取内容

//3、textColor //设置文字颜色,默认为黑色
lable.textColor = [UIColor redColor];

//4、font //设置字体大小,默认17
label.font = [UIFont systemFontOfSize:20]; //⼀一般方法
label.font = [UIFont boldSystemFontOfSize:20]; //加粗方法
label.font = [UIFont fontWithName:@"Arial" size:16]; //指定
//字体的方法
//还有⼀一种从外部导入字体的方法。

//5、textAlignment //设置标签文本对齐方式。
label.textAlignment = NSTextAlignmentCenter; //还有
NSTextAlignmentLeft、 NSTextAlignmentRight.

//6、numberOfLines //标签最多显示行数,如果为0则表示多行。
label.numberOfLines = 2; //显示两行
label.numberOfLines = 0; //折行显示

//7、enabled //只是决定了Label的绘制方式,将它设置
//为NO将会使文本变暗,表示它没有激活,这时向它设置颜色值是无效的。
label.enable = NO;

//8、highlighted //是否高亮显示
label.highlighted = YES;
label.highlightedTextColor = [UIColor orangeColor]; //高亮

//显示时的文本颜色
//9、ShadowColor //设置阴影颜色
[label setShadowColor:[UIColor blackColor]];

//10、ShadowOffset //设置阴影偏移量
[label setShadowOffset:CGSizeMake(-1, -1)];

//11、baselineAdjustment //如果adjustsFontSizeToFitWidth属性设
//置为YES,这个属性就来控制文本基线的行为。
label.baselineAdjustment = UIBaselineAdjustmentNone;
UIBaselineAdjustmentAlignBaselines = 0,//默认,文本最上端与中线对齐。
UIBaselineAdjustmentAlignCenters,  //文本中线与label中线对齐。
UIBaselineAdjustmentNone, //文本最低端与label中线对齐。

//12、Autoshrink //是否自动收缩
//Fixed Font Size 默认,如果Label宽度小于文字长度时时,文字大小不自动缩放
//minimumScaleFactor 设置最小收缩比例,如果Label宽度小于文字长度时,文字进行收缩,收缩超过比例后,停止收缩。
//minimumFontSize 设置最小收缩字号,如果Label宽度小于文字长度时,文字字号减小,低于设定字号后,不再减小。//6.0以后不再使用了。
label.minimumScaleFactor = 0.5;

//13、adjustsLetterSpacingToFitWidth //改变字母之间的间距来适应Label大小
myLabel.adjustsLetterSpacingToFitWidth = NO;

//14、 lineBreakMode //设置文字过长时的显示格式
label.lineBreakMode = NSLineBreakByCharWrapping;//以字符为显示单位显示,后面部分省略不显示。
label.lineBreakMode = NSLineBreakByClipping;//剪切与文本宽度相同的内容长度,后半部分被删除。
label.lineBreakMode = NSLineBreakByTruncatingHead;//前面部分文字以……方式省略,显示尾部文字内容。
label.lineBreakMode = NSLineBreakByTruncatingMiddle;//中间的内容以……方式省略,显示头尾的文字内容。
label.lineBreakMode = NSLineBreakByTruncatingTail;//结尾部分的内容以……方式省略,显示头的文字内容。
label.lineBreakMode = NSLineBreakByWordWrapping;//以单词为显示单位显示,后面部分省略不显示。

//15、 adjustsFontSizeToFitWidth //设置字体大小适应label宽度
label.adjustsFontSizeToFitWidth = YES;

//16、attributedText:设置标签属性文本。
NSString *text = @"first";
NSMutableAttributedString *textLabelStr =
[[NSMutableAttributedString alloc]
 initWithString:text];
[textLabelStr
 setAttributes:@{NSForegroundColorAttributeName :
                     [UIColor lightGrayColor], NSFontAttributeName :
                     [UIFont systemFontOfSize:17]} range:NSMakeRange(11,
                                                                     10)];
label.attributedText = textLabelStr;

//17、竖排文字显示每个文字加一个换行符,这是最方便和简单的实现方式。
label.text = @"请\n竖\n直\n方\n向\n排\n列";
label.numberOfLines = [label.text length];

//18、计算UIlabel 随字体多行后的高度
CGRect bounds = CGRectMake(0, 0, 200, 300);
heightLabel = [myLabel textRectForBounds:bounds
                  limitedToNumberOfLines:20]; //计算20行后的Label的Frame
NSLog(@"%f",heightLabel.size.height);

//19、UILabel根据字数多少自动实现适应高度
UILabel *msgLabel = [[UILabel alloc]
                     initWithFrame:CGRectMake(15, 45, 0, 0)];
msgLabel.backgroundColor = [UIColor lightTextColor];
[msgLabel setNumberOfLines:0];
msgLabel.lineBreakMode = UILineBreakModeWordWrap;
msgLabel.font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(290, 1000);
msgLabel.text = @"获取到的deviceToken,我们可以通过webservice服务提交给.net应用程序,这里我简单处理,直接打印出来,拷贝到.net应用环境中使用。";
CGSize msgSie = [msgLabel.text sizeWithFont:fonts
                          constrainedToSize:size];
[msgLabel setFrame:CGRectMake(15, 45, 290, msgSie.height)];

//------------label自适应高度------------
//初始化label
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
    //设置自动行数与字符换行
    [label setNumberOfLines:0];
    label.lineBreakMode = UILineBreakModeWordWrap;
    // 测试字串
    NSString *s = @"自适应高度马上呈现自适应高度马上呈现自适应高度马上呈现自适应高度马上呈现自适应高度马上呈现自适应高度马上呈现自适应高度马上。。。";
    UIFont *font = [UIFont fontWithName:@"Arial" size:12];
    //设置一个行高上限
    CGSize size = CGSizeMake(320,2000);
    //计算实际frame大小,并将label的frame变成实际大小
    CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
    [label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];
//-------------------------------------


 //20、渐变字体Label
UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage
                                                      imageNamed:@"btn.png"]];
NSString *title = @"Setting";
UILabel *titleLabel = [[UILabel alloc]
                       initWithFrame:CGRectMake(0, 0, 80, 44)];
titleLabel.textColor = titleColor;
titleLabel.text = title;
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:titleLabel];
[titleLabel release];

//21、Label添加边框
titleLabel.layer.borderColor = [[UIColor grayColor] CGColor];
titleLabel.layer.borderWidth = 2;



 

<pre>    <span style="color:#008000;">
    //</span><span style="color:#008000;">清空背景颜色   </span>
    label.backgroundColor =<span style="color:#000000;"> [UIColor clearColor];   

</span>    <span style="color:#008000;">//</span><span style="color:#008000;">设置字体颜色为白色   </span>
    label.textColor =<span style="color:#000000;"> [UIColor whiteColor];   

</span>    <span style="color:#008000;">//</span><span style="color:#008000;">文字居中显示   </span>
    label.textAlignment =<span style="color:#000000;"> UITextAlignmentCenter;   

</span>    <span style="color:#008000;">//</span><span style="color:#008000;">自动折行设置   </span>
    label.lineBreakMode =<span style="color:#000000;"> UILineBreakModeWordWrap;   
</span>    label.numberOfLines = <span style="color:#800080;">0</span>;  

 
//设置高亮 和字体颜色     

    label1.highlighted =YES;     
  label1.highlightedTextColor = [UIColororangeColor]; 

//设置阴影颜色和偏移位置     

    label1.shadowColor = [UIColorredColor];     
   label1.shadowOffset = CGSizeMake(1.0,1.0);//设置label中的文字是否可变,默认值是YES          label1.enabled =NO;//设置文字过长时的显示格式     

    label1.lineBreakMode =UILineBreakModeMiddleTruncation;//截去中间 



2、UITextField 

    // 1. UITextField 控制文本输入和显示的控件 (cmd + k 打开模拟器的键盘)
    // 作用:1.文本显示;2.文本输入控制;3.外观配置
    // UITextField 和 UILabel的区别 UILabel主要用于文字显示,UITextField主要用于编辑文字(输入)
    // 1.1 创建UITextField 和创建 UILabel 相似:
    UITextField * tf1 = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 180, 50)];
    tf1.backgroundColor = [UIColor greenColor];
    [self.window addSubview:tf1];
    

<span>    textField.</span>text = @"文本输入";    <pre name="code" class="objc"><span>    textField.</span>textColor = [UIColor blueColor];    <pre name="code" class="objc"><pre name="code" class="objc"><span>    textField</span>.textAlignment = NSTextAlignmentCenter;<pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><span>    textField</span>.font = [UIFont systemFontOfSize:20];

    //placeholder(占位字符串,没有输入时给提示)

 
 
<pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><span>    textField</span>.placeholder = @"这里输入密码";
// 1.3 输入控制
    // enabled  是否允许输入  默认为YES
    // clearsOnBeginEditing  开始输入是否清空原输入框内容 默认为YES
    // secureTextEntry  是否文字以圆点格式显示
    // keyboardType  弹出键盘的类型(枚举值)
    // returnKeyType  键盘右下角return按钮类型(枚举值)
    // inputView        自定义输入视图(默认是键盘)
    // inputAccessoryView   输入视图上方的辅助视图(默认nil)
    
    tf1.enabled = YES;
    tf1.clearsOnBeginEditing = NO;
    tf1.secureTextEntry = YES;
    tf1.keyboardType = UIKeyboardTypeDefault;
    tf1.returnKeyType = UIReturnKeyGo;

//    UIView * v1= [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 100)];
//    v1.backgroundColor  = [UIColor redColor];
//    tf1.inputView = v1;
//    UIView * v2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];
//    v2.backgroundColor = [UIColor yellowColor];
//    tf1.inputAccessoryView = v2;

    // 1.4 外观控制
    // borderStyle  边框样式
    // clearButtonMode  清除按钮模式
    // leftView     输入框左视图
    // leftViewMode     左视图显示模式
    // rightView    输入框右视图
    // rightViewMode    右视图的显示模式
    
    // 显示边框为圆角矩形
    tf1.borderStyle = UITextBorderStyleRoundedRect;
    // 总是显示清除按钮
    tf1.clearButtonMode = UITextFieldViewModeAlways;
    UIView * v3= [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 100)];
    v3.backgroundColor  = [UIColor redColor];
    tf1.leftView = v3;
    tf1.leftViewMode =UITextFieldViewModeAlways;




 
 
 
 
 
 
 

3、UIButton 

    // UIButton
    // UIButton 创建UIButton用的是类方法
        UIButton * b1 = [UIButton buttonWithType:UIButtonTypeSystem];
         b1.frame = CGRectMake(50, 50, 100, 100);
         b1.backgroundColor = [UIColor yellowColor];
     
      // 添加事件 buttonAction: 为行为方法
         [b1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDragInside];
     
      // 移除事件
         [b1 removeTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDragInside];
     
     
     
      // 设置文字
         [b1 setTitle:@"按钮" forState:UIControlStateNormal];
     
         // 获取Button上的文字
         NSLog(@"%@",[b1 titleForState:UIControlStateNormal]);
     
         // 设置文字颜色
         [b1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
     
         // 获取文字颜色
         UIColor * color = [b1 titleColorForState:UIControlStateNormal];

        // 设置按钮圆角
         bt.layer.cornerRadius = 10;  

        // 设置阴影颜色
         [b1 setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal];
//         b1.titleLabel.shadowOffset =
    
      // 设置button图片 把图片拖到左侧树
      // 这里的图片必须是镂空图
         [b1 setImage:[UIImage imageNamed:@"user.png"] forState:UIControlStateNormal];
     
      // 获取图片
         UIImage * image = [b1 imageForState:UIControlStateNormal];
     
      // 设置背景图
         [b1 setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
         [self.window addSubview:b1];

      // 隐藏button
         b1.hidden = YES;

      //设置button上字体大小
      btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];   

//设置button上字体的位置
Button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//左对齐(UIControlContentHorizontalAlignment、CenterUIControlContentHorizontalAlignmentFill、UIControlContentHorizontalAlignmentRight)
Button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;//底部对其(UIControlContentVerticalAlignmentCenter、UIControlContentVerticalAlignmentFill、UIControlContentVerticalAlignmentTop)

4、UIdelegate 

@interface AppDelegate ()<UITextFieldDelegate>

// 1.程序启动完成
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// delegate
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    UITextField * tf2 = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 250, 50)];
    tf2.backgroundColor = [UIColor yellowColor];
    [self.window addSubview:tf2];
    
    // 设置代理 (以后写代理的时候,一般也是写self)
    tf2.delegate = self;
    [self.window addSubview:tf2];
}

//延展作用: 1、定义私有变量,2、管理私有方法,3、遵循协议

// textField协议方法
// 当发生这些事件的时候,我们可以在方法中相应的处理方案
// 将要开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{    
    NSLog(@"将要开始编辑");
    return YES;    
}


- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"已经开始编辑");
}


//
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    NSLog(@"将要结束编辑");
    return NO;
}

// 点击return的时候
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    // 取消
    [textField resignFirstResponder];
    return YES;
}


5、程序的启动流程

// delegate.m

// 1.程序启动完成
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

}

// 2.程序将要释放活跃状态
- (void)applicationWillResignActive:(UIApplication *)application {
    NSLog(@"程序将要释放活跃状态");
}

// 3.程序进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"程序进入后台");
}

// 4.程序将要进入前台
- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"程序将要进入前台");
}

// 5.程序变为活跃状态
- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSLog(@"程序变为活跃状态");
}

// 6.程序将要结束
- (void)applicationWillTerminate:(UIApplication *)application {
    NSLog(@"程序将要结束");
}







6、自定义视图 

// 自定义视图

    LTView * lt1 = [[LTView alloc]initWithFrame:CGRectMake(50, 50, 250, 50)];
    lt1.backgroundColor  = [UIColor yellowColor];
    [self.window addSubview:lt1];
    
    LTView * lt2 = [[LTView alloc]initWithFrame:CGRectMake(CGRectGetMinX(lt1.frame), CGRectGetMaxY(lt1.frame)+20, CGRectGetMaxX(lt1.frame), CGRectGetMaxY(lt1.frame))];
    lt2.backgroundColor = [UIColor yellowColor];
    [self.window addSubview:lt2];



7、容器视图控制器  UIViewController

      UIViewController
     容器视图控制器主要是简化逻辑,不要让程序变的太复杂

     UIViewController 管理视图 一个Controller管理一个视图
     UIViewController 是 MVC 设计模式的核心。    

     1、将数据导给视图。
     2、处理逻辑。
     3、遵循协议,设置代理,添加事件。

#import "AppDelegate.h"
#import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

// 第二部分  练习控制视图
// 1.创建UIViewCtroller控制器类;2.然后导入类;3.创建视图控制器;4.设置根视图控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor yellowColor];
    [self.window makeKeyAndVisible];
    
    // 创建视图控制器
    RootViewController * rootVC = [[RootViewController alloc]init];
    
    // 设置根视图控制器
    self.window.rootViewController = rootVC;
    
    return YES;
}

@end



#import "RootViewController.h"
#import "LTView.h"
#import "RootView.h"

// 一个页面至少要有一个Controller

//延展非常重要

@interface RootViewController ()

// 声明一个属性,将要替换的View
@property (nonatomic,strong)RootView * rv;

@end

@implementation RootViewController

// 加载视图 (重写)
-(void)loadView{
    self.rv = [[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //替换view
    self.view = _rv;
}


// 视图加载完成(也就是视图能够显示出来了,控制器自带的View)
- (void)viewDidLoad {
    // 只要走到这个方法,说明自带的视图已经创建好了
    [super viewDidLoad];
    // Do any additional setup after loading the view.
/*
    // 添加控件
    self.view.backgroundColor = [UIColor yellowColor];
    
    // 创建
    LTView * lt1 = [[LTView alloc]initWithFrame:CGRectMake(50, 50, 250, 50)];
    lt1 .backgroundColor = [UIColor yellowColor];
    
    // 添加到某个地方
    [self.view addSubview:lt1];
*/
    
    // 不用系统自带的view,自己创建一个view
}

// 支持设备旋转方向 以 UIInterfaceOrientationMask 开头
-(NSUInteger)supportedInterfaceOrientations{
    
    // 支持设备所有方向 (最好不要支持所有方向)
    return UIInterfaceOrientationMaskAll;
}

// 接收到内存警告  现在内存都比较大  目前这种写法已经很少用了
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    // 处理内存问题
    // View加载过,并且没有显示
    if ([self isViewLoaded] == YES && self.view.window == nil) {
        // 将根视图销毁
        self.view = nil;
    }
}

@end 



8、检测屏幕旋转 

//检测当中阻断 置为NO不允许触摸检测
     self.userInteractionEnabled = NO;



9、处理内存警告 


10、事件的基本概念 


11、触摸的基本概念 

#import "RootView.h"

@implementation RootView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        TouchView * tv = [[TouchView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
        
        tv.backgroundColor = [UIColor redColor];
        
        [self addSubview:tv];
       
    }
    return self;
}

#import "TouchView.h"

@interface TouchView(){
    //刚触摸的点
    CGPoint _startPoint;
    //
    CGPoint _centerPoint;
    
}
@end;

@implementation TouchView



-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"TouchView 开始触摸");
    self.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
    
    //获取点击位置的点:
    UITouch * touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.superview];
    NSLog(@"%@",NSStringFromCGPoint(point));
    
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"TouchView 触摸中");
    
    UITouch * touch = [touches anyObject];
    CGPoint movePoint = [touch locationInView:self.superview];
    
    NSLog(@"%@",NSStringFromCGPoint(movePoint));
    
//    self.frame = CGRectMake(movePoint.x - 50, movePoint.y - 50, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    
    //计算移动差值
    CGFloat datalx = movePoint.x - _startPoint.x;
    CGFloat dataly = movePoint.y - _startPoint.y;
    
    // 给中心点加上差值
    self.center = CGPointMake(_centerPoint.x+datalx, _centerPoint.y+dataly);
    
    // 设置背景色
    self.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
    /*
    //检测:检测过程检测的都是视图
    
     1、检测应用 -- > window --> view
     2、
     
     响应:Responder
     响应者链  
     
     
    */
}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"TouchView 触摸结束");
}


-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"TouchView 触摸取消");
}
@end



12、响应者链

     1、检测:检测触摸点在哪个视图中。   

          UIApplication -> window -> viewController -> view ->检测所有⼦视图   

          最终确认触碰位置  完成响应者链的查询过程      

   

     2、响应:继承于UIResponder的对象都可以成为响应者链中的一部分。作用是确定哪个响应者处理事件。

        事件处理的顺序与触摸检测查询相反

         触摸的⼦子视图-> view -> viewController -> window -> UIApplication


      3、响应者链阻断:userInteractionEnabled

        关闭后能阻断查询过程。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值