UI基础之UI控件
//UIButton继承于UIController可以用addTarget方法
//自定义设置按钮的样子
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 45)];
//设置按钮背景颜色
button.backgroundColor=[UIColor blackColor];
//将按钮居中
button.center=self.view.center;
//设置按钮不可用状态,如果设置了该属性,当前按钮不会再响应点击等事件
// button.enabled=NO;
//设置按钮为高亮状态
// button.highlighted=YES;
//
// button.selected=YES;
[button setTitle:@"收藏" forState:UIControlStateNormal];
[button setTitle:@"取消收藏" forState:UIControlStateSelected];
[button setTitle:@"Highlighted" forState:UIControlStateHighlighted];
[button setTitle:@"Disabled" forState:UIControlStateDisabled];
// [button setTitle:@"Selected" forState:UIControlStateSelected];
//为按钮设置一个事件
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)click:(UIButton *) button{
if (button.selected==YES) {
button.selected=NO;
}else{
button.selected=YES;
}
// button.selected=!button.selected;
}
[button setTitle:@"收藏" forState:UIControlStateNormal];
[button setTitle:@"取消收藏" forState:UIControlStateSelected];
//内容位置水平和垂直
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
//移动图片
//上左下右 距边框的距离
[button setImageEdgeInsets:UIEdgeInsetsMake(15, 30, 0, 0)];
[button setTitleEdgeInsets:UIEdgeInsetsMake(55, -10, 0, 0)];
//
//设置不同状态下标题的颜色
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
//设置字体大小
button.titleLabel.font=[UIFont systemFontOfSize:16];
//设置正常状态下加入按钮图标
[button setImage:[UIImage imageNamed:@"h"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"heart"] forState:UIControlStateSelected];
//为按钮增加背景图片
// [button setBackgroundImage:[UIImage imageNamed:@"heart"] forState:UIControlStateNormal];
[button setTitle:@"Highlighted" forState:UIControlStateHighlighted];
[button setTitle:@"Disabled" forState:UIControlStateDisabled];
// [button setTitle:@"Selected" forState:UIControlStateSelected];
UILabel * label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 45)];
label.backgroundColor=[UIColor redColor];
label.center=self.view.center;
//为label设置展示文本
label.text=@"00:00";
//为label设置文本颜色
label.textColor=[UIColor whiteColor];
//为label设置文本对齐方式
label.textAlignment=NSTextAlignmentCenter;
//为label设置字体大小
label.font=[UIFont systemFontOfSize:20];
[self.view addSubview:label];
#pragma mark -
- (void) setSlider{
#warning UISLider 高度没有效果,不用设置太大一般45-50左右最好
_slider=[[UISlider alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
[self.view addSubview:_slider];
//slider的默认范围是0-1,通过设置value属性来控制按钮的滑动位置
_slider.minimumValue=0;//设置最大值
_slider.maximumValue=10;//设置最小值
_slider.center=self.view.center;
_slider.value=5;//设置当前值
#pragma mark 设置按钮的颜色
// [_slider setThumbTintColor:[UIColor redColor]];
#pragma mark 设置按钮图片
[_slider setThumbImage:[UIImage imageNamed:@"channel_friend_love"] forState:UIControlStateNormal];
#pragma mark 设置轨道颜色
// [_slider setMaximumTrackTintColor:[UIColor redColor]];
// [_slider setMinimumTrackTintColor:[UIColor orangeColor]];
#pragma mark 为轨道设置图片
[_slider setMinimumTrackImage:[UIImage imageNamed:@"btlv"] forState:UIControlStateNormal];
[_slider setMaximumTrackImage:[UIImage imageNamed:@"btpurple"] forState:UIControlStateNormal];
// [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeSlider) userInfo:nil repeats:YES];
}
#pragma mark - 改变进度条
- (void)changeSlider{
_slider.value=_slider.value+1;
}
##
#import "ViewController.h"
@interface ViewController ()
{
UITextField * _tf;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor lightGrayColor];
UITextField * tf=[[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 45)];
_tf=tf;
tf.center=self.view.center;
tf.borderStyle=UITextBorderStyleRoundedRect;
//设置输入提示
tf.placeholder=@"请输入账号";
tf.text=@"zhangsan";
tf.textColor=[UIColor redColor];
tf.font=[UIFont systemFontOfSize:20];
tf.adjustsFontSizeToFitWidth=YES;
// tf.enabled=NO;
//在刚开始编辑的时候,将原来的内容清空
tf.clearsOnBeginEditing=YES;
tf.clearButtonMode=UITextFieldViewModeWhileEditing;
// tf.background=[UIImage imageNamed:@"beijing"];
UILabel * label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
// view.backgroundColor=[UIColor blueColor];
label.text=@"🔍";
tf.leftView=label;
tf.leftViewMode=UITextFieldViewModeAlways;
UILabel * label2=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
// view.backgroundColor=[UIColor blueColor];
label2.text=@"×";
tf.rightView=label2;
tf.rightViewMode=UITextFieldViewModeAlways;
//设置成密码格式
tf.secureTextEntry=YES;
// UIView *inputAccess=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 35)];
// inputAccess.backgroundColor=[UIColor greenColor];
// tf.inputAccessoryView=inputAccess;
//
//
// UIView *inputView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 135)];
// inputView.backgroundColor=[UIColor yellowColor];
// tf.inputView=inputView;
//
[self.view addSubview:tf];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//放弃第一响应者(关闭键盘)
[_tf resignFirstResponder];
}
@end