码迷,mamicode.com
首页 > 其他好文 > 详细

自定义搜索框-输入框光标不贴边

时间:2016-05-09 17:04:43      阅读:788      评论:0      收藏:0      [点我收藏+]

标签:

//

//  RBCustomTextField.swift

//

//  Created by Touch on 16/4/27.

//  Copyright © 2016年 renrenbx. All rights reserved.

//

 

import Foundation

 

class RBCustomTextField: UITextField {

    

    override init(frame: CGRect) {

        super.init(frame: frame)

    }

    

    // 编辑状态内容显示位置

    override func editingRectForBounds(bounds: CGRect) -> CGRect {

        super.editingRectForBounds(bounds)

        var newBounds = bounds

        if self.leftView?.frame.size.width > 0{

            newBounds.origin.x = bounds.origin.x + (self.leftView?.frame.size.width)!

        }else{

            newBounds.origin.x = bounds.origin.x + 8

        }

        newBounds.origin.y = bounds.origin.y + 1

        return newBounds

    }

    

    // 解决输入问题位置偏移问题

    override func drawTextInRect(rect: CGRect) {

 

        super.drawTextInRect(CGRect(x: 8, y: self.frame.origin.y + 5.5, width: 0, height: 0))

    }

    

    // 占位符偏移

    override func placeholderRectForBounds(bounds: CGRect) -> CGRect {

        super.placeholderRectForBounds(bounds)

        echo("self.frame",self.frame)

        echo("bounds",bounds)

        var newBounds = bounds

        newBounds.origin.x = bounds.origin.x + 8

//        newBounds.origin.y = 3

        echo("newBounds",newBounds)

        return newBounds

    }

    

    // 解决占位符位置靠上问题

    override func drawPlaceholderInRect(rect: CGRect) {

        super.drawPlaceholderInRect(CGRect(x: 0, y: self.frame.size.height * 0.5 - 1, width: 0, height: 0))

    }

    

    // 左视图 位置

    override func leftViewRectForBounds(bounds: CGRect) -> CGRect {

        super.leftViewRectForBounds(bounds)

        

        let newBounds = CGRect(x: 0, y: 0, width: self.frame.size.height, height: self.frame.size.height)

        return newBounds

    }

    

    // 右视图 位置

    override func rightViewRectForBounds(bounds: CGRect) -> CGRect {

        super.rightViewRectForBounds(bounds)

        

        let newBounds = CGRect(x: self.frame.size.width - self.frame.size.height, y: 0, width: self.frame.size.height, height: self.frame.size.height)

        return newBounds

    }

    

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

}

自定义搜索框-输入框光标不贴边

标签:

原文地址:http://www.cnblogs.com/madarax/p/5474565.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!