美文网首页ios开发笔记iOS开发指南iOS知识
iOS UI类汇总(UIlabel UIbutton ...)

iOS UI类汇总(UIlabel UIbutton ...)

作者: 轻斟浅醉17 | 来源:发表于2016-06-05 01:17 被阅读762次

    一 .首先介绍UIButton按钮
    IOS开发中最常用的控件,需要了解其基本定义和常用设置,以便在开发在熟练运用。1、UIButton的定义

    UIButton *button=[[UIButton buttonWithType:(UIButtonType);
    

    能够定义的button类型有以下6种,

    typedef enum {
    UIButtonTypeCustom = 0, 自定义风格
    UIButtonTypeRoundedRect, 圆角矩形
    UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
    UIButtonTypeInfoLight, 亮色感叹号
    UIButtonTypeInfoDark, 暗色感叹号
    UIButtonTypeContactAdd, 十字加号按钮
    } UIButtonType;
    

    2、设置frame

     button1.frame = CGRectMake(20, 20, 280, 40);
    [button setFrame:CGRectMake(20,20,50,50)];
    

    3、button背景色

    button1.backgroundColor = [UIColor clearColor];
    [button setBackgroundColor:[UIColor blueColor]];
    

    4、state状态
    forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现

     enum {
     UIControlStateNormal = 0, 常规状态显现
     UIControlStateHighlighted = 1 << 0, 高亮状态显现
     UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
     UIControlStateSelected = 1 << 2, 选中状态
     UIControlStateApplication = 0x00FF0000, 当应用程序标志时
     UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管
    };
    @property(nonatomic,getter=isEnabled)BOOL enabled;
    @property(nonatomic,getter=isSelected)BOOL selected;
    @property(nonatomic,getter=isHighlighted)BOOL highlighted;
    

    5 、设置button填充图片和背景图片

    [button setImage:[UIImage imageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];
    [buttonsetBackgroundImage:  [UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];
    

    6、设置button标题和标题颜色

    [button1 setTitle:@"点击" forState:UIControlStateNormal];               
    [button setTitleColor:[UIColorredColor]forState:UIControlStateNormal];
    

    7、设置按钮按下会发光

    button.showsTouchWhenHighlighted=NO;
    

    8、添加或删除事件处理

    [button1 addTarget:self action:@selector(butClick:)    forControlEvents:UIControlEventTouchUpInside];
    [btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
    

    9、 设置按钮内部图片间距和标题间距

    UIEdgeInsets insets; // 设置按钮内部图片间距
    insets.top = insets.bottom = insets.right = insets.left = 10;
    bt.contentEdgeInsets = insets;
    bt.titleEdgeInsets = insets; // 标题间距
    

    二.其次UILabel
    //创建UILabel

    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)];
    

    //设置背景色

    label1.backgroundColor = [UIColor grayColor];
    

    //设置tag

    label1.tag = 91;
    

    //设置标签文本

    label1.text = @"轻斟浅醉17真帅";
    

    //设置标签文本字体和字体大小

     label1.font = [UIFont fontWithName:@Arial size:30];
    

    //设置文本对齐方式

     label1.textAlignment = UITextAlignmentCenter;
    

    //文本对齐方式有以下三种

    typedef enum {
           UITextAlignmentLeft = 0,左对齐
           UITextAlignmentCenter,居中对齐
           UITextAlignmentRight, 右对齐
           } UITextAlignment;
    

    //文本颜色

    label1.textColor = [UIColor blueColor];
    

    //超出label边界文字的截取方式

    label1.lineBreakMode = UILineBreakModeTailTruncation;
    

    //截取方式有以下6种

    typedef enum {
          UILineBreakModeWordWrap = 0, 以空格为边界,保留整个单词
          UILineBreakModeCharacterWrap, 保留整个字符
          UILineBreakModeClip, 到边界为止
          UILineBreakModeHeadTruncation, 省略开始,以……代替
          UILineBreakModeTailTruncation, 省略结尾,以……代替
          UILineBreakModeMiddleTruncation,省略中间,以……代替,多行时作用于最后一行
          } UILineBreakMode;
    

    //文本文字自适应大小

    label1.adjustsFontSizeToFitWidth = YES;
          //当adjustsFontSizeToFitWidth=YES时候,如果文本font要缩小时
          //baselineAdjustment这个值控制文本的基线位置,只有文本行数为1是有效
            label1.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    

    //有三种方式

    typedef enum {
           UIBaselineAdjustmentAlignBaselines = 0, //默认值文本最上端于label中线对齐
       UIBaselineAdjustmentAlignCenters,//文本中线于label中线对齐
       UIBaselineAdjustmentNone,//文本最低端与label中线对齐
           } UIBaselineAdjustment;
    

    //文本最多行数,为0时没有最大行数限制

    label1.numberOfLines = 2;
    

    //最小字体,行数为1时有效,默认为0.0

    label1.minimumFontSize = 10.0;
    

    //文本高亮

    label1.highlighted = YES;
    

    //文本是否可变

    label1.enabled = YES;
    

    //去掉label背景色

    label1.backgroundColor = [UIColor clearColor];
    

    //文本阴影颜色

    label1.shadowColor = [UIColor grayColor];
    

    //阴影大小

    label1.shadowOffset = CGSizeMake(1.0, 1.0);
    

    //是否能与用户交互

    label1.userInteractionEnabled = YES;
         [self.view addSubview:label1];
    

    时间不早了,大家熬夜的赶紧睡觉吧,有时间我把其他UI类的都整理出来,供大家使用!

    相关文章

      网友评论

      本文标题:iOS UI类汇总(UIlabel UIbutton ...)

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