IOS界面开发UItextField定制全局组件_自定文字左边距、图标

来源:互联网 发布:java神奇的海螺 编辑:程序博客网 时间:2024/06/05 22:21

在开发系统登录界面的时候遇到定制UItextField的问题,结合上午的使用定制全局变量的案例总结代码如下:

1.定义UItextField文字的左边距,代码如下:

-(void)setTextFieldLeftPadding:(UITextField *)textField forWidth:(CGFloat)leftWidth{    CGRect frame = textField.frame;    frame.size.width = leftWidth;    UIView *leftview = [[UIView alloc] initWithFrame:frame];    textField.leftViewMode = UITextFieldViewModeAlways;    textField.leftView = leftview;}


2.定义UItextField的图标,代码如下:

//最右侧加图片是以下代码  左侧类似    UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];    text.rightView=image;    text.rightViewMode = UITextFieldViewModeAlways;  typedef enum {    UITextFieldViewModeNever,    UITextFieldViewModeWhileEditing,    UITextFieldViewModeUnlessEditing,    UITextFieldViewModeAlways} UITextFieldViewMode; 
3.在头文件里定制全局变量:

#import <UIKit/UIKit.h>@interface appstoreViewController : UIViewController@property (strong, nonatomic) UIButton *osButton1;@property (strong, nonatomic) UIButton *osButton2;@end

4.修改默认字体颜色

//第一种       UIColor *color = [UIColor whiteColor];      _userName.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"用户名" attributes:@{NSForegroundColorAttributeName: color}];      //第二种   [_userName setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];

5.UIColor的使用RGB三色来表示颜色,RGB的颜色值范围都是在0.0~1.0之间的

 UIColor *color = [UIColor colorWithRed:145.0/255.0 green:151.0/255.0 blue:151.0/255.0 alpha:1];



0 0