Ustaw padding dla UITextField z UITextBorderStyleNone

Chciałem użyć niestandardowego tła dla mojego UITextFields. To działa dobrze, z wyjątkiem faktu, że muszę użyć UITextBorderStyleNone, aby wyglądać ładnie. Wymusza to przyklejenie tekstu do lewej strony bez wypełnienia.

Czy mogę ręcznie ustawić padding tak, aby wyglądał podobnie do UITextBorderStyleRoundedRect z wyjątkiem korzystania z mojego niestandardowego obrazu tła?

Author: Leo Dabus, 2010-09-16

30 answers

Znalazłem zgrabny mały hack, aby ustawić lewy padding dla tej konkretnej sytuacji.

Zasadniczo ustawiasz właściwość leftView UITextField jako pusty widok o żądanym rozmiarze wypełnienia:

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
Zadziałało jak urok dla mnie!

W Swift 3 , można to zrobić robiąc to

let paddingView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 20))
textField.leftView = paddingView
textField.leftViewMode = .always
 782
Author: Evil Trout,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-17 09:29:23

Stworzyłem implementację tej kategorii i dodałem ją do górnej części pliku .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

Na podstawie linku podanego przez Piotra Błasiaka. Wydawało się to prostsze niż utworzenie zupełnie nowej podklasy, a także prostsze niż dodanie dodatkowej UIView. Mimo to wydaje się, że czegoś brakuje, aby nie móc kontrolować wypełnienia wewnątrz pola tekstowego.

Swift 2 Rozwiązanie:

import UIKit

class CustomTextField: UITextField {

    required init?(coder aDecoder: NSCoder){
        super.init(coder: aDecoder)
    }

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

    override func editingRectForBounds(bounds: CGRect) -> CGRect {
        return self.textRectForBounds(bounds);
    }
}
 160
Author: Nate Flink,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-21 09:57:55

Wersja Swift 3 dla Xcode >6, gdzie można edytować wartość Wstaw w interfejsie Builder / Storyboard.

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)
    }

}

Tutaj wpisz opis obrazka

 123
Author: bandejapaisa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-18 09:01:46

Dobrym podejściem do dodania wypełnienia do UITextField jest podklasa i dodanie właściwości edgeInsets. Następnie ustawisz edgeInsets i UITextField zostanie odpowiednio narysowane. Będzie to również działać poprawnie z niestandardowym ustawieniem leftView lub rightView.

OSTextFieldh

#import <UIKit/UIKit.h>

@interface OSTextField : UITextField

@property (nonatomic, assign) UIEdgeInsets edgeInsets;

@end

OSTextFieldm

#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
 65
Author: Brody Robertson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-04 20:30:22

Edit: nadal działa w iOS 11.3.1

W iOS 6 myTextField.leftView = paddingView; powoduje problem

To rozwiązuje problem

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

Dla wyrównanego do prawej pola tekstowego użyj CATransform3DMakeTranslation(-5, 0, 0) jako wzmianki przez latenitecoder w komentarzach

 64
Author: Inder Kumar Rathore,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-05 09:29:13

Po prostu podklasa UITextField w ten sposób:

@implementation DFTextField


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

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


@end

Dodaje poziome wyściełanie po 10 punktów z każdej strony.

 24
Author: Camsoft,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-08 09:50:05

Objective C Code

MyTextField.h

#import <UIKit/UIKit.h>

@interface MyTextField : UITextField

@property (nonatomic) IBInspectable CGFloat padding;

@end

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

Tutaj wpisz opis obrazka

 19
Author: Kirit Vaghela,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-28 05:35:09

Na podstawie odpowiedzi Evil Trout możesz utworzyć kategorię, aby ułatwić korzystanie z niej w wielu aplikacjach.

Plik nagłówka:

@interface UITextField (PaddingText)

-(void) setLeftPadding:(int) paddingValue;

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

Plik implementacji:

#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

Przykład Użycia

#import "UITextField+PaddingText.h"

[self.YourTextField setLeftPadding:20.0f];
Mam nadzieję, że to wam pomoże.]}

Cheers

 18
Author: Paulo Miguel Almeida,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-22 15:28:49
  1. Utwórz własne pole tekstowe

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. Ustaw klasę textfield jako PaddingTextField i dostosuj ją do własnych potrzeb Tutaj wpisz opis obrazka Tutaj wpisz opis obrazka

  2. Enjoy it

finał

 16
Author: 777Q,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-11 03:18:36

Wersja Swift:

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)
        }
    }
}

Aby można było przypisać wartość w IB

Ibinspectable ustawienie reprezentowane w Interface Builder

 12
Author: superarts.org,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-10 07:45:19

Nie możesz ustawić paddingu. Zamiast tego miej UIView, który zawiera twój obraz tła i UITextField wewnątrz niego. Ustaw UITextField Szerokość jako UIViewWidth-(paddingSize x 2) i wysokość podobnie, a następnie ustaw ją w punkcie paddingSize,paddingSize.

 11
Author: Thomas Clayson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-01 05:21:51

Just subclass UITextField like this (Swift version):

import UIKit

class CustomTextField: UITextField {

    override func textRectForBounds(bounds: CGRect) -> CGRect {
       return CGRectInset(bounds, 25.0, 0)
    }

    override func editingRectForBounds(bounds: CGRect) -> CGRect {
       return self.textRectForBounds(bounds)
    }

}

To dodaje poziome wyściełanie 25.0 punkty po obu stronach.

 8
Author: King-Wizard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-22 00:00:16

Opierałem się na rozwiązaniu Nate' a, ale okazało się, że powoduje to problemy podczas korzystania z właściwości leftView/rightView, więc lepiej dostroić implementację super, ponieważ weźmie pod uwagę widok lewo/prawo.

- (CGRect)textRectForBounds:(CGRect)bounds {
    CGRect ret = [super textRectForBounds:bounds];
    ret.origin.x = ret.origin.x + 5;
    ret.size.width = ret.size.width - 10;
    return ret;
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [self textRectForBounds:bounds];
}
 7
Author: Stoto,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-16 11:47:51

Oto jak to osiągnąć w SWIFT

@IBOutlet weak var yourTextField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()
let paddingView = UIView(frame: CGRectMake(0, 0, 10, self.yourTextField.frame.height))
yourTextField.leftView = paddingView
yourTextField.leftViewMode = UITextFieldViewMode.Always
}
}

Resource

 5
Author: DrPatience,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-06 08:55:15

Wersja Swift 2.0:

let paddingView: UIView = UIView(frame: CGRectMake(0, 0, 5, 20))
textField.leftView = paddingView
textField.leftViewMode = UITextFieldViewMode.Always;
 5
Author: Phil,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-15 09:01:31

^ te sugestie są świetne dla tych, którzy programowo tworzą interfejs.

Ale są dwa leniwe proste sposoby dla tych z nas, którzy używają Xcode interface builder:

  • Łatwiej: umieść Interfejs Użytkownika za polem tekstowym

  • Najprostszy: Zmień styl obramowania na prosty czarny kwadrat( drugi od lewej opcji), a następnie dodaj swój obraz jako obraz tła. obraz ma pierwszeństwo przed kwadratem, więc nadal masz wypełnienie potrzebne do normalnego tła obrazu, bez rysowania kwadratu.

EDIT: możesz również użyć czarnej sfery (trzecia od lewej opcja przy wyborze UITextBox w IB), nie działa ona z prawym," graphical sfera " styl.

 4
Author: woody121,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-25 22:49:53

Najlepszym sposobem na to jest utworzenie klasy przy użyciu podklasy UITextField i in.m file

 #import "CustomTextField.h"
 #import <QuartzCore/QuartzCore.h>
 @implementation CustomTextField


- (id)initWithCoder:(NSCoder*)coder 
 {
  self = [super initWithCoder:coder];

  if (self) {

//self.clipsToBounds = YES;
//[self setRightViewMode:UITextFieldViewModeUnlessEditing];

self.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,15,46)];
self.leftViewMode=UITextFieldViewModeAlways;
   }

  return self;

 }

W ten sposób przejdź do swojego storyboardu lub xib i kliknij Inspektora tożsamości i zastąp UITextfield swoim własnym "CustomTextField" w opcji class.

Uwaga: Jeśli po prostu użyjesz wypełnienia z automatycznym układem dla textfield, Twoja aplikacja nie będzie działać i będzie wyświetlać tylko pusty ekran.

 3
Author: Anuj Kumar Rai,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-19 08:29:58

Zaktualizowana wersja dla Swift 3:

@IBDesignable
class FormTextField: UITextField {

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

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

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return textRect(forBounds: bounds)
    }
}
 3
Author: Beninho85,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-19 13:24:15

Swift 3 Solution

class CustomTextField: UITextField {

 override func textRect(forBounds bounds: CGRect) -> CGRect {
  return CGRect(x: bounds.origin.x + 10, y: bounds.origin.y + 8, width: bounds.size.width - 20, height: bounds.size.height - 16)
 }

 override func editingRect(forBounds bounds: CGRect) -> CGRect {
  return self.textRect(forBounds: bounds)
 }
}
 2
Author: Ben Sullivan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-23 11:14:46

Ustaw padding dla UITextField za pomocą UITextBorderStyleNone: Swift

Na podstawie najczęściej głosowanej odpowiedzi @ Evil Trout stworzyłem własną metodę w klasie ViewController, jak pokazano poniżej:

- (void) modifyTextField:(UITextField *)textField
{
    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
    textField.leftView = paddingView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.rightView = paddingView;
    textField.rightViewMode = UITextFieldViewModeAlways;

    [textField setBackgroundColor:[UIColor whiteColor]];
    [textField setTextColor:[UIColor blackColor]];
}

Teraz mogę wywołać tę metodę wewnątrz (metoda viewDidLoad) i wysłać dowolne moje pola tekstowe do tej metody i dodać padding dla prawej i lewej strony, i nadać tekst i kolory tła, pisząc tylko jedną linijkę kodu, w następujący sposób:

[self modifyTextField:self.firstNameTxtFld];

To działało idealnie na iOS 7! Wiem o tym. dodanie zbyt dużej ilości widoków może sprawić, że ta klasa będzie nieco cięższa do załadowania. Ale kiedy zaniepokojony trudnościami w innych rozwiązaniach, znalazłem się bardziej stronniczy w tej metodzie i bardziej elastyczny w użyciu w ten sposób. ;)

Dzięki za Hack "zły pstrąg"! (bow)

Pomyślałem, że powinienem zaktualizować fragment kodu tej odpowiedzi za pomocą Swift:

Ponieważ Swift pozwala nam pisać rozszerzenia dla istniejących klas, napiszmy to w ten sposób.

extension UITextField {
    func addPaddingToTextField() {
        let paddingView: UIView = UIView.init(frame: CGRectMake(0, 0, 8, 20))
        self.leftView = paddingView;
        self.leftViewMode = .Always;
        self.rightView = paddingView;
        self.rightViewMode = .Always;


        self.backgroundColor = UIColor.whiteColor()
        self.textColor = UIColor.blackColor()
    }
}

Użycie:

self.firstNameTxtFld.addPaddingToTextField()

Nadzieja to by pomogło komuś innemu!
Zdrowie!

 2
Author: Randika Vishman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-07 19:41:23

@Evil trout ' s answer is great. Takie podejście stosuję już od dłuższego czasu. Jedyne, czego mu brakuje, to "radzenie sobie z licznymi polami tekstowymi". Próbowałem innych podejść, ale nie wydaje się działać.

Podklasowanie UITextField tylko po to, aby dodać padding nie miało dla mnie żadnego sensu. Tak więc, iterowałem na wszystkich UITextFields, aby dodać padding.
-(void) addPaddingToAllTextFields:(UIView*)view {

    for(id currentView in [view subviews]){
        if([currentView isKindOfClass:[UITextField class]]) {
            // Change value of CGRectMake to fit ur need
            [currentView setLeftView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)]];
            [currentView setLeftViewMode:UITextFieldViewModeAlways];
        }

        if([currentView respondsToSelector:@selector(subviews)]){
            [textfieldarray addObjectsFromArray:[self addPaddingToAllTextFields:currentView]];
        }
    }
}
 1
Author: Kishor Kundan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-25 06:43:47

Rozwiązanie Brody ' ego zadziałało dla mnie idealnie. Musiałem dodać widoki boczne na polu tekstowym i dodać dodatkowe wypełnienie. Tak więc implementując niestandardową właściwość UIEdgeInsets do podklasy UITextField udało mi się osiągnąć to zadanie. Zamierzam użyć tej nowej podklasy we wszystkich moich projektach.

 1
Author: Lubakis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-03 13:17:59
textField.layer.borderWidth = 3;

Doda obramowanie, które działało jak padding dla mnie.

 1
Author: Saqib Saud,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-31 07:49:52

Poniżej znajduje się kod Swift, aby dać padding w UITextfield

 func txtPaddingVw(txt:UITextField) {
     let paddingView = UIView(frame: CGRectMake(0, 0, 10, 10))
     txt.leftViewMode = .Always
     txt.leftView = paddingView
 }

I wywołanie za pomocą

self.txtPaddingVw(txtPin)
 1
Author: Hardik Thakkar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-13 09:05:05

Jeśli ktoś szuka Swift 4.0 Wersji to poniżej {[2] } jest praca. Posiada zarówno Left jak i Right wyściółkę dla UITextField. W rzeczywistości jest to IBInspectable dla konfiguracji storyboardu. Możesz ustawić wartość bezpośrednio z Kreatora interfejsu / storyboardu. Jest to testowany kod w wersji Swift 4.0 i Xcode 9.0

Pamiętaj, że jeśli chcesz włączyć Clear Button na tym samym UITextField, musisz zachować puste wypełnienie.

import UIKit

extension UITextField {

    @IBInspectable var paddingLeft: CGFloat {
        get {
            return leftView!.frame.size.width
        }
        set {
            let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height))
            leftView = paddingView
            leftViewMode = .always
        }
    }

    @IBInspectable var paddingRight: CGFloat {
        get {
            return rightView!.frame.size.width
        }
        set {
            let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height))
            rightView = paddingView
            rightViewMode = .always     
        }
    }
}  
 1
Author: iOS.Wolf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-21 12:55:14

Inną kwestią jest to, że jeśli masz więcej niż jeden UITextField, w którym dodajesz wypełnienia, jest utworzenie oddzielnego UIView dla każdego pola tekstowego-ponieważ nie mogą być współdzielone.

 0
Author: user1686700,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-01 05:22:40

Dlaczego nie przypisać ciąg !?! jest to jedna z zalet IOS 6.0:)

NSMutableParagraphStyle *mps = [[NSMutableParagraphStyle alloc] init];
            mps.firstLineHeadIndent = 5.0f;
UIColor *placeColor = self.item.bgColor;

textFieldInstance.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"My Place Holder" attributes:@{NSForegroundColorAttributeName: placeColor, NSFontAttributeName : [UIFont systemFontOfSize:7.0f], NSParagraphStyleAttributeName : mps}];
 0
Author: Towhidul Islam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-15 04:41:58

Odpowiedź Nate ' a Flinka jest moją ulubioną, ale nie zapomnij o prawych / lewych poglądach. Np. UITextField podklasa:

override func rightViewRectForBounds(bounds: CGRect) -> CGRect {
    let rightViewBounds = super.rightViewRectForBounds(bounds)

    return CGRectMake(CGRectGetMinX(rightViewBounds) - 10, CGRectGetMinY(rightViewBounds), CGRectGetWidth(rightViewBounds), CGRectGetHeight(rightViewBounds))
}

Nad kodem Ustaw prawy padding dla rightView z UITextField.

 0
Author: rafalkitta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:57

Możesz użyć kategorii. Ustaw padding w lewo i w prawo

UITextField+Padding.h

@interface UITextField (Padding)
@property (nonatomic, assign) CGFloat paddingValue;
@property (nonatomic, assign) CGFloat leftPadding;
@property (nonatomic, assign) CGFloat rightPadding;

//overwrite
-(CGRect)textRectForBounds:(CGRect)bounds;
-(CGRect)editingRectForBounds:(CGRect)bounds;
@end

UITextField+Padding.m

#import "UITextField+Padding.h"
#import <objc/runtime.h>

static char TAG_LeftPaddingKey;
static char TAG_RightPaddingKey;
static char TAG_Left_RightPaddingKey;

@implementation UITextField (Padding)

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
-(CGRect)textRectForBounds:(CGRect)bounds {

CGFloat offset_Left=0;
CGFloat offset_Right=0;
if (self.paddingValue>0) {
    offset_Left=self.paddingValue;
    offset_Right=offset_Left;
}else{
    if (self.leftPadding>0){
        offset_Left=self.leftPadding;
    }
    if (self.rightPadding>0){
        offset_Right=self.rightPadding;
    }
}

if (offset_Left>0||offset_Right>0) {
    return CGRectMake(bounds.origin.x+ offset_Left ,bounds.origin.y ,
                      bounds.size.width- (offset_Left+offset_Right), bounds.size.height-2 );
 }else{
    return bounds;
 }
}



-(CGRect)editingRectForBounds:(CGRect)bounds {
    return [self textRectForBounds:bounds];
}
#pragma clang diagnostic pop


#pragma maek -setter&&getter
- (CGFloat)paddingValue
{
    return [objc_getAssociatedObject(self,&TAG_Left_RightPaddingKey) floatValue];
}
-(void)setPaddingValue:(CGFloat)paddingValue
{
    objc_setAssociatedObject(self, &TAG_Left_RightPaddingKey, @(paddingValue), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(CGFloat)leftPadding
{
    return [objc_getAssociatedObject(self,&TAG_LeftPaddingKey) floatValue];
}

-(void)setLeftPadding:(CGFloat)leftPadding
{
    objc_setAssociatedObject(self, &TAG_LeftPaddingKey, @(leftPadding), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(CGFloat)rightPadding
{
    return [objc_getAssociatedObject(self,&TAG_RightPaddingKey) floatValue];
}

-(void)setRightPadding:(CGFloat)rightPadding
{
    objc_setAssociatedObject(self, &TAG_RightPaddingKey, @(rightPadding), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

Możesz ustawić padding w ten sposób siebie.phoneNumTF.paddingValue=10.f; lub siebie.phoneNumTF.leftPadding = 10.f;

 0
Author: rikiluo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-17 03:56:22

Wersja Swift 3:

class CustomTextField:UITextField{

    required init?(coder aDecoder: NSCoder){
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect.init(x: bounds.origin.x + 8, y: bounds.origin.y, width: bounds.width, height: bounds.height)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return self.textRect(forBounds:bounds)
    }
}
 0
Author: 王怡飞,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-29 06:00:05