Подтвердить что ты не робот

Как изменить цвет и шрифт заполнителя UITextfield с помощью swift 2.0?

Как изменить UITextfield placeholder и fontsize в SWIFT 2.0?

4b9b3361

Ответ 1

# 1. установить цвет текстового поля заполнителя программно

    var myMutableStringTitle = NSMutableAttributedString()
    let Name  = "Enter Title" // PlaceHolderText

    myMutableStringTitle = NSMutableAttributedString(string:Name, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!]) // Font
    myMutableStringTitle.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range:NSRange(location:0,length:Name.characters.count))    // Color
    txtTitle.attributedPlaceholder = myMutableStringTitle

ИЛИ ЖЕ

txtTitle.attributedPlaceholder = NSAttributedString(string:"Enter Title", attributes: [NSForegroundColorAttributeName: yellowColor])

Примечание. Name - это ваш местозаполнитель textField.

PlaceHolder TextFiled:

enter image description here

-------------------------------- ИЛИ ЖЕ ----------------- --------------------

# 2. установить цвет текстового поля заполнителя в атрибуте времени выполнения

  1. Установить текстовое поле placeHolder text Enter Title

    enter image description here

  2. Нажмите на личность инспектора свойства текстового поля.

    enter image description here

  3. Пользователь определяет атрибуты времени выполнения, добавляет атрибуты цвета

    Ключевой путь: _placeholderLabel.textColor

    Тип: Color

    значение: Your Color or RGB value

    enter image description here

    PlaceHolder TextFiled:

    enter image description here

Ответ 2

Обновлено для Swift 3

Если вы хотите изменить цвет закладки UITextField для Swift 3, используйте следующие строки кода:

let yourTextFieldName = UITextField(frame: CGRect(x: 0, y: 0, width: 180, height: 21))
yourTextFieldName.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSForegroundColorAttributeName: UIColor.white])

Ответ 3

Для быстрой 4 вместо

NSForegroundColorAttributeName

использование

NSAttributedStringKey.foregroundColor

Ответ 4

Вы можете попробовать с этим примером кода

let  textFld = UITextField();
textFld.frame = CGRectMake(0,0, 200, 30)
textFld.center = self.view.center;
textFld.attributedPlaceholder = NSAttributedString(string:"Test Data for place holder", attributes:[NSForegroundColorAttributeName: UIColor.blueColor(),NSFontAttributeName :UIFont(name: "Arial", size: 10)!])
self.view.addSubview(textFld)

Ответ 5

Для swift 4.2 используйте NSAttributedString.Key.foregroundColor вместо NSForegroundColorAttributeName

Итак, сделайте это так

textField.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])

Ответ 6

Заполнитель для текстового поля Цель C

NSString* str = @"Placeholder text...";                                    
NSRange range1 = [str rangeOfString:@"Placeholder text..."];


NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:str];
[attributedText setAttributes:@{
                                NSFontAttributeName:[UIFont fontWithName:customFont_NotoSans_Regular size:13.0]
                                }
                        range:range1];

[attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range1];

txtFld.font = [UIFont fontWithName:customFont_NotoSans_Regular size:13.0];
txtFld.keyboardType = UIKeyboardTypeDefault;
txtFld.attributedPlaceholder = attributedText;

Ответ 7

Это легко сделать с подклассом UITextField.

Добавьте свойство placeholderColor чтобы легко установить цвет, а затем .placeholder за изменением .placeholder чтобы применить к нему цвет (с использованием свойства .attributedPlaceholder).

var placeholderColor: UIColor = .lightGray

override var placeholder: String? {
    didSet {
        let attributes = [ NSAttributedString.Key.foregroundColor: placeholderColor ]
        attributedPlaceholder = NSAttributedString(string: placeholder ?? "", attributes: attributes)
    }
}

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

Ответ 8

установить заполнитель текстового поля

let leftBarButtonItem = UIBarButtonItem.init(image: UIImage(named:"ic_nav-bar_back.png"), landscapeImagePhone: nil, style: .plain, target: viewController, action: #selector(viewController.buttonClick(_:)))
        leftBarButtonItem.imageInsets = UIEdgeInsets(top: 0, left: -15, bottom: 0, right: 0)
        leftBarButtonItem.tintColor = UIColor(hex: 0xED6E19)
        viewController.navigationItem.setLeftBarButton(leftBarButtonItem, animated: true)