zoukankan      html  css  js  c++  java
  • iOS开发~视图(UIView)与控件(UIControl)

    1.UIView类

       1.什么是视图

          看得见的都是视图

       2.什么是控件 

         一种特殊的视图,都是UIControl的子类,不仅具有一定的显示外观,还能响应高级事件,与用户交互。严格意义上UILabel不是控件,因为label不能响应用户交互事件。

       3.术语的理解:

       视图:一个大一点的显示区域,里面可以容纳控件,做容器讲

       控件:容器中包含的子元素

    2.UILabel标签

     1. 是什么?

          静态文本内容的展示控件

     2.label属性

        1)text:显示文本的内容

        2)font:显示文本的字体

        3)numberOfLines:默认为1,显示的最大行数,0表示无上限

        4)lineBreakMode:换行模式, 省略头或尾

              NSLineBreakByTruncatingHead, /* Truncate at head of line: "...wxyz" */

              NSLineBreakByTruncatingTail, /* Truncate at tail of line: "abcd..." */

              NSLineBreakByTruncatingMiddle /* Truncate middle of line:  "ab...yz"

        5)adjustsFontSizeToWidth:是否调整字体大小适应控件宽度   YES  ;

        6)  textColor:设置文本的颜色

    例:

    在 ViewController.m中

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        //设计view

        UILabel* label=[[UILabel alloc]init];

        label.frame=CGRectMake(30, 200,400, 60);

        //设置字体颜色

        label.textColor=[UIColor whiteColor];

        //设置最大显示行数

        label.numberOfLines=2;

        //设置标签内容的字体

        label.font=[UIFont systemFontOfSize:20];

        //设置换行模式

        label.lineBreakMode=NSLineBreakByTruncatingHead;

        //调整字体大小

        //label.adjustsFontSizeToFitWidth=YES;

        label.text=@"SunShine  SunShine  SunShine  SunShine   SunShine  SunShine   SunShine  SunShine  SunShine  SunShine";

        //添加到控制器自带的那个视图里面

        [self.view addSubview:label];

    }

    @end

    效果如下:

     

    3.UIButton按钮

       1.什么是按钮?

            可以与用户交互,能够点击的一种控件

       2.创建方式

             工厂方法创建,使用系统模式

       3.常用属性

           1)frame :按钮大小

           2)backgroundColor:按钮背景色

           3)setBackgroundImage:按钮背景图

                 1.点击images.xcassets文件,将要添加的图片拖拉进文本框,左边框修改图片名字

                 2.点击下方Show Slicing按钮

                 3.在下方进行缩小放大操作

                 4.点击图片中Start Slicing按钮进行裁剪,再点击中间按钮

                 5.九切片:横3线:1线范围不变,1-2线之间复制,2-3线裁剪省掉,3线不变

                 6.将照片名字添加到程序

          //[button setBackgroundImage:[UIImage imageNamed:@"2a"] forState:UIControlStateNormal];

            4)tintColor:按钮字体颜色

            5)  setTitle:点击按钮的状态

          UIControlStateNormal       = 0, 正常按下

          UIControlStateHighlighted  = 1 << 0, 长按状态

          UIControlStateDisabled     = 1 << 1,  

          UIControlStateSelected     = 1 << 2,

     4.添加事件

    ***点一次按钮,执行调用一次方法

           addTarget:为按钮添加响应事件,即点击按钮时需实现的功能

    参数:  1.target:让当前控制器对象成为处理响应的对象

               2.action:处理事件的对象所使用的方法

               3.events:添加对按钮的什么事件的处理

             [button addTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside];

    例:  

    在ViewController.m中

    #import "ViewController.h"

    @interface  ViewController ()

    //设置全局变量

    @property(nonatomic,strong)UILabel *label; 

    @end

    @implementation  ViewController

    -(void)viewDidLoad

    {

        [super viewDidLoad];

        //使用工厂方法创建button对象

        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

        //设置frame属性

        button.frame = CGRectMake(100,200,100, 40);

        //设置按钮上的文字

        [button setTitle:@"切换" forState:UIControlStateNormal];

        // [button setTitle:@"KO" forState:UIControlStateHighlighted];

        //设置按钮的背景色

        //button.backgroundColor = [UIColor lightGrayColor];

        //设置按钮的背景图

        //[button setBackgroundImage:[UIImage imageNamed:@"2a"] forState:UIControlStateNormal];

        //设置按钮的图片

        [button setImage:[UIImage imageNamed:@"qw"] forState:UIControlStateNormal];

        //为按钮添加响应事件

        //target:让当前控制器对象成为处理响应的对象

        //action:处理事件的对象所使用的方法

        //events:添加对按钮的什么事件的处理

        [button addTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside];

        //添加按钮到视图中

        [self.view addSubview:button];

        //添加一个UILable

        UILabel *label = [[UILabel alloc]init];

        self.label = label;

        label.frame = CGRectMake(130,150,400, 40);

        label.text = @"SunShine";

        [self.view addSubview:label];

    }

    //处理事件的对象所使用的方法

    -(void)Click{

        self.label.text = @"Hello SunShine";   

    }

    @end

    效果如下:            

    在AppDelegate.h中

    @implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    {

       self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

        //self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

        self.window.backgroundColor=[UIColor greenColor];

        //1.创建控制器的实例  myVC自带一个视图

        ViewController* myVC=[[ViewController alloc]init];

        //2.将控制器设置为window的根视图控制器

        self.window.rootViewController=myVC;

        //self.window.rootViewController=[ViewController new];

        [self.window makeKeyAndVisible];

        return YES;

    }

    做一个小的应用

    界面中有一个按钮,每次按下按钮,界面多一个UILabel

    要求:

    1)label之间间隔10个点的距离

    2)所有label和屏幕左边距离20个点

    3)所有label宽300,高30

    4)每个Label的内容进行叠加(Hello,Hello

    SunShine,HelloSunShineSunShine,........)

    #import "ViewController.h" 

    @interface ViewController ()

    //@property(nonatomic,strong)UILabel* label;

    @property(nonatomic,assign)int y;

    @property(nonatomic,strong)NSMutableString* str;

    @end

    @implementation  ViewController

    - (void)viewDidLoad{

        [super viewDidLoad];

        UIButton* button=[UIButton buttonWithType:UIButtonTypeSystem];

        button.frame=CGRectMake(130,50,100,40);

        button.backgroundColor=[UIColor whiteColor];

        [button setFont:[UIFont systemFontOfSize:20]];

        button.tintColor=[UIColor redColor];

     [button setTitle:@"呈现" forState:UIControlStateNormal];

      //[button setTitle:@"off" forState:UIControlStateHighlighted];

      //响应事件

      [button addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:button];

        //初始化

        self.y=100;

        self.str=[[NSMutableString alloc]initWithString:@"Hello"];

    }

    -(void)show{

        UILabel* label=[[UILabel alloc]init];

        //label.backgroundColor=[UIColor purpleColor];

        label.text=self.str;

        label.frame=CGRectMake(20,self.y,300,30);

        label.adjustsFontSizeToFitWidth=YES;

        label.font=[UIFont systemFontOfSize:18];

        label.textColor=[UIColor redColor];

        [self.view addSubview:label];

        //只改变当前使用的值,出了范围就是改变后的值

        self.y+=35;

        [self.str appendString:@"SunShine"];

    }

    @end

    效果如下:

  • 相关阅读:
    JVM系列之:String.intern和stringTable
    一文了解JDK12 13 14 GC调优秘籍-附PDF下载
    JVM系列之:对象的锁状态和同步
    JVM系列之:Contend注解和false-sharing
    十分钟搭建自己的私有NuGet服务器-BaGet
    .Net Core in Docker极简入门(下篇)
    .Net Core in Docker极简入门(上篇)
    Flutter 实现酷炫的3D效果
    2020年20个Flutter最漂亮的UI库和项目
    Python 为什么能支持任意的真值判断?
  • 原文地址:https://www.cnblogs.com/yhj1787354782/p/5050659.html
Copyright © 2011-2022 走看看