ios 常用基础控件属性
目录
一、UIImageView常用属性
二、UIButton常用属性
三、UITextField常用属性
1、常用属性
2、计算UILabel高度
3、设置代理:添加键盘完成按钮,取消第一响应者
一、UIImageView常用属性
1、图片背景拉伸:类似点9图片
UIImage *image=[UIImage imageNamed:@"icon"]; [image stretchableImageWithLeftCapWidth:image.size.width*0.5 topCapHeight:image.size.height*0.5];
2、让imageView的大小和图片的大小保持一致
//让imageView的大小和图片的大小保持一致 [self.imageView sizeToFit];
二、UIButton常用属性
#pragma mark 设置UIButton属性 - (UIButton *) setUIButtonAttribute:(UIButton *)textButton andPText:(NSString *)text { //设置圆角的半径 [textButton.layer setCornerRadius:15]; //切割超出圆角范围的子视图 textButton.layer.masksToBounds = YES; //设置边框的颜色 [textButton.layer setBorderColor:[UIColor whiteColor].CGColor]; //设置边框的粗细 [textButton.layer setBorderWidth:0.5]; //设置字体文字 [textButton setTitle:text forState:UIControlStateNormal]; //设置字体颜色 [textButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; return textButton; }
三、UITextField常用属性
1、常用属性
#pragma mark 设置UITextField属性 - (UITextField *) setUITextFieldAttribute:(UITextField *)textField andPlaceHolderText:(NSString *)placeHolderText andLeftImageName:(UIImage *)leftImage{ //设置文本提示 textField.placeholder =placeHolderText; //设置光标颜色 textField.tintColor= [UIColor lightGrayColor]; //创建左边图标父控件 UIView *leftViewSup= [[UIView alloc] initWithFrame:CGRectMake(-20, 0, 30, 20)]; UIImageView *imageViewPwd=[[UIImageView alloc]initWithFrame:CGRectMake(8, 0, 18, 20)]; [leftViewSup addSubview:imageViewPwd]; //设置左边图标 imageViewPwd.image=leftImage; //设置图标 textField.leftView=leftViewSup; //只要输入框有内容就出现清空按钮 textField.leftViewMode=UITextFieldViewModeAlways; /** UIControlContentHorizontalAlignmentCenter = 0,//水平居中 UIControlContentHorizontalAlignmentLeft = 1,//水平居左 UIControlContentHorizontalAlignmentRight = 2,//水平居右 UIControlContentHorizontalAlignmentFill = 3, };//水平适应 */ //设置文本垂直居中 textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; //添加键盘完成按钮注意:这个方法是类方法 [textField setKeyType]; //设置圆角的半径 [textField.layer setCornerRadius:15]; //切割超出圆角范围的子视图 textField.layer.masksToBounds = YES; //设置边框的颜色 [textField.layer setBorderColor:[UIColor lightGrayColor].CGColor]; //设置边框的粗细 [textField.layer setBorderWidth:0.5]; return textField; }
2、计算UILabel高度
/** 计算UILabel 高度 */ - (CGSize)getLabelCGSize:(UIFont *)fontSize setTitle:(NSString *)title labelWidth:(CGFloat)labelWidth { NSDictionary *attrDict = @{ NSFontAttributeName: fontSize }; NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:title attributes:attrDict]; return [attrString boundingRectWithSize:CGSizeMake(labelWidth, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) context:nil].size; }
3、 设置代理:添加键盘完成按钮,取消第一响应者
/** 改变为完成键,如果在项目中导入了YYText框架那么原生的就被替换掉了, 变为returnKeyType = UIKeyboardTypeTwitter; */ - (void) setKeyType{ self.delegate = self; self.returnKeyType = UIReturnKeyDone; } //实现UITextField代理方法 - (BOOL)textFieldShouldReturn:(UITextField *)textField { [self resignFirstResponder];//取消第一响应者 return YES; }
4、iOS 改变UITextField placeholder颜色和大小
iOS11之后,使用UITextField时,它的私有属性_placeholderLabel被禁止访问了。所以我们无法像以前一样使用如下方法改变placeholderLabel的属性了
[textField setValue:[UIColor greenColor] forKeyPath:@"_placeholderLabel.textColor"];
OC:
.h
#import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface HanPlaceholderTextField : UITextField @property(nonatomic, strong) UIColor *placeholderColor; @property(nonatomic, strong) UIFont *placeholderFont; @end
.m
#import "HanPlaceholderTextField.h" #import <objc/runtime.h> @interface HanPlaceholderTextField () @end @implementation HanPlaceholderTextField -(void)changePlaceholder{ Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel"); UILabel *placeholderLabel = object_getIvar(self, ivar); placeholderLabel.textColor = _placeholderColor; placeholderLabel.font = _placeholderFont; } -(void)setPlaceholderColor:(UIColor *)placeholderColor{ _placeholderColor = placeholderColor; [self changePlaceholder]; } -(void)setPlaceholder:(NSString *)placeholder{ [super setPlaceholder:placeholder]; [self changePlaceholder]; } -(void)setPlaceholderFont:(UIFont *)placeholderFont{ _placeholderFont = placeholderFont; [self changePlaceholder]; } @end
用法
... #import "HanPlaceholderTextField.h" ... .... HanPlaceholderTextField *textField = [[HanPlaceholderTextField alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 40)]; textField.placeholderColor = [UIColor redColor]; textField.placeholderFont = [UIFont systemFontOfSize:10]; textField.textColor = [UIColor blueColor]; textField.placeholder = @"你好"; [self.view addSubview:textField]; ....