zoukankan      html  css  js  c++  java
  • UILabel和UIButton

    一、UILabel
    1、UILabel:标签,主要用来显示文字。
    创建步骤:
    (1)开辟空间并初始化(如果本类有初始化方法,使用自己的,否则,使用负父类的)。
     
    UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(130, 280, 100, 80)];
     
    (2)设置文本控制的相关属性
     
    textLabel.backgroundColor = [UIColor redColor];//背景颜色
        textLabel.text = @"提示您是否确认购买!;//
       textLabel.textColor = [UIColorgreenColor];//字体颜色
       
       
        textLabel.backgroundColor = [UIColor colorWithRed:60/255.0 green:50/255.0 blue:300/255.0 alpha:1];
       
        textLabel.lineBreakMode =NSLineBreakByCharWrapping;
        textLabel.numberOfLines = 0;
       
        textLabel.font = [UIFont systemFontOfSize:25];//设置字体的大小
       
        textLabel.textAlignment =  NSTextAlignmentCenter;//设置字符串居中格式,此外也可以设置格式为左对齐或者右对齐
     
    (3)添加到父视图上
     
    [self.view addSubview:textLabel];
     
    (4)释放
     
     
    二、UILabel的基本属性
     
    首先创建一个UILabel对象
     
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(130, 360, 100, 50)];
        label1.backgroundColor = [UIColor blueColor];
       
        //对齐方式,默认的是左对齐
        label1.textAlignment = NSTextAlignmentRight;//右对齐
        label1.textAlignment = NSTextAlignmentLeft;//左对齐
        label1.textAlignment = NSTextAlignmentCenter;//居中
     
    1、 text文本属性
    label1.text = @"提示"; //给对象设置内容
     
    2、 textColor文本颜色
    label1.textColor = [UIColor yellowColor];//设置文本颜色
     
    3、 font字体属性
    label1.font = [UIFont systemFontOfSize:20];//设置字体大小
        label1.font = [UIFont boldSystemFontOfSize:20];//字体加租
        label1.font = [UIFont fontWithName:@"Heleretica-Blod" size:30];//自定义字体第一个参数代表字体名,第二个参数代表字体大小
     
    4、 lineBreakMode 断行模式
    label1.lineBreakMode = NSLineBreakByCharWrapping;
    label1.numberOfLines = 0;//换行设置,设置为0则自动定义多少行,也就是自动换行,如果是给的具体的行数,则会生成具体的行数
     
    5、 shadow属性
    label1.shadowColor = [UIColor blueColor];//设置阴影颜色
    label1.shadowOffset = CGSizeMake(2, 3);// 设置阴影
     
    6、highlighted设置高亮
    label1.highlighted = YES;//是否高亮显示  
    label1.highlightedTextColor = [UIColor orangeColor];//设置高亮时的文本颜色
     
    7、 NSLineBreakByTruncatingHead
    label1.lineBreakMode = NSLineBreakByTruncatingHead;//文本超长时,中间的内容 以……方式省略,显示头尾的文字内容。
     
    8、
    NSLineBreakByTruncatingHead//前面部分文字以……方式省略,显示尾部文字内容
     
    9、
    NSLineBreakByTruncatingTail//结尾部分文字以……方式省略,显示前面文字内容
     
    10、 .adjustsFontSizeToFitWidth
    label1.adjustsFontSizeToFitWidth = YES;//字体大小适应label宽度,字体根据label的尺寸,自动调整其大小
     
     
     
     
     
    三、 UIButton
    1、UIButton即按钮控件
    2、创建UIButton
    (1)创建UIButton对象(如果本类有初始化方法,使用自己的,否则,使用负父类的)。
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
     
    (2)设置按钮现显示的相关属性
    button.frame = CGRectMake(100, 100, 160, 160);
     
    [button setTitle:@"按钮" forState:UIControlStateNormal];
     
    [button setTitle:@"选中" forState:UIControlStateSelected];
     
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; //设置文本颜色
       
        button.titleLabel.font = [UIFont boldSystemFontOfSize:30]; //设置字体大小
     
    [button setTitle:@"高亮" forState:UIControlStateHighlighted]; //设置高亮
     
    //此处可设置背景图片
    [button setBackgroundImage:[UIImage imageNamed:@"12"] forState:UIControlStateNormal];//设置背景图,图片自动充满按钮,此时标题会在背景图之上
     
    button.imageEdgeInsets = UIEdgeInsetsMake(-30, 0, 0, 0);//设置图片的偏量
       
        button.titleEdgeInsets = UIEdgeInsetsMake(100, -160, 0, 0);//设置标题的偏量
    button.imageEdgeInsets = UIEdgeInsetsMake(-30, 0, 0, 0);//设置图片的偏量
         //以上偏移量的设置是为了让图片与文字有良好的视觉效果,用户可以根据需要自己设置
     
    button.selected = NO;//设置按钮被选中
       
        NSLog(@"---%d",button.isSelected);//获取按钮的选中状态(是否被选中)
     
     
     
    (3)为按钮添加点击事件
    [button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
     
    //实现点击事件的方法
    //注意此处是定义了一个方法
    -(void)buttonClicked{
        NSLog(@"按钮点击事件");
    }
     
     
    (4)添加按钮到赴视图上用以显示
    [self.view addSubview:button];
     
    (5)按钮无需释放(因为使用的是类方法创建的Button)
     
    3、UIButton的基本属性
    (1)[button setTitle:@"按钮" forState:UIControlStateNormal];
     
    (2)[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; //设置文本颜色
     
    (3)button.titleLabel.font = [UIFont boldSystemFontOfSize:30]; //设置字体大小
     
    (4)[button setTitle:@"高亮" forState:UIControlStateHighlighted]; //设置高亮
     
    (5)button.selected = NO;//设置按钮被选中
     
     
    4、创建多个UIButton
     
    例题:
    #import "ViewController.h"

    @interface ViewController (){
        UILabel *_label;
    }

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.backgroundColor = [UIColor orangeColor];
        button.frame = CGRectMake(170, 100, 50, 50);
        [button addTarget:self action:@selector(buttonClicked1:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 1;
        [self.view addSubview:button];
       
        UIButton *button1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
       
        button1.backgroundColor = [UIColor redColor];
        button1.frame = CGRectMake(170, 180, 50, 50);
        [button1 addTarget:self action:@selector(buttonClicked1:) forControlEvents:UIControlEventTouchUpInside];
        button1.tag = 2;
        [self.view addSubview:button1];
       
       
        _label = [[UILabel alloc]initWithFrame:CGRectMake(190, 250, 50, 50)];
       
        [self.view addSubview:_label];
     
    }
     
    //点击的实现方法
    -(void)buttonClicked1:(UIButton *)btn{
       
        _label.text= [NSString stringWithFormat:@"%zi",btn.tag];
        NSLog(@"按钮%zi被点击",btn.tag);
    }
     
    运行结果:
     
    例题:
    //创建对象
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    //设置属性
        button.backgroundColor = [UIColor grayColor];
        button.frame = CGRectMake(170, 300, 80, 80);
        //赋值
        [button setTitle:@"按钮" forState:UIControlStateNormal];
        [button setTitle:@"选中" forState:UIControlStateSelected];
     
        button.selected = NO;
         //添加事件
        [button addTarget:self action:@selector(buttonClicked1) forControlEvents:UIControlEventTouchUpInside];
         //添加图片
        [button setBackgroundImage:[UIImage imageNamed:@"34"] forState:UIControlStateNormal];
         //设置图片位置
        button.imageEdgeInsets = UIEdgeInsetsMake(30, 0,90 , 0);
         //添加到父视图
       [self.view addSubview:button];
     
    //实现点击的方法
    -(void)buttonClicked1{
        NSLog(@"按钮被点击");
       
     
    运行结果:
     
  • 相关阅读:
    在Spring中使用cache(EhCache的对象缓存和页面缓存)
    halcon 模板匹配 -- 转化 vector_angle_to_rigid
    halcon 模板匹配 -- find_shape_model
    halcon 模板匹配 -- create_shape_model
    C#快速获取指定网页源码的几种方式,并通过字符串截取函数 或 正则 取指定内容(IP)
    C# Socket通讯 本机多网卡,指定网卡通讯
    C# 获取所有网卡信息
    C#关闭退出线程的几种方法
    C#多线程方法 可传参
    C# Datetime 使用详解
  • 原文地址:https://www.cnblogs.com/wxzboke/p/4953605.html
Copyright © 2011-2022 走看看