如果文本长于UITextField宽度,如何选取UITextField占位符?

时间:2017-05-12 10:11:28

标签: ios iphone swift uitextfield marquee

是否可以选取UITextFiled占位符,如果占位符文本比UITextField宽度大,则有UILabel MarqueeLabel的lib但我不知道如何选择占位符,请提供一些建议,或者你可以解释什么是占位符实际上,它看起来不像UILabel

我使用带有验证错误消息的CustomTextField的波纹管代码

import UIKit

@IBDesignable
class CustomTextField: UITextField {

    var placeholdertext: String?
    @IBInspectable
    public var cornerRadius :CGFloat {

        set { layer.cornerRadius = newValue }

        get {
            return layer.cornerRadius
        }

    }

    // Provides left padding for images
    override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
        var textRect = super.leftViewRect(forBounds: bounds)
        textRect.origin.x += leftPadding
        return textRect
    }

    override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
        var textRect = super.rightViewRect(forBounds: bounds)
        textRect.origin.x -= rightPadding
        return textRect
    }

    @IBInspectable var leftImage: UIImage? {
        didSet {
            updateView()
        }
    }

    @IBInspectable var isUnderLine:Bool = false
    {
        didSet{
            updateView()
        }
    }

    @IBInspectable var rightImage: UIImage? {
        didSet {
            updateView()
        }
    }

    func setError(error:String?){
        if(error != nil)
        {
            self.attributedPlaceholder = NSAttributedString(string: error!, attributes: [NSForegroundColorAttributeName: UIColor.red])
        }

    }

    @IBInspectable var leftPadding: CGFloat = 0
    @IBInspectable var rightPadding: CGFloat = 0
    @IBInspectable var textLeftPadding:CGFloat = 0

    @IBInspectable var color: UIColor = UIColor.lightGray {
        didSet {
            updateView()
        }
    }
    @IBInspectable var underlineColor:UIColor = UIColor.black
    {
        didSet{
            self.updateView()
        }
    }
    private var placeholderColorValue:UIColor = UIColor.lightGray
    @IBInspectable public var placeholderColor:UIColor
    {
        set{
            self.attributedPlaceholder = NSAttributedString(string:placeholder!, attributes: [NSForegroundColorAttributeName: newValue])
            placeholderColorValue = newValue
        }
        get{
            return placeholderColorValue
        }
    }

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

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

    override func awakeFromNib() {
        super.awakeFromNib()
        self.setUpView()
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
    }

    func setUpView() {
        if(rightImage != nil)
        {
            self.leftViewMode = UITextFieldViewMode.always
            let rightImageView:UIImageView = UIImageView(image: rightImage)
            rightImageView.frame = CGRect(x: 0, y: 0, width: self.frame.size.height, height: self.frame.size.height)
            self.rightView = rightImageView
        }
        else {
            rightViewMode = UITextFieldViewMode.never
            rightView = nil
        }
    }

    func updateView() {
        if let imageLeft = leftImage {
            leftViewMode = UITextFieldViewMode.always
            let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
            imageView.image = imageLeft
            // Note: In order for your image to use the tint color, you have to select the image in the Assets.xcassets and change the "Render As" property to "Template Image".
            imageView.tintColor = color
            leftView = imageView
        } else {
            leftViewMode = UITextFieldViewMode.never
            leftView = nil
        }

        if let imageRight = rightImage {
            rightViewMode = UITextFieldViewMode.always
            let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
            imageView.image = imageRight
            // Note: In order for your image to use the tint color, you have to select the image in the Assets.xcassets and change the "Render As" property to "Template Image".
            imageView.tintColor = color
            rightView = imageView
        } else {
            rightViewMode = UITextFieldViewMode.never
            rightView = nil
        }

        // Placeholder text color
        attributedPlaceholder = NSAttributedString(string: placeholder != nil ?  placeholder! : "", attributes:[NSForegroundColorAttributeName: color])
        if(self.isUnderLine)
        {
            let underline:UIView = UIView()
            underline.frame = CGRect(x: 0, y: self.frame.size.height-1, width: self.frame.size.width, height: 1)
            underline.backgroundColor = underlineColor
            self.addSubview(underline)
        }
    }

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

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

}

并使用验证

          if (txtField.text != "condtion" ){
                txtField.setError(error:"Error message for text field");
                valid = false;
                txtField.becomeFirstResponder()
            }

0 个答案:

没有答案