1. 程式人生 > >IOS介面開發UItextField定製全域性元件_自定文字左邊距、圖示

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