是否可以在textfield中将图像或xib设置为值?

时间:2018-03-08 04:21:01

标签: ios swift uitextfield

如标题所述,我希望在我的文本字段中添加图像或xib作为值。这是我的设计:

Design

对于我可以使用占位符实现的3个黑点,但是第一个具有值的文本字段怎么样?我需要设置一个带有边框的圆形紫色,并且它是文本字段的值,这意味着用户可以删除它并编辑文本字段。

我尝试过这样的事情:

let imageView = UIImageView();
let image = UIImage(named: "dot-icon");
imageView.image = image;
imageView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
tfTest.addSubview(imageView)
let leftView = UIView.init(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
tfTest.leftView = leftView;
tfTest.leftViewMode = UITextFieldViewMode.always

这可以通过向文本字段添加图像来实现我想要的效果,但现在文本字段不可编辑。

任何人都有任何想法?

由于

1 个答案:

答案 0 :(得分:2)

拍4张imageView [pinImgView1,pinImgView2,pinImgView3,pinImgView4]

和一个TextField [pinTextField]。

删除方法:

def get_vacation_info(x):
    vacations = sorted(x["DATE"][x["VACATION"]=="True"].tolist())
    df_x = pd.DataFrame(vacations, columns=["DATE"]).apply(pd.to_datetime)
    df_x["year"] = df_x.DATE.dt.year
    df_x["week"] = df_x.DATE.dt.weekofyear
    df_x["quarter"] = df_x.DATE.dt.quarter

    def get_subtotals(y):
        total = len(y)
        perweek = y["week"].value_counts().reset_index()
        weekindex = pd.DataFrame([i for i in range(1, 53)], columns=["index"])
        merged_week = pd.merge(weekindex, perweek, on=["index"], how="left").fillna(0).astype(int)
        perweek = merged_week["week"].tolist()

        perquarter = y["quarter"].value_counts().reset_index()
        quarterindex = pd.DataFrame([i for i in range(1, 5)], columns=["index"])
        merged_quater = pd.merge(quarterindex, perquarter, on=["index"], how="left").fillna(0).astype(int)
        perquarter = merged_quater["quarter"].tolist()
        return {"total": total, "perweek": perweek, "perquarter": perquarter}

    subtotals = df_x.groupby("year").apply(lambda y: get_subtotals(y)).to_json()

    return {"vacations": vacations, "subtotals": subtotals}  

result = df.groupby("NAME").apply(lambda x: get_vacation_info(x))

然后添加此方法:

  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
    {
        let characterSet = CharacterSet(charactersIn: "0123456789")
        if (string.rangeOfCharacter(from: characterSet) == nil && string.characters.count > 0)
        {
            return false
        }

        let newString = (self.pinTextField.text! as NSString).replacingCharacters(in: range, with: string) as NSString
        self.showPinImagesAsPerString(stringPIN: newString as String)
        return true
    }