一、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(@"按钮被点击");
   
 
运行结果:
 

UILabel和UIButton的更多相关文章

  1. 1.注册或登录页面设计:UILabel,UIButton,UITextField

    学习iOS开发已经有一段时日了,之前一直没有系统的对iOS开发的相关知识进行归纳总结,导致很多知识点云里雾里在脑子里形不成iOS开发的思想,现将自己在学习过程中遇到的一些知识进行总结,希望能对iOS初 ...

  2. iOS UILabel UITextView UIButton 等等显示文本行间距

    iOS UILabel  UITextView UIButton 等等显示文本行间距都用如下方法 NSMutableParagraphStyle *paragraphStyle = [[NSMutab ...

  3. iOS开发-UILabel和UIButton添加下划线

    关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选择 ...

  4. UILabel和UIButton添加下划线

    关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的 NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选 ...

  5. iOS学习21之UILabel, UITextField, UIButton, UIImageView

    1.UILabel 1> 概述 UILabel (标签): 是显示文本的控件.在App中 UILabel 是出现频率最高的控件 UILabel 是 UIView 子类,作为子类一般是为了扩充父类 ...

  6. UILabel,UITextField,UIButton三大基础控件总结

    (一)UILabel空件 属性: 1.背景颜色 label.backgroundColor = [UIColor ***]; 2. 显示文字: label.text = @"******&q ...

  7. IOS UIlabel 、UIButton添加下划线

    1.给UILabel 添加下划线 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )]; label.backgrou ...

  8. UILabel和UIbutton(富文本)封装方法

    /** 方法说明:设置label的富文本属性 参数说明:contentStr富文本内容 textColor字体颜色 rangeSet设置字体颜色及大小的位置 */ - (UILabel *)backf ...

  9. 你真的了解UIButton、UILabel 吗?

    一:首先查看一下关于UIButton的定义 @class UIImage, UIFont, UIColor, UIImageView, UILabel; //设置UIButton的样式 typedef ...

随机推荐

  1. JS动态获取数据

    JS访问数据,有实时获取数据的时候,请加上时间戳 如:'&stampflag=' + Math.round(new Date().getTime() / 1000); 因为有的浏览器(如IE9 ...

  2. Python自动化 【第六篇】:Python基础-面向对象

      目录: 面向过程VS面向对象 面向对象编程介绍 为什么要用面向对象进行开发 面向对象的特性:封装.继承.多态 面向过程 VS 面向对象 面向过程编程(Procedural Programming) ...

  3. C#调用C++的dll

    在类中添加引用 using System.Runtime.InteropServices; 在程序中写 //关闭连接 [DllImport("opapi2.dll", EntryP ...

  4. VirtualBox 内的 Ubuntu Server 虚拟机网络配置

    环境: 宿主机:Windows 7,单网卡: 虚拟机:ubuntu-14.04.1-server-amd64: 宿主机上网是连接的路由器,IP 地址是通过 DHCP 服务自动获取的: 基本情况: Ub ...

  5. git撤销commit

    请参考该文章:http://www.cnblogs.com/ningkyolei/p/5026011.html 场景: 不小心commit了一个不应该commit的修改,但是还没有push,想撤销那个 ...

  6. [Swift]基础

    [Swift]基础 一, 常用变量 var str = "Hello, playground" //变量 let str1="Hello xmj112288" ...

  7. Origin9.1如何绘制风向玫瑰图(Binned Data)?

    Origin9.1如何绘制风向玫瑰图(Binned Data)? 时间:2014/5/14 21:02:44 点击: 2624 核心提示:今天为大家介绍下如何使用Origin9.1绘制如下图所示的风向 ...

  8. ffmpeg未整理好,有时间整理下

    v  容器(Container) v  容器就是一种文件(封装)格式,比如flv.mkv.ts.mp4.rmvb.avi等.包含下面5种流以及文件头信息. v  流(Stream) v  是一种视频数 ...

  9. 解决KDE桌面环境下Eclipse崩溃的问题--让Eclipse使用特定的GTK2主题运行

    最近在Kubuntu14.04上安装Eclipse,由于Ubuntu软件中心中的版本太老(3.8),而且会自动安装OpenJDK,于是到官网下载最新的4.4版.(Luna,代号很有亲切感有木有,女神万 ...

  10. poj 3264 Balanced Lineup (RMQ)

    /******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...