How to get left padding on UITextField leftView image?

后端 未结 4 1586
忘了有多久
忘了有多久 2021-01-05 03:15

I am setting up a UIImageView as a leftView on a UITextField like so:

UIImageView *envelopeView = [[UIImageView alloc]         


        
相关标签:
4条回答
  • 2021-01-05 03:56

    you can simply try this:

        UIImageView *envelopeView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 30, 30)];
        envelopeView.image = [UIImage imageNamed:@"comment-128.png"];
        envelopeView.contentMode = UIViewContentModeScaleAspectFit;
      UIView *test=  [[UIView alloc]initWithFrame:CGRectMake(20, 0, 30, 30)];
        [test addSubview:envelopeView];
        [self.textField.leftView setFrame:envelopeView.frame];
        self.textField.leftView =test;
            self.textField.leftViewMode = UITextFieldViewModeAlways;
    
    0 讨论(0)
  • 2021-01-05 03:59

    For Swift 3 Users

    Here is what worked for me:

    extension UITextField {
    
     /// set icon of 20x20 with left padding of 8px 
     func setLeftIcon(_ icon: UIImage) {
    
        let padding = 8
        let size = 20
    
        let outerView = UIView(frame: CGRect(x: 0, y: 0, width: size+padding, height: size) )
        let iconView  = UIImageView(frame: CGRect(x: padding, y: 0, width: size, height: size))
        iconView.image = icon
        outerView.addSubview(iconView)
    
        leftView = outerView
        leftViewMode = .always  
      }
    }
    

    test:

    txOrigin.setLeftIcon(icon_location)
    

    result:

    0 讨论(0)
  • 2021-01-05 04:08

    For Swift 4.2 +

    You can use this extension:

    extension UITextField {
        func leftImage(_ image: UIImage?, imageWidth: CGFloat, padding: CGFloat) {
            let imageView = UIImageView(image: image)
            imageView.frame = CGRect(x: padding, y: 0, width: imageWidth, height: frame.height)
            imageView.contentMode = .center
            
            let containerView = UIView(frame: CGRect(x: 0, y: 0, width: imageWidth + 2 * padding, height: frame.height))
            containerView.addSubview(imageView)
            leftView = containerView
            leftViewMode = .always
        }
    }
    
    0 讨论(0)
  • 2021-01-05 04:08

    You can use this. Change your frame according to your need.

    NSTextAttachment*   placeholderImageTextAttachment = [[NSTextAttachment alloc] init];
    placeholderImageTextAttachment.image = [UIImage imageNamed:@"Search"];
    placeholderImageTextAttachment.bounds = CGRectMake(0, -2, 16, 16);
    NSMutableAttributedString*  placeholderImageString = [[NSAttributedString attributedStringWithAttachment:placeholderImageTextAttachment] mutableCopy];
     NSMutableAttributedString*  placeholderString = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"   Search", nil)];
    [placeholderImageString appendAttributedString:placeholderString];
     _txtFieldSearch.attributedPlaceholder = placeholderImageString;
    _txtFieldSearch.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    
    0 讨论(0)
提交回复
热议问题