iOS学习(1)——UIButton

来源:互联网 发布:js获取事件源 编辑:程序博客网 时间:2024/06/08 11:59
    //创建自己需要的类型的按钮    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];    //设置按钮的fram    [button setFrame:CGRectMake(0, 0, 50, 50)];    button.frame=CGRectMake(0, 0, 50, 50);    CGRect rect=button.frame;    //设置按钮的tag,用来标识    [button setTag:1];    button.tag=1;    NSInteger tag=button.tag;    //设置按钮背景颜色,在storyboard里面没有这个属性,只能在代码里设置    [button setBackgroundColor:[UIColor greenColor]];    button.backgroundColor=[UIColor greenColor];    //设置按钮是否隐藏    [button setHidden:YES];    button.hidden=NO;    BOOL hidden=button.hidden;    //设置按钮文字的布局    [button setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentRight)];    button.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;    //这种方法不起作用    button.titleLabel.textAlignment=NSTextAlignmentRight;    //设置按钮文字    [button setTitle:@"点我" forState:UIControlStateNormal];    NSString *titleText=button.titleLabel.text;    //这种方法无效    //button.titleLabel.text=@"点我";    //设置按钮文字大小    button.titleLabel.font=[UIFont systemFontOfSize:14];    //已经被废弃    [button setFont:[UIFont systemFontOfSize:14]];    //设置按钮文字颜色    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    //这种方法无效    //button.titleLabel.textColor=[UIColor whiteColor];    //设置文字距离边框的间距    button.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);    //设置按钮图片    [button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];    button.imageView.image=[UIImage imageNamed:@"icon_customer_mine_intrest.png"];    //设置按钮事件    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];    //添加到父控件里    [self.view addSubview:button];
0 0
原创粉丝点击