Search code examples
iosuitextfieldcalayerplaceholdershadow

Remove shadow from placeholder text in UITextField styled using CALayer


I've created a UITextField from the Interface Builder(IB). Since I wanted custom styling, I selected the None styling in IB. On viewDidLoad, I'm assigning it the following style:

txtEmail.layer.cornerRadius = 8.0;
txtEmail.layer.borderColor = [UIColor colorWithWhite:199.0/255.0 alpha:1].CGColor;
txtEmail.layer.borderWidth = 1.0;
txtEmail.layer.shadowColor = [UIColor blackColor].CGColor;
txtEmail.layer.shadowOpacity = 0.17;
txtEmail.layer.shadowOffset = CGSizeMake(-0.9,0.9);
txtEmail.layer.shadowRadius = 1.1;

// This is to provide a left padding for None styled textfields
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
txtEmail.leftView = paddingView;
txtEmail.leftViewMode = UITextFieldViewModeAlways;

Now my textfield is getting perfectly styled, but my placeholder text (provided in IB) is also inheriting that shadow. How can I get rid of that?


Solution

  • Find the below code its working fine, you need to remove shadowRadius

    txtEmail.backgroundColor = [UIColor clearColor];
    txtEmail.layer.cornerRadius = 8.0;
    txtEmail.layer.borderColor = [UIColor colorWithWhite:199.0/255.0 alpha:1].CGColor;
    txtEmail.layer.borderWidth = 1.0;
    txtEmail.layer.shadowColor = [UIColor blackColor].CGColor;
    txtEmail.layer.shadowOpacity = 0.17;
    txtEmail.layer.shadowOffset = CGSizeMake(-0.9,0.9);
    txtEmail.layer.masksToBounds = false;
    
    // This is to provide a left padding for None styled textfields
    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
    txtEmail.leftView = paddingView;
    txtEmail.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:txtEmail];
    

    Image