一、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. 1,SFDC 管理员篇 - 基本设置

    1, 公司配置 Setup | Administrator| Company Profile *Company Inforamtion:公司基础信息,License信息,重要的设置包括本地时间,币种, ...

  2. .NET 扩展方法

    .NET 的扩展方法是在.NET 3.0引入的,MSDN给出的定义是:扩展方法使你能够向现有类型“添加”方法(包括你自定义的类型和对象噢),而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩 ...

  3. maximo功能修改笔记

    经过前几次的简单的修改系统功能,对maximo的bean开发已经有了一定了解,现在是耗时近两个礼拜来修改了一项系统功能,所用到的知识 Bean Fld, 下面我认真总结修改功能过程中的学到的知识: 目 ...

  4. [已解决]EnvironmentError: mysql_config not found

    $ pip install MySQL-python==1.2.5 报错: EnvironmentError: mysql_config not found 原因是缺少包 libmysqlclient ...

  5. JAVA的包装类 【转】

    Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的,这在实际使用时存在很多的不便,为了解决这个不足,在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个和基本数 ...

  6. 利用spring、cxf编写并发布webservice

    配置文件spring-wsServer.xml <?xml version="1.0" encoding="UTF-8"?> <beans x ...

  7. GBDT基本理论及利用GBDT组合特征的具体方法(收集的资料)

    最近两天在学习GBDT,看了一些资料,了解到GBDT由很多回归树构成,每一棵新回归树都是建立在上一棵回归树的损失函数梯度降低的方向. 以下为自己的理解,以及收集到的觉着特别好的学习资料. 1.GBDT ...

  8. JQuery之正则表达式

    1.定义正则表达式 /.../  用于定义正则表达式 /.../g 表示全局匹配 /.../i 表示不区分大小写 /.../m 表示多行匹配 2.匹配正则表达式 非全局模式,不分组 var patte ...

  9. 自定义控件TextView

    public class defineTextView extends TextView { Context context; public defineTextView(Context contex ...

  10. 开发中用到的开源框架汇总(Updating)

    SuperWebSocket SuperSocket NPOI 官网:http://npoi.codeplex.com/ 实例讲解:http://www.cnblogs.com/yutian/p/52 ...