How to get left padding on UITextField leftView image?

后端 未结 4 1605
忘了有多久
忘了有多久 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 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;
    

提交回复
热议问题