UIButton的使用

来源:互联网 发布:淘宝店铺男装推荐 编辑:程序博客网 时间:2024/06/07 00:56

本篇引荐别人的代码

这里写图片描述

  UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];#pragma mark------设置位置和使能-------    //设置大小位置    [button setFrame:CGRectMake(16, 30, 200, 200)];    //设置中心点    [button setCenter:self.view.center];    //设置使能    [button setEnabled:YES];#pragma mark-------设置背景颜色/图片------    //设置背景颜色/图片[    [button setBackgroundColor:[UIColor blueColor]];//    [button setBackgroundImage:[UIImage imageNamed:@"www.png"] forState:UIControlStateNormal];//    [button setBackgroundImage:[UIImage imageNamed:@"www.png"] forState:UIControlStateHighlighted];#pragma mark-------设置圆角/边框-------    [button.layer setCornerRadius:50.0];    [button.layer setMasksToBounds:YES];    [button.layer setBorderWidth:10.0];    [button.layer setBorderColor:[[UIColor redColor] CGColor]];#pragma mark--------- 设置标题/字体/颜色/下划线----    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"属性标题"];    NSRange strRange = {0,[attributedString length]};    [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];    [button setAttributedTitle:attributedString forState:UIControlStateNormal];#pragma mark------设置标题和图标偏移------    [button setTitleEdgeInsets:UIEdgeInsetsMake(10, 10, 0, 0)];    [button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 10, 10)];#pragma mark------添加目标动作响应-----       [button addTarget:self action:@selector(holdDownButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];- (void)holdDownButtonTouchUpInside:(UIButton *)sender{    NSLog(@"按住按钮触摸到里面");}
原创粉丝点击