美文网首页
UILabel & UITextFiled &UIButton

UILabel & UITextFiled &UIButton

作者: 董立权 | 来源:发表于2017-12-12 17:59 被阅读0次
    UILabel 标签
    
    @property(nullable, nonatomic,copy)   NSString           *text;内容
    @property(null_resettable, nonatomic,strong) UIFont      *font; 字体
    @property(null_resettable, nonatomic,strong) UIColor     *textColor; 颜色
    @property(nonatomic) NSInteger numberOfLines; 行数(0为允许任意行)
    @property(nonatomic)        NSTextAlignment    textAlignment;对齐方式
    
    UITextFiled 文本输入框
    
    @property(nullable, nonatomic,copy)   NSString               *text; 内容
    @property(nullable, nonatomic,strong) UIColor                *textColor;内
    容颜色
    @property(nullable, nonatomic,strong) UIFont                 *font; 字体
    @property(nonatomic)        NSTextAlignment         textAlignment; 对齐方式
    @property(nullable, nonatomic,copy)   NSString               *placeholder;提示信息
    @property(nonatomic)        UITextBorderStyle       borderStyle;边框样式
    
    UIButton 按钮
    
    - (void)setTitle:(nullable NSString *)title forState:(UIControlState)state; 设置指定状态的标题
    - (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR;设置指定状态的标题颜色
    - (void)setImage:(nullable UIImage *)image forState:(UIControlState)state;设置指定状态显示的图片
    
    UIControl 控件
    
    - (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;添加监听事件
    参数1:事件触发时执行对象
    参数2:事件触发时执行指定对象中的指定方法
    参数3:监听的事件类型
    
    UIImageView 图片视图
    
    - (instancetype)initWithImage:(nullable UIImage *)image;根据图片创建视图,视图的大小跟图片一样大
    @property (nullable, nonatomic, strong) UIImage *image; 图片
    
    UIImage 图片
    
    + (nullable UIImage *)imageNamed:(NSString *)name;根据名称生成图片
    + (nullable UIImage *)imageWithData:(NSData *)data;根据二进制生成图片
    

    案例1(加法计算器)

    加法计算器
    案例2(小飞机)
    //定义方向枚举
    typedef NS_ENUM(NSInteger, DirType) {
        DIR_UP = 101,
        DIR_LEFT = 102,
        DIR_DOWN = 103,
        DIR_RIGHT = 104
    };
    
    @interface ViewController ()
    //小飞机按钮
    @property (nonatomic,weak) UIButton *planeButton;
    @end
    
    //设置界面
    -(void)setUpUI{
        //设置背景视图
        UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background"]];
        [self.view addSubview:iv];
        
        //设置飞机
        UIButton *planeButton = [[UIButton alloc] init];
        [planeButton setImage:[UIImage imageNamed:@"hero1"] forState:UIControlStateNormal];
        [planeButton setImage:[UIImage imageNamed:@"hero2"] forState:UIControlStateHighlighted];
        [self.view addSubview:planeButton];
        [planeButton sizeToFit];
        planeButton.center = self.view.center;
        planeButton.center = CGPointMake(self.view.center.x, self.view.center.y - 200);
        //记录变量
        self.planeButton = planeButton;
        
        //设置方向按钮
        CGFloat offset = 30;
        //方向按钮根基 偏移值CGPoint normal图片 helight图片 tag生成
        CGPoint center = CGPointMake(self.view.center.x, self.view.center.y + 180);
        [self creatButtonWith:@"top_normal" :@"top_highlighted" :CGPointMake(0, -offset) :DIR_UP :center];
        [self creatButtonWith:@"left_normal" :@"left_highlighted" :CGPointMake(-offset, 0) :DIR_LEFT :center];
        [self creatButtonWith:@"bottom_normal" :@"bottom_highlighted" :CGPointMake(0, offset) :DIR_DOWN :center];
        [self creatButtonWith:@"right_normal" :@"right_highlighted" :CGPointMake(offset, 0) :DIR_RIGHT :center];
        
    }
    
    //创建按钮并添加到视图
    -(void)creatButtonWith:(NSString *)normalName :(NSString *)helightName :(CGPoint)offset :(NSInteger)tag :(CGPoint)center{
        UIButton *btn = [[UIButton alloc] init];
        [self.view addSubview:btn];
        [btn setImage:[UIImage imageNamed:normalName] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:helightName] forState:UIControlStateHighlighted];
        btn.tag = tag;
        btn.frame = CGRectMake(0, 0, 40, 40);
        btn.center = CGPointMake(center.x + offset.x, center.y + offset.y);
        
        [btn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    //点击按钮
    -(void)clickButton:(UIButton *)sender {
        //获取初始位置
        CGRect frame = self.planeButton.frame;
        //每次点击移动的距离
        CGFloat offset = 20;
        //修改位置
        switch (sender.tag) {
            case DIR_UP:
                frame.origin.y -= offset;
                break;
            case DIR_LEFT:
                frame.origin.x -= offset;
                break;
            case DIR_DOWN:
                frame.origin.y += offset;
                break;
            case DIR_RIGHT:
                frame.origin.x += offset;
                break;
                
            default:
                break;
        }
        //赋值
        self.planeButton.frame = frame;
    }
    @end
    
    

    相关文章

      网友评论

          本文标题:UILabel & UITextFiled &UIButton

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