2012-12-07 26 views
0

enter image description here定製的UITextField像這樣

我必須定製的UITextField像上面的圖片,所以我嘗試寫我的自定義類擴展UITextField類。 但我的問題是:RECT的中風陰影輪廓,並設置背景爲白色內 我的自定義代碼文本框是:

- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    self.autocorrectionType = UITextAutocorrectionTypeNo; 
    self.autocapitalizationType = UITextAutocapitalizationTypeNone; 
    CALayer *layer = self.layer; 
    layer.cornerRadius = 15.0; 
    layer.masksToBounds = YES; 
    layer.borderWidth = 1.0; 
    layer.borderColor = [[UIColor colorWithRed:0 green:0 blue:0 alpha:1] CGColor]; 
    [layer setShadowColor: [[UIColor blackColor] CGColor]]; 
    [layer setShadowOpacity:1]; 
    [layer setShadowOffset: CGSizeMake(2.0, 2.0)]; 
    [self setClipsToBounds:NO]; 
    [self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter]; 
    [self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; 
} 
    - (CGRect)textRectForBounds:(CGRect)bounds { 
      return CGRectMake(bounds.origin.x + 20, bounds.origin.y + 8, 
        bounds.size.width - 40, bounds.size.height - 16); 
} 
- (CGRect)editingRectForBounds:(CGRect)bounds { 
      return [self textRectForBounds:bounds]; 
} 

所以我的問題是:我錯過了什麼代碼(這裏設爲白色背景中,爲邊框設置輪廓陰影)感謝您的幫助!

回答

2

你先試試這個嗎?
self.textField.background = myUIImage;
self.textField.borderStyle = UITextBorderStyleNone;

0

使用文本字段框的圖像,並將其放在設置爲自定義的普通UITextField後面,因此透明。這樣你就可以獲得文本字段的功能,並且可以在其後面放置任何圖形。

0

最後,我解決了閱讀一些教程後,我的問題,這是我的代碼,我已經寫了

- (void)drawRect:(CGRect)rect 
{ 

    self.autocorrectionType = UITextAutocorrectionTypeNo; 
    self.autocapitalizationType = UITextAutocapitalizationTypeNone; 
    CALayer *layer = self.layer; 
    layer.backgroundColor = [[UIColor whiteColor] CGColor]; 
    layer.cornerRadius = 15.0; 
    layer.masksToBounds = YES; 
    layer.borderWidth = 1.0; 
    layer.borderColor = [[UIColor colorWithRed:0 green:0 blue:0 alpha:1] CGColor]; 
    [layer setShadowColor: [[UIColor blackColor] CGColor]]; 
    [layer setShadowOpacity:1]; 
    [layer setShadowOffset: CGSizeMake(0, 2.0)]; 
    [layer setShadowRadius:5]; 
    [self setClipsToBounds:NO]; 
    [self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter]; 
    [self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; 
    } 
    - (CGRect)textRectForBounds:(CGRect)bounds { 
     return CGRectMake(bounds.origin.x + 20, bounds.origin.y + 8, 
        bounds.size.width - 40, bounds.size.height - 16); 
    } 
    - (CGRect)editingRectForBounds:(CGRect)bounds { 
     return [self textRectForBounds:bounds]; 
    }