[英](Rubymotion) How can I do this custom view in Formotion?
I am struggling at the moment to make a custom Form with Formotion(Also I'm very new in Rubymotion). 我目前正在努力用Formotion制作自定义表单(而且我在Rubymotion中还很陌生)。 Maybe I have to try another framework or maybe not doing any framework at all.... 也许我不得不尝试另一个框架,或者根本不做任何框架。
I try to make this: 我试图做到这一点:
At this moment I created this in Formotion: 此刻,我在Formotion中创建了此代码:
The problem is the Border between password and email. 问题是密码和电子邮件之间的边界。 It has to start under the text field and not under the icon. 它必须在文本字段下而不是图标下开始。 It is a small change but I don't know how to do this with Formotion. 这是一个很小的变化,但是我不知道如何使用Formotion来做到这一点。 Are those kind of things easy possible? 这些事情容易实现吗? Thanks in advanced! 提前致谢!
Here is my code: 这是我的代码:
class LoginScreen < PM::FormotionScreen
title ""
def table_data
{
sections: [
{
title: "Login",
rows: [
{
title: "Email",
key: :email,
type: :email,
placeholder: "me@mail.com",
auto_correction: :no,
auto_capitalization: :none,
text_alignment: UITextAlignmentLeft
}, {
title: "Password",
key: :password,
type: :password,
placeholder: "required",
secure: true,
text_alignment: UITextAlignmentLeft
}
]
}
]
}
end
end
I overwritten the EmailRow to insert the icon 我覆盖了EmailRow以插入图标
motion_require 'string_row'
module Formotion
module RowType
class EmailRow < StringRow
def build_cell(cell)
cell.selectionStyle = self.row.selection_style || UITableViewCellSelectionStyleBlue
field = UITextField.alloc.initWithFrame(CGRectZero)
field.tag = TEXT_FIELD_TAG
observe(self.row, "value") do |old_value, new_value|
break_with_semaphore do
update_text_field(new_value)
end
end
field.clearButtonMode = UITextFieldViewModeWhileEditing
field.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter
field.textAlignment = row.text_alignment || UITextAlignmentRight
field.keyboardType = keyboardType
field.secureTextEntry = true if row.secure?
field.returnKeyType = row.return_key || UIReturnKeyNext
field.autocapitalizationType = row.auto_capitalization if row.auto_capitalization
field.autocorrectionType = row.auto_correction if row.auto_correction
field.clearButtonMode = row.clear_button || UITextFieldViewModeWhileEditing
field.enabled = row.editable?
field.inputAccessoryView = input_accessory_view(row.input_accessory) if row.input_accessory
add_callbacks(field)
cell.swizzle(:layoutSubviews) do
def layoutSubviews
old_layoutSubviews
# viewWithTag is terrible, but I think it's ok to use here...
formotion_field = self.viewWithTag(TEXT_FIELD_TAG)
formotion_field.sizeToFit
field_frame = formotion_field.frame
field_frame.origin.x = self.textLabel.frame.origin.x + Formotion::RowType::Base.field_buffer * 2
field_frame.origin.y = ((self.frame.size.height - field_frame.size.height) / 2.0).round
field_frame.size.width = self.frame.size.width - field_frame.origin.x - Formotion::RowType::Base.field_buffer
formotion_field.frame = field_frame
end
end
if UIDevice.currentDevice.systemVersion >= "6.0"
field.swizzle(:setText) do
def setText(text)
r = old_setText(text)
self.sendActionsForControlEvents(UIControlEventEditingChanged)
r
end
end
end
field.font = BW::Font.new(row.font) if row.font
field.placeholder = row.placeholder
field.text = row_value
icon = UIImage.imageNamed("icons/mail.png")
icon_view = UIImageView.alloc.initWithImage(icon)
icon_view.setFrame(CGRectMake(20, 18, icon_view.frame.size.width, icon_view.frame.size.height))
cell.addSubview(icon_view)
cell.addSubview(field)
cell.textLabel.hidden = true
field
end
end
end
end
This was extremely easy in the end. 最后,这非常容易。 Just add an image in cell.imageView
in EmailRow. 只需在cell.imageView
中的cell.imageView中添加图像。
added this line: 添加了这一行:
cell.imageView.image = UIImage.imageNamed("icons/mail.png")
cell.imageView.setFrame(CGRectMake(20, 18, cell.imageView.frame.size.width, cell.imageView.frame.size.height))
and you access the cell with the code below, for example: 并使用以下代码访问单元格,例如:
cell = @form.table.visibleCell.first
I'd recommend disabling the built-in border entirely and adding a pseudo-border using a thin UIView
. 我建议完全禁用内置边框,并使用薄UIView
添加伪边框。 That way you can set the frame
to position it in the right position and give it a backgroundColor of #E3E3E5k
like in your mockup. 这样,您可以将frame
设置为将其定位在正确的位置,并像在模型中一样将其赋予backgroundColor #E3E3E5k
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.