ios 使用 UITextBorderStyleNone 为 UITextField 设置填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3727068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:44:02  来源:igfitidea点击:

Set padding for UITextField with UITextBorderStyleNone

iosuitextfieldpadding

提问by Sebastian Wramba

I wanted to use a custom background for my UITextFields. This works fine except for the fact that I have to use UITextBorderStyleNoneto make it look pretty. This forces the text to stick to the left without any padding.

我想为我的UITextFields. 这工作得很好,除了事实,我必须使用UITextBorderStyleNone,使它看起来漂亮。这会强制文本在没有任何填充的情况下粘在左侧。

Can I set a padding manually so that it looks similar to UITextBorderStyleRoundedRectexcept for using my custom background image?

UITextBorderStyleRoundedRect除了使用我的自定义背景图像外,我可以手动设置填充以使其看起来类似于吗?

回答by Evil Trout

I found a neat little hack to set the left padding for this exact situation.

我发现了一个整洁的小黑客来设置左填充这个确切的情况。

Basically, you set the leftView property of the UITextFieldto be an empty view of the size of the padding you want:

基本上,您将 leftView 属性设置UITextField为所需填充大小的空视图:

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;

Worked like a charm for me!

对我来说就像一个魅力!

In Swift 3/ Swift 4, it can be done by doing that

Swift 3/ Swift 4 中,可以这样做

let paddingView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 20))
textField.leftView = paddingView
textField.leftViewMode = .always

回答by Nate Flink

I created this category implementation and added it to the top of the .mfile.

我创建了这个类别实现并将它添加到.m文件的顶部。

@implementation UITextField (custom)
    - (CGRect)textRectForBounds:(CGRect)bounds {
        return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 8,
                          bounds.size.width - 20, bounds.size.height - 16);
    }
    - (CGRect)editingRectForBounds:(CGRect)bounds {
        return [self textRectForBounds:bounds];
    }
@end

Based off the link Piotr Blasiak provided. It seemed simpler then creating a whole new subclass, and also simpler then adding the additional UIView. Still, it seems like something is missing to not be able to control the padding inside a text field.

基于 Piotr Blasiak 提供的链接。创建一个全新的子类似乎更简单,添加额外的UIView. 尽管如此,似乎缺少一些东西,无法控制文本字段内的填充。

Swift 4 solution:

斯威夫特 4 解决方案:

class CustomTextField: UITextField {
    struct Constants {
        static let sidePadding: CGFloat = 10
        static let topPadding: CGFloat = 8
    }

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(
            x: bounds.origin.x + Constants.sidePadding,
            y: bounds.origin.y + Constants.topPadding,
            width: bounds.size.width - Constants.sidePadding * 2,
            height: bounds.size.height - Constants.topPadding * 2
        )
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return self.textRect(forBounds: bounds)
    }
}

回答by bandejapaisa

A Swift 3 version for Xcode >6, where you can edit the inset value in Interface Builder / Storyboard.

适用于 Xcode >6 的 Swift 3 版本,您可以在其中编辑 Interface Builder / Storyboard 中的 inset 值。

import UIKit

@IBDesignable
class FormTextField: UITextField {

    @IBInspectable var inset: CGFloat = 0

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: inset, dy: inset)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return textRect(forBounds: bounds)
    }

}

enter image description here

在此处输入图片说明

回答by Inder Kumar Rathore

Edit: Still works in iOS 11.3.1

编辑:仍然适用于 iOS 11.3.1

In iOS 6 myTextField.leftView = paddingView;is causing issue

在 iOS 6myTextField.leftView = paddingView;中导致问题

This solves the problem

这解决了问题

myTextField.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0)

For right aligned text field use CATransform3DMakeTranslation(-5, 0, 0)as mention by latenitecoderin comments

对于右对齐的文本字段,请使用latenitecoder在评论中的CATransform3DMakeTranslation(-5, 0, 0)提及

回答by Brody Robertson

A good approach to add padding to UITextField is to subclass and add an edgeInsets property. You then set the edgeInsets and the UITextField will be drawn accordingly. This will also function correctly with a custom leftView or rightView set.

向 UITextField 添加填充的一个好方法是子类化并添加 edgeInsets 属性。然后设置 edgeInsets,UITextField 将相应地绘制。这也可以使用自定义的 leftView 或 rightView 集正常运行。

OSTextField.h

OSTextField.h

#import <UIKit/UIKit.h>

@interface OSTextField : UITextField

@property (nonatomic, assign) UIEdgeInsets edgeInsets;

@end

OSTextField.m

OSTextField.m

#import "OSTextField.h"

@implementation OSTextField

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.edgeInsets = UIEdgeInsetsZero;
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if(self){
        self.edgeInsets = UIEdgeInsetsZero;
    }
    return self;
}

- (CGRect)textRectForBounds:(CGRect)bounds {
    return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [super editingRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}

@end

回答by Camsoft

Just subclass UITextField like this:

像这样子类化 UITextField :

@implementation DFTextField


- (CGRect)textRectForBounds:(CGRect)bounds
{
    return CGRectInset(bounds, 10.0f, 0);
}

- (CGRect)editingRectForBounds:(CGRect)bounds
{
    return [self textRectForBounds:bounds];
}


@end

This adds horizontal padding of 10 points either side.

这会在两侧增加 10 点的水平填充。

回答by Kirit Vaghela

Objective C Code

目标 C 代码

MyTextField.h

文本字段.h

#import <UIKit/UIKit.h>

@interface MyTextField : UITextField

@property (nonatomic) IBInspectable CGFloat padding;

@end

MyTextField.m

MyTextField.m

#import "MyTextField.h"

IB_DESIGNABLE
@implementation MyTextField

@synthesize padding;

-(CGRect)textRectForBounds:(CGRect)bounds{
    return CGRectInset(bounds, padding, padding);
}

-(CGRect)editingRectForBounds:(CGRect)bounds{
    return [self textRectForBounds:bounds];
}

@end

enter image description here

在此处输入图片说明

回答by 777Q

  1. Create a textfield Custom
  1. 创建文本字段自定义

PaddingTextField.swift

PaddingTextField.swift

import UIKit
class PaddingTextField: UITextField {

@IBInspectable var paddingLeft: CGFloat = 0
@IBInspectable var paddingRight: CGFloat = 0

override func textRectForBounds(bounds: CGRect) -> CGRect {
    return CGRectMake(bounds.origin.x + paddingLeft, bounds.origin.y,
        bounds.size.width - paddingLeft - paddingRight, bounds.size.height);
}

override func editingRectForBounds(bounds: CGRect) -> CGRect {
    return textRectForBounds(bounds)
}}
  1. Set your textfield class is PaddingTextField and custom your padding as you want enter image description hereenter image description here

  2. Enjoy it

  1. 将您的文本字段类设置为 PaddingTextField 并根据需要自定义填充 在此处输入图片说明在此处输入图片说明

  2. 好好享受

final

最终的

回答by Paulo Miguel Almeida

Based on Evil Trout's answer you might wanna create a category to make it easier to use across multiple applications.

根据 Evil Trout 的回答,您可能想要创建一个类别,使其更易于在多个应用程序中使用。

Header file:

头文件:

@interface UITextField (PaddingText)

-(void) setLeftPadding:(int) paddingValue;

-(void) setRightPadding:(int) paddingValue;
@end

Implementation file:

实现文件:

#import "UITextField+PaddingText.h"

@implementation UITextField (PaddingText)

-(void) setLeftPadding:(int) paddingValue
{
    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, paddingValue, self.frame.size.height)];
    self.leftView = paddingView;
    self.leftViewMode = UITextFieldViewModeAlways;
}

-(void) setRightPadding:(int) paddingValue
{
    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, paddingValue, self.frame.size.height)];
    self.rightView = paddingView;
    self.rightViewMode = UITextFieldViewModeAlways;
}

@end

Usage Example

使用示例

#import "UITextField+PaddingText.h"

[self.YourTextField setLeftPadding:20.0f];

Hope it helps you out guys

希望能帮到大家

Cheers

干杯

回答by superarts.org

Swift version:

迅捷版:

extension UITextField {
    @IBInspectable var padding_left: CGFloat {
        get {
            LF.log("WARNING no getter for UITextField.padding_left")
            return 0
        }
        set (f) {
            layer.sublayerTransform = CATransform3DMakeTranslation(f, 0, 0)
        }
    }
}

So that you can assign value in IB

这样你就可以在IB中赋值

IBInspectable setting represented in Interface Builder

Interface Builder 中表示的 IBInspectable 设置