UIButton的用法

来源:互联网 发布:中信债券优化净值走势 编辑:程序博客网 时间:2024/05/09 01:44

UIButton的用法

 (2013-06-03 20:22:12)
  
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
   
   
    //非公开的按钮风格,改变按钮颜色方法
//    [button setTintColor:[UIColor blueColor]];

    [button setFrame:CGRectMake((320-200)/2, 150, 200, 50)];
    //设置title---------(一些基本属性都可以设置 普通状态 和 高亮选中状态不同显示)
   
    [button setTitle:@"present1233333"  forState:UIControlStateNormal];
    [button setTitle:@"高亮状态" forState:UIControlStateHighlighted];
   
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ab" ofType:@"png"];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    
     //设置背景图片或者追加图片---------在按钮中:(1)同时有title,image,追加状态
                           //(2)但是image范围太大,会把title挤出去
     
     [button setImage:image forState:UIControlStateNormal] ;
    

    
    [button setTag:102];//设置tag值
    
    //设置背景颜色,图片---------前提是button的type不能为白色圆角矩形,因为太大,button都被白色覆盖了
    button.backgroundColor = [UIColor greenColor];
    [button setBackgroundImage:image forState:UIControlStateHighlighted];
    
    //设置文字颜色
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    //设置阴影---颜色,偏移量
    [button setTitleShadowColor:[UIColor blueColor] forState:UIControlStateNormal];
    button.titleLabel.shadowOffset = CGSizeMake(2, 0);
     
    //设置文字字号

    button.titleLabel.font = [UIFont fontWithName:@"STHeitiTC-Light" size:18];


    //查找文字名称-先找familyName,再找fontName

    NSArray *familyNames = [UIFont familyNames];
    for(NSString *familyName in familyNames)
    {
        NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
        for(NSString *fontName in fontNames)
        {
            NSLog(@"fontName = %@",fontName);
        }
    }
    
    //设置文字所处位置,左对齐,右对齐,水平居中对齐。上对齐,下对齐,垂直居中对齐。
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    
    //设置文字超过button显示时的显示方式(换行符方式)。6种方式
    //  typedef enum {        
    //   UILineBreakModeWordWrap = 0,            // Wrap at word boundaries
    //   UILineBreakModeCharacterWrap,           // Wrap at character boundaries
    //   UILineBreakModeClip,                    // Simply clip when it hits the end of the rect
    //   UILineBreakModeHeadTruncation,          // Truncate省略头部,使用..代替
    //   UILineBreakModeTailTruncation,          // Truncate省略尾部,使用..代替
    //   UILineBreakModeMiddleTruncation,        // Truncate 省略中间,使用..代替
    //   } UILineBreakMode;

    button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
    
    //调整按钮的边间距---整个内容的边间距使用:contentEdgeInsets
    //                 图片使用:imageEdgeInsets属性; 标题使用:titleEdgeInsets
    //如果只设置标题title边距,距离以 标题到图片的距离计算,top = 0;left=30;bottom=0;right=10;
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 10);
    
    
    //如果设置YES,按钮在高亮状态下,图片的颜色会进行自动加深。如果设置成NO,保持原样。default是YES
    //与设置高亮状态,不能一起用。设置高亮了,就不用设置这个了
    //    button.adjustsImageWhenHighlighted = YES;
    
    //设置按钮在按下时发光。这可以用于信息按钮或者有些重要的按钮

    button.showsTouchWhenHighlighted = YES;


    //设置响应代理方法

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

    
    [self.view addSubview:button];
0 0