UI -- UIButton小结

来源:互联网 发布:js遍历标签数组 编辑:程序博客网 时间:2024/06/07 05:02

圆角,边框,字体

  • 让四角变圆
  • 1
    2
    [[viewlayer]setCornerRadius:10.0];
    [viewsetClipsToBounds:YES];

  • 添加边框:
  • 1
    2
    [[viewlayer]setBorderColor:[[UIColorblackColor]CGColor]];
    [[viewlayer]setBorderWidth:1.0];

写法例子:

  1. 首先需要添加QuartzCore Framework
  2. 在要使用的地方添加头文件

    #import <QuartzCore/QuartzCore.h>

  3. 圆角、边框、字体

UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(250, 45, 60, 30)];

[button setTitle:@"Do" forState:UIControlStateNormal];

[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

//字体大小

button.titleLabel.font=[UIFont systemFontOfSize:14.0];

//圆角

[[button layer] setCornerRadius:10.0];

[button setClipsToBounds:YES];

//边框

[[button layer] setBorderColor:[[UIColor blackColor] CGColor]];

[[button layer] setBorderWidth:1.0];



1.预置按钮类型

  sdk提供了5个预置按钮类型:Detail Disclosure,Info Light,Info Dark,Contact Add,Rounded Rectangle。它们添加到sdk中首先是为了方便苹果公司自己。

  构造方式:[UIButton buttonWithType:UIButtonTypeContactAdd];

 

2.显示系统私有UIButton风格

     指定 值为100 以上的UIButton的buttonWithType可以得到非公开的按钮风格,像红色按钮,黑色按钮,箭头返回按钮等。

     对于某种风格,可以用[button setTintColor:[UIColor blueColor]];来改变按钮颜色。

     参考 

     http://zhaohaiyang.blog.51cto.com/2056753/756082

 

3.图片和文字环绕

   UIButtonTypeCustom按钮可以设置title。

   若置title于图像上面时,可使用setBackgroundImage;

   若置title于图像右边时,可使用setImage,且要设置frame宽度大于图像,以能显示出title文字。

   设置titleEdgeInsets可实现文字到图片下方,不过要经过一翻计算。

   setImage的图的Z坐标是最高的。

 

4.光晕效果

   button.showsTouchWhenHighlighted=YES;点击时的闪光效果会被前景图片遮住中间部分;

   Shows Touch On Highlight (高亮)光晕的大小是55x55像素,大于40x40像素的按钮不能使用该视觉效果。

 

5.指定目标函数传递的参数问题

  例如 

   [button addTarget:self action:@selector(tableView:accessoryButtonTappedForRowWithIndexPath:)  forControlEvents:UIControlEventTouchUpInside];,

  在执行时,传递给tableView函数的参数类型分别是UIButton类型和UITouchesEvent类型。即不论函数原型是什么,button实际传递的参数类型是固定的。

 

6.点击测试UIButton响应UIControlEventTouchUpInside事件时,响应点超出了它button的范围。

 

7.在UIButton中addSubview的问题

   UIView的userInteractionEnabled值默认为YES,必须设置UIButton所有的subview的userInteractionEnabled为NO,才能让UIButton正常响应点击。

   但是如果设置了UIView的setUserInteractionEnabled为NO,其子view都将得不到响应。

 

8.处理双击问题

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

   -(void)onTouchUpInside:(id)sender withEvent:(UIEvent*)event 
{
    UITouch* touch = [[event allTouches] anyObject];
    NSLog(@"onTouchUpInside tagCount:%d",touch.tapCount);

    //判断点击次数
    if (touch.tapCount == 1) 
    {

       //todo
    }
}


设置UIButton的文字显示位置、字体的大小、字体的颜色

分类: iphone界面详解 383人阅读 评论(0) 收藏 举报

btn.frame = CGRectMake(x, y, width, height);

[btn setTitle: @"search" forState: UIControlStateNormal];

//设置按钮上的自体的大小

//[btn setFont: [UIFont systemFontSize: 14.0]];    //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法

//应该使用

btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];

[btn seBackgroundColor: [UIColor blueColor]];

//最后将按钮加入到指定视图superView

[superView addSubview: btn];

==========================================================

tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];

这样初始化的button,文字默认颜色是白色的,所有如果背景也是白色的话,是看不到文字的,

btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//设置文字位置,现设为居左,默认的是居中

[btn setTitle:@“title”forState:UIControlStateNormal];// 添加文字

有些时候我们想让UIButton的title居左对齐,我们设置

btn.textLabel.textAlignment = UITextAlignmentLeft

是没有作用的,我们需要设置

btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

但是问题又出来,此时文字会紧贴到做边框,我们可以设置

btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

使文字距离做边框保持10个像素的距离。

=======================================================

设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:

[btn.titleLabel setTextColor:[UIColorblackColor]];

btn.titleLabel.textColor=[UIColor redColor];

而是用:

[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];