zoukankan      html  css  js  c++  java
  • UI之UIButton--属性及用法


    1
    /*一、简单说明 2 一般情况下,点击某个控件后,会做出相应反应的都是按钮 3 按钮的功能比较多, 既能显示文字,又能显示图片 ,还能随时调整内部图片和文字的位置 4 二、按钮的三种状态 5 normal (普通状态) 6 默认情况( Default ) 7 对应的枚举常量: UIControlStateNormal 8 highlighted (高亮状态) 9 按钮被按下去的时候(手指还未松开) 10 对应的枚举常量: UIControlStateHighlighted 11 disabled (失效状态,不可用状态) 12 如果 enabled 属性为 NO ,就是处于 disable 状态,代表按钮不可以被点击 13 对应的枚举常量: UIControlStateDisabled 14 三、 注意点 15 (1) 从Xcode5开始,图片资源都放到Images.xcassets中进行管理,可以使用拖拽的方式添加项目中用到的图片到Images.xcassets中 16 (2)若干多个控件共用一段代码,通常使用tag. */ 17 18 // 初始化按钮并设置类型 19 UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 20 // 能够定义的UIButton类型有以下6种: 21 22 // typedef enum { 23 // UIButtonTypeCustom = 0, 自定义风格 24 // UIButtonTypeRoundedRect, 圆角矩形 25 // UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用 26 // UIButtonTypeInfoLight, 亮色感叹号 27 // UIButtonTypeInfoDark, 暗色感叹号 28 // UIButtonTypeContactAdd, 十字加号按钮 29 // } UIButtonType; 30 // 按钮类型是枚举类型的,不可以自定义,只可以初始化按钮的时候选择提供给我们的按钮类型。 31 32 // 设置按钮大小和位置 33 btn.frame = CGRectMake(100, 100, 50, 30); 34 // 按钮初始化是设置大小和位置 35 // UIButton* btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 50, 30)]; 36 37 // 设置按钮背景颜色 38 // [btn setBackgroundColor:[UIColor blueColor]]; 39 // btn.backgroundColor = [UIColor blueColor]; 40 // btn.backgroundColor = [UIColor colorWithRed:0.806 green:0.553 blue:0.802 alpha:1.000]; 41 // btn.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.8]; 42 // 0x4fd 十六进制前面加 0x 除以 255点 226 除 以255点 43 btn.backgroundColor = [UIColor colorWithRed:0x4fd/255. green:226/255. blue:0.802 alpha:1.000]; 44 45 // 设置按钮透明度(0 -1之间) 46 btn.alpha = 0.5; 47 48 // 设置按钮隐藏状态 49 btn.hidden = YES; 50 // [btn setHidden:YES]; 51 52 // 设置按钮点击是高亮 53 btn.showsTouchWhenHighlighted = YES; 54 // [btn setShowsTouchWhenHighlighted:YES]; 55 56 // 设置按钮图片 57 UIImage* im = [UIImage imageNamed:@"imageName"]; 58 [btn setImage:im forState:UIControlStateNormal]; 59 // [btn setImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal]; 60 61 // 设置按钮背景图片 62 [btn setBackgroundImage:im forState:UIControlStateNormal]; 63 // [btn setBackgroundImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal]; 64 65 // 设置按钮文字 66 [btn setTitle:@"dianji" forState:UIControlStateNormal]; // 正常状态显示 67 [btn setTitle:@"yanshi" forState:UIControlStateSelected]; // 选择状态显示 68 btn.selected = YES; 69 70 // 设置按钮文本颜色 71 [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 72 73 // forState这个参数的作用是定义按钮的文字或图片在何种状态下才会显现,以下是几种状态: 74 // enum { 75 // UIControlStateNormal = 0, 常规状态显现 76 // UIControlStateHighlighted = 1 << 0, 高亮状态显现 77 // UIControlStateDisabled = 1 << 1, 禁用的状态才会显现 78 // UIControlStateSelected = 1 << 2, 选中状态 79 // UIControlStateApplication = 0x00FF0000, 当应用程序标志时 80 // UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他 81 // }; 82 83 //设置按钮上的字体的大小 84 // [btn setFont: [UIFont systemFontSize: 14.0]]; //这种可以用来设置字体的大小,但是在SDK版本中去除了 85 //应该使用 86 btn.titleLabel.font = [UIFont systemFontOfSize:14.0]; 87 // [btn.titleLabel setFont:[UIFont systemFontOfSize:17]]; 88 89 // 设置按钮文本对其方式(右对齐) 90 btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; 91 92 [btn.layer setMasksToBounds:YES]; 93 94 // 设置按钮四个圆角半径 95 btn.layer.cornerRadius = 4.0; 96 // [btn.layer setCornerRadius:4.0]; 97 98 // 设置按钮边框宽度 99 [btn.layer setBorderWidth:0.5]; 100 // btn.layer.borderWidth = 0.5; 101 102 // 设置按钮边框颜色 103 CGColorRef colorref = CGColorCreate(CGColorSpaceCreateDeviceRGB(),(CGFloat[]){168/255.0f, 168/255.0f, 168/255.0f, 1.0}); 104 [btn.layer setBorderColor:colorref]; 105 // [btn.layer setBorderColor:[UIColor blueColor].CGColor]; 106 // btn.layer.borderColor = [UIColor redColor].CGColor; 107 108 // 设置按钮标签 109 btn.tag = 1;

    ios7.1下的button的enable属性设置为no,会将button隐藏;而在7.1之前的button enable属性设置为no不会隐藏button,而是显示灰色,点击不作用。
    110 111 // 去除按钮在叠加视图中的按下延迟 112 // tableView.delaysContentTouches = NO; 113 114 // 添加点击事件 115 [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; 116 117 // 在视图中显示按钮 118 [self.view addSubview:btn]; 119 } 120 // 按钮点击事件 121 - (void)btnAction:(id)sender 122 { 123 // do something 124 NSLog(@"被点击了。。。。。"); 125 } 126 /*====================================================== 127 tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)]; 128 这样初始化的button,文字默认颜色是白色的,所有如果背景也是白色的话,是看不到文字的, 129 btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//设置文字位置,现设为居左,默认的是居中 130 [btn setTitle:@“title”forState:UIControlStateNormal];// 添加文字 131 有些时候我们想让UIButton的title居左对齐,我们设置 132 btn.textLabel.textAlignment = UITextAlignmentLeft 133 是没有作用的,我们需要设置 134 btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft; 135 但是问题又出来,此时文字会紧贴到做边框,我们可以设置 136 btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0); 137 使文字距离做边框保持10个像素的距离。 138 ======================================== 139 在OC中,不允许直接修改“对象”的“结构体属性”的“成员”,但是允许修改“对象”的“结构体属性” 140 修改结构体属性的成员方法如下: 141 1> 使用临时变量记录对象的结构体属性 142 2> 修改临时变量的属性 143 3> 将临时变量重新设置给对象的结构体属性 144 3. 在程序开发中需要避免出现魔法数字(Magic Number) 145 ======================================== 146 使用枚举类型,可以避免在程序中出现魔法数字 147 1> 枚举类型实质上就是一个整数,其作用就是用来替代魔法数字 148 2> 枚举类型中,指定了第一个整数之后,后面的数字会递增 149 4. frame & bounds & center */
    
    

     

  • 相关阅读:
    MySQL 使用 ON UPDATE CURRENT_TIMESTAMP 自动更新 timestamp (转)
    MySQL数据类型:UNSIGNED注意事项(转)
    MySQL慢查询参数
    elasticsearch中mapping的_source和store的笔记(转)
    python 使用 elasticsearch 常用方法(聚合)
    python 使用 elasticsearch 常用方法(检索)
    python 使用 elasticsearch 常用方法(索引)
    ElasticSearch集群状态查看命令大全(转)
    用C#实现修改网页数据
    基于jQuery可悬停控制图片轮播代码
  • 原文地址:https://www.cnblogs.com/WillingToAsk1946zzh/p/4458701.html
Copyright © 2011-2022 走看看