美文网首页
UImageView显示图片

UImageView显示图片

作者: 我不白先生 | 来源:发表于2020-10-07 15:34 被阅读0次

    用于显示图片视图

    • 属性:
      image: UIImage类型,用于设置要显示的图片。
      如何构建UIImage类型的对象,使用imageNamed:方法即可。
      contentMode:设置内容的显示模式,此属性是枚举类型的,有三个选项
      1.UIViewContentModeScaleToFill默认值,会拉伸图片
      2.UIViewContentModeScaleAspectFit保持宽高比,并且看到图片的全部,如果图片不合适,可能在图片框中留出空
      3.UIViewContentModeScaleAspectFill保持宽高比,填满图片框不会留白,但是可能会有图片过大时超出图片框的范围,此时要配合一个设置,即多出部分剪切掉clipsToBounds = YES
    //    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 300, 400)];
    //    imageView.backgroundColor = [UIColor redColor];
    //    imageView.image = [UIImage imageNamed:@"timg"];
        
    //    /*UIViewContentModeScaleToFill,会拉伸填充满
    //    UIViewContentModeScaleAspectFit,会留白
    //    UIViewContentModeScaleAspectFill,会切割超出部分 但要配合clipsToBounds 使用
    //     */
    //    imageView.contentMode = UIViewContentModeBottom;
    //    imageView.clipsToBounds = YES;
    //    [self.view addSubview:imageView];
        //如果使用initWithImage初始化方法创建 UIImageView对象,不设置frame,那么frame就是图片的原尺寸大小
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"timg"]];
        [self.view addSubview:imageView];
    
    • 练习对话框练习及封装,return用委托方法处理事件且不收键盘
    #import "MyViewController.h"
    //使用 系统委托 第一件事 遵守协议 委托方类名+Delegate
    @interface MyViewController ()<UITextFieldDelegate>
    @property(nonatomic,assign)NSInteger y;
    @end
    
    @implementation MyViewController
    //使用 系统委托 第二件事 实现协议中 的代理方法
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [self senderMessage:textField isMe:YES];
        return YES;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.y = 20;
        UITextField *meTF = [[UITextField alloc]initWithFrame:CGRectMake(20, self.y, self.view.frame.size.width - 20 - 20, 40)];
        meTF.borderStyle = UITextBorderStyleRoundedRect;
        meTF.placeholder = @"请输入聊天内容";
    //    //添加点击return的事件方法 didEndOnExit
    //    [meTF addTarget:self action:@selector(sendMeMessage:) forControlEvents:UIControlEventEditingDidEndOnExit];
        //使用 系统委托 第三件事 设置委托方的代理人是谁
        meTF.delegate =  self;
        [self.view addSubview:meTF];
        self.y += meTF.frame.size.height + 20;
        UITextField *otherTF = [[UITextField alloc]initWithFrame:CGRectMake(20, self.y, meTF.frame.size.width, 40)];
        otherTF.borderStyle = UITextBorderStyleRoundedRect;
        otherTF.placeholder = @"请输入聊天内容";
        [otherTF addTarget:self action:@selector(sendOtherMessage:) forControlEvents:UIControlEventEditingDidEndOnExit];
        [self.view addSubview:otherTF];
        self.y += otherTF.frame.size.height + 20;
    }
    ////meTF 的事件方法
    //-(void)sendMeMessage:(UITextField*)sender{
    //    [self senderMessage:sender isMe:YES];
    //}
    -(void)sendOtherMessage:(UITextField*)sender{
       [self senderMessage:sender isMe:NO];
    }
    -(void)senderMessage:(UITextField*)sendTextField isMe:(BOOL)isMe
    {
        UILabel *label = [[UILabel alloc]init];
        label.text = sendTextField.text;
        label.numberOfLines= 0;
        //根据内容获取实际 宽 高
        CGRect labelRect = [label textRectForBounds:CGRectMake(0, 0, 200, 999) limitedToNumberOfLines:0];
        label.frame = CGRectMake(isMe ? 20 : 30, 20, labelRect.size.width, labelRect.size.height);
        CGFloat imageViewWidth = label.frame.size.width +50;
        CGFloat imageViewHeight= label.frame.size.width +40;
        
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:isMe ? @"气泡右" : @"气泡左"];
        imageView.frame= CGRectMake(isMe ? self.view.frame.size.width - imageViewWidth - 20 : 20 , self.y, imageViewWidth, imageViewHeight);
        //将label添加到 imageView上
        [imageView addSubview:label];
        //将imageView添加到 self.view上
        [self.view addSubview:imageView];
        self.y += imageViewHeight +20;
        sendTextField.text = @"";
    }
    
    
    • 练习二,文本框密码登陆
    @interface MyViewController ()
    @property(nonatomic,strong)UITextField *usernameTextField;
    @property(nonatomic,strong)UITextField *passwordTextField;
    @property(nonatomic,strong)UIButton *button;
    @property(nonatomic,strong)UILabel *label;
    @property(nonatomic,assign)NSInteger i;
    @end
    
    @implementation MyViewController
    /*
     声明一个属性
     声明一个 _属性名 变量
     生成一个setter方法 set属性名
     生成一个getter方法 直接使用属性名做方法名
     */
    //在第一次使用该属性的 getter方法的 创建对象
    //懒得不能在懒了 在使用前不创建不行了z才去创建
    //延迟加载 (属性的懒加载)
    -(UITextField *)usernameTextField{
        if(!_usernameTextField)
        _usernameTextField = [[UITextField alloc]init];
        _usernameTextField.frame = CGRectMake(20, 100, 300, 40);
        _usernameTextField.borderStyle = UITextBorderStyleRoundedRect;
        _usernameTextField.placeholder = @"请输入用户名";
        _usernameTextField.textAlignment = NSTextAlignmentCenter;
        //给文本框 添加事件 当文本框编辑是会自动调用
        [_usernameTextField addTarget:self action:@selector(textFieldEditing:) forControlEvents:UIControlEventEditingChanged];
        [self.view addSubview:_usernameTextField];
        
        return _usernameTextField;
        
    }
    -(UITextField *)passwordTextField
    {   if(!_passwordTextField)
        _passwordTextField = [[UITextField alloc]init];
        _passwordTextField.frame = CGRectMake(20, 200, 300, 40);
        _passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
        _passwordTextField.placeholder = @"请输入密码";
        _passwordTextField.textAlignment = NSTextAlignmentCenter;
        //给文本框 添加事件 当文本框编辑是会自动调用
        [_passwordTextField addTarget:self action:@selector(textFieldEditing:) forControlEvents:UIControlEventEditingChanged];
        [self.view addSubview:_passwordTextField];
        
        return _passwordTextField;
    }
    -(UIButton*)button
    {    if(!_button)
       {
           _button = [UIButton buttonWithType:UIButtonTypeSystem];
           _button.frame = CGRectMake(20, 280, 300, 40);
           [_button setBackgroundImage:[UIImage imageNamed:@"BTN"] forState:UIControlStateNormal];
           [_button setBackgroundImage:[UIImage imageNamed:@"BTN2"] forState:UIControlStateHighlighted];
           [_button setBackgroundImage:[UIImage imageNamed:@"disable"] forState:UIControlStateDisabled];
           [_button addTarget:self action:@selector(userLogin:) forControlEvents:UIControlEventTouchUpInside];
           //设置button为非激活状态(失效状态)
           _button.enabled = NO;
           [self.view addSubview:_button];
       }
        return _button;
    }
    -(UILabel *)label
    {
        if(!_label){
       _label = [[UILabel alloc]initWithFrame:CGRectMake(20, 350, 300, 40)];
        _label.backgroundColor = [UIColor whiteColor];
            [self.view addSubview:_label];
      }
        return _label;
    }
    -(void)textFieldEditing:(UITextField*)sender
    {
        if(self.usernameTextField.text.length != 0 && self.passwordTextField.text.length !=0)
            //如果两个文本框中都有内容 button 激活
        {
            self.button.enabled = YES;
        }
        else
        {
            self.button.enabled = NO;
        }
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    //    NSInteger d = self.i +1;//getter
    //    self.i = 10+20;//setter
    //    self.i += self.i + 20;//前面 setter 后面 getter
    //    NSInteger d2 = [self i] +1;//getter
    //    [self setI:10 +20];//setter
    //    [self setI: [self i] + 20];//前面 setter 后面 getter
        [self usernameTextField];
        [self passwordTextField];
        [self button];
        [self label];
        
    }
    -(void)userLogin:(UIButton*)sender
    {
        if([self.usernameTextField.text isEqualToString:@"abc"]&&[self.passwordTextField.text isEqualToString:@"123"]){
            self.label.text =@"登陆成功";
            self.label.textAlignment = NSTextAlignmentCenter;
            
        }
        self.label.text = @"登陆失败";
    }
    

    相关文章

      网友评论

          本文标题:UImageView显示图片

          本文链接:https://www.haomeiwen.com/subject/rocwuktx.html