UIButton的使用

本文详细介绍了UIButton控件的使用方法,包括如何创建按钮、设置按钮的外观属性(如标题、颜色、字体等)、添加响应事件及如何根据不同状态设置不同的显示效果。

UIButton是按钮视图控件,是UIView的子类,也拥有和view一样的属性,同时也有自己特有的属性;

UIButton的主要作用是用来响应,或控制其他事件的发生;比如说显示,或隐藏其他控件,或调用其他函数方法。


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. UILabel *lable01 = [[UILabel alloc] initWithFrame:CGRectMake(0.00.0300.040.0)];  
  2. lable01.backgroundColor = [UIColor yellowColor];  
  3. lable01.text = @"按钮在控制我的显示,或隐藏";  
  4. lable01.textColor = [UIColor redColor];  
  5. [self.view addSubview:lable01];  
  6. lable01.hidden = YES;  
  7. lable01.center = self.view.center;  
  8. lable01.tag = 2000;  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 实例化  
  2. UIButton *button001 = [[UIButton alloc] initWithFrame:CGRectMake(10.050.0120.040.0)];  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 是view的子类,拥有view一样的属性  
  2. [self.view addSubview:button001];  
  3. //    button001.backgroundColor = [UIColor yellowColor];  
  4. //    button001.layer.cornerRadius = 5.0;  
  5. //    button001.layer.masksToBounds = YES;  
  6. //    button001.layer.borderWidth = 0.5;  
  7. //    button001.layer.borderColor = [UIColor redColor].CGColor;  
  8. button001.tag = 1000;  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 自己特有的属性  
  2. // 1 选中状态,默认是NO,即未选中;通常结合响应方法进行使用  
  3. button001.selected = NO;  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 2 按钮标题颜色,可以根据响应状态进行设置,比如常规,或高亮,或选中等  
  2. [button001 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];  
  3. [button001 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];  
  4. [button001 setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 3 按钮标题  
  2. // 3-1 大小  
  3. button001.titleLabel.font = [UIFont systemFontOfSize:12.0];  
  4. // 3-2 对齐方式,默认居中  
  5. //    button001.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;  
  6. //    button001.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 4 按钮标题,同样也可以根据响应状态进行设置  
  2. [button001 setTitle:@"显示标签" forState:UIControlStateNormal];  
  3. [button001 setTitle:@"隐藏标签" forState:UIControlStateHighlighted];  
  4. [button001 setTitle:@"隐藏标签" forState:UIControlStateSelected];  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 5 设置图片,默认图标在左,标题在右,还可通过设置改变显示样式,比如图片在上,标题在下,或图片在右,标题在左。  
  2. [button001 setImage:[UIImage imageNamed:@"imageNormal"] forState:UIControlStateNormal];  
  3. [button001 setImage:[UIImage imageNamed:@"imageSelected"] forState:UIControlStateSelected];  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 6 设置背景图片  
  2. [button001 setBackgroundImage:[UIImage imageNamed:@"bgImageNormal"] forState:UIControlStateNormal];  
  3. [button001 setBackgroundImage:[UIImage imageNamed:@"bgImageHighlight"] forState:UIControlStateHighlighted];  
  4. [button001 setBackgroundImage:[UIImage imageNamed:@"bgImageSelected"] forState:UIControlStateSelected];  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 7 其他属性  
  2. // 7-1 如当前显示标题  
  3. NSString *title = button001.currentTitle;  
  4. NSLog(@"title=%@",title);  
  5.       
  6. // 7-2 点击属性,默认为YES,即可点击  
  7. // 方法1  
  8. //    BOOL isEnable = button001.enabled;  
  9. //    NSLog(@"isEnable=%@",@(isEnable));  
  10. //    button001.enabled = NO; // 设置为不可用  
  11. //    isEnable = button001.enabled;  
  12. //    NSLog(@"isEnable=%@",@(isEnable));  
  13. // 方法2 view的userInteractionEnabled属性,默认为YES,即可点击  
  14. BOOL isEnable = button001.userInteractionEnabled;  
  15. NSLog(@"isEnable=%@",@(isEnable));  
  16. button001.userInteractionEnabled = NO// 设置为不可用  
  17. isEnable = button001.userInteractionEnabled;  
  18. NSLog(@"isEnable=%@",@(isEnable));  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // target方法  
  2. [button001 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];  


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. - (void)buttonClick:(UIButton *)button  
  2. {  
  3.     button.selected = !button.selected;  
  4.       
  5.     UILabel *label = (UILabel *)[self.view viewWithTag:2000];  
  6.     label.hidden = !button.selected;  
  7.       
  8.     NSLog(@"按钮被点击了");  
  9. }  




使用 `Sunny.UI` 中的 `UIButton` 控件时,开发者可以像使用标准 WinForm 控件一样进行操作。该控件支持多种样式和主题设置,适合用于构建具有现代外观的 Windows 应用程序界面。 创建一个 `UIButton` 实例的基本步骤如下: ```csharp // 创建一个 UIButton 实例 UIButton btnSubmit = new UIButton(); // 设置按钮上的文本 btnSubmit.Text = "提交"; // 设置按钮的位置 btnSubmit.Location = new Point(100, 100); // 设置按钮的样式为蓝色 btnSubmit.Style = UIStyle.Blue; // 将按钮添加到窗体的控件集合中 this.Controls.Add(btnSubmit); ``` 对于更复杂的场景,比如设置窗体的主题,可以使用 `UIMainFormHelper` 类来完成。以下是一个完整的示例,展示了如何在一个继承自 `UIForm` 的窗体类中初始化一个按钮,并设置窗体的主题[^2]: ```csharp using Sunny.UI; public partial class MainForm : UIForm { public MainForm() { InitializeComponent(); // 设置窗体主题为暗色系 UIMainFormHelper.SetTheme(this, UITheme.Dark); // 创建并添加一个SunnyUI按钮 UIButton button = new UIButton(); button.Text = "Click me!"; button.Size = new Size(100, 30); button.Location = new Point(10, 10); this.Controls.Add(button); } } ``` 通过这些代码片段,可以看到 `UIButton` 的基本用法以及如何将其集成到基于 `Sunny.UI` 构建的应用程序中。开发者可以根据具体需求调整属性,如 `Text`, `Location`, `Size` 和 `Style` 等,以实现所需的功能和视觉效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值