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

在开发系统登录界面的时候遇到定制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 

@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];



你可能感兴趣的:(IOS)