Отступ текста UITextField и заполнитель - PullRequest
0 голосов
/ 28 апреля 2019

Я программно создал UITextField в нижнем колонтитуле UITableView следующим образом:

let customView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 50))

self.textArea = UITextField(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width - 50, height: 50))
self.textArea.placeholder = "Add Item"
self.textArea.layer.borderColor = UIColor.gray.cgColor
self.textArea.layer.borderWidth = 1
customView.addSubview(self.textArea)

let button = UIButton(frame: CGRect(x: self.view.bounds.width - 50, y: 0, width: 50, height: 50))
button.setTitle("Add", for: .normal)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = UIColor(displayP3Red: 3.0 / 255.0, green: 0.0  / 255.0, blue: 113.0  / 255.0, alpha: 1.0)
button.addTarget(self, action: #selector(self.addWishListItem), for: .touchUpInside)
customView.addSubview(button)

self.tableView.tableFooterView = customView

Мой вопрос заключается в том, как сделать отступ для текста UITextField и текста-заполнителя, чтобы он не находился у края левой стороны?

Я нашел это:

let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: self.textArea.frame.height))
            self.textArea.leftView = paddingView
            self.textArea.leftViewMode = .always

Есть ли лучший способ сделать это?

1 Ответ

0 голосов
/ 28 апреля 2019

Вы можете создать customTextField и переопределить поля для текста, заполнителя, границы ... в основном для всего.

import UIKit
class CustomTextField: UITextField {

    override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect.init(x: 10, y: -10, width: bounds.width, height: bounds.height)
    }

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

Вы можете контролировать его положение, изменяя значения x и y

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...