zoukankan      html  css  js  c++  java
  • ios 常用基础控件属性

                         目录

            一、UIImageView常用属性

            二、UIButton常用属性

            三、UITextField常用属性

                 1、常用属性

                 2、计算UILabel高度

                 3、设置代理:添加键盘完成按钮,取消第一响应者

           一、UIImageView常用属性

             1、图片背景拉伸:类似点9图片

        UIImage *image=[UIImage imageNamed:@"icon"];
        [image stretchableImageWithLeftCapWidth:image.size.width*0.5 topCapHeight:image.size.height*0.5];

           

         2、让imageView的大小和图片的大小保持一致

     //让imageView的大小和图片的大小保持一致
        [self.imageView sizeToFit];

         

           二、UIButton常用属性

    #pragma  mark 设置UIButton属性
    - (UIButton *) setUIButtonAttribute:(UIButton *)textButton andPText:(NSString *)text {
              //设置圆角的半径
              [textButton.layer setCornerRadius:15];
              //切割超出圆角范围的子视图
              textButton.layer.masksToBounds = YES;
              //设置边框的颜色
              [textButton.layer setBorderColor:[UIColor whiteColor].CGColor];
              //设置边框的粗细
              [textButton.layer setBorderWidth:0.5];
              //设置字体文字
              [textButton setTitle:text forState:UIControlStateNormal];
              //设置字体颜色
              [textButton  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        return textButton;
    }

           三、UITextField常用属性

                1、常用属性

    #pragma  mark 设置UITextField属性
    - (UITextField *) setUITextFieldAttribute:(UITextField *)textField andPlaceHolderText:(NSString *)placeHolderText andLeftImageName:(UIImage *)leftImage{
               //设置文本提示
               textField.placeholder =placeHolderText;
               //设置提示文本颜色
               textField.tintColor= [UIColor lightGrayColor];
               //创建左边图标父控件
               UIView *leftViewSup=  [[UIView alloc] initWithFrame:CGRectMake(-20, 0, 30, 20)];
               UIImageView *imageViewPwd=[[UIImageView alloc]initWithFrame:CGRectMake(8, 0, 18, 20)];
               [leftViewSup addSubview:imageViewPwd];
               //设置左边图标
               imageViewPwd.image=leftImage;
               //设置图标
               textField.leftView=leftViewSup;
               //只要输入框有内容就出现清空按钮
               textField.leftViewMode=UITextFieldViewModeAlways;
                /**
                 UIControlContentHorizontalAlignmentCenter = 0,//水平居中
                 UIControlContentHorizontalAlignmentLeft = 1,//水平居左
                 UIControlContentHorizontalAlignmentRight = 2,//水平居右
                 UIControlContentHorizontalAlignmentFill = 3, };//水平适应
                 */
                //设置文本垂直居中
                textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
                //添加键盘完成按钮注意:这个方法是类方法
                [textField setKeyType];
        
        //设置圆角的半径
        [textField.layer setCornerRadius:15];
         //切割超出圆角范围的子视图
         textField.layer.masksToBounds = YES;
        //设置边框的颜色
         [textField.layer setBorderColor:[UIColor lightGrayColor].CGColor];
         //设置边框的粗细
         [textField.layer setBorderWidth:0.5];
        
        return textField;
    }

             

               2、计算UILabel高度

           

    /**
     计算UILabel 高度
     */
    - (CGSize)getLabelCGSize:(UIFont *)fontSize setTitle:(NSString *)title labelWidth:(CGFloat)labelWidth {
        NSDictionary *attrDict = @{ NSFontAttributeName: fontSize };
        NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:title attributes:attrDict];
        return [attrString boundingRectWithSize:CGSizeMake(labelWidth, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) context:nil].size;
    }

                 3、 设置代理:添加键盘完成按钮,取消第一响应者

    /**
    改变为完成键,如果在项目中导入了YYText框架那么原生的就被替换掉了,
    变为returnKeyType = UIKeyboardTypeTwitter;
    */
    - (void) setKeyType{
        self.delegate = self;
        self.returnKeyType = UIReturnKeyDone;
    }
    //实现UITextField代理方法
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [self resignFirstResponder];//取消第一响应者
        return YES;
    }

            

  • 相关阅读:
    实验2实验报告
    实验1实验报告
    汇编实验九
    汇编实验5
    汇编实验四
    汇编实验三
    汇编实验二
    汇编实验一
    汇编第一章
    浅谈webpack4.0 性能优化
  • 原文地址:https://www.cnblogs.com/huihuizhang/p/12881716.html
Copyright © 2011-2022 走看看