我的代码:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateFormatter.dateFromString("2015-09-01 00-32-40")
Run Code Online (Sandbox Code Playgroud)
结果:2015-08-31 17:32:40
但我希望得到这样的结果:17:32.我该如何解决?
我试图同时创建两个对象,但由于某种原因,我的第二个对象的字段是空白的.
模型是:
Users: id, name, last, email, password ...
Locations: address, longitude, latitude.
Run Code Online (Sandbox Code Playgroud)
关系如此:
User has_many location
Location belongs_to users
Run Code Online (Sandbox Code Playgroud)
事件控制器创建方法:
def create
@event = current_users.events.build(params[:event])
@location = @event.locations.build(params[:location])
@location.longitude = params[:longitude]
@location.latitude = params[:latitude]
respond_to do |format|
if @location.save
@location.longitude = params[:longitude]
@location.latitude = params[:latitude]
if @location.save
if @event.save
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render json: @event, status: :created, location: @event }
else
format.html { render action: "new" }
format.json { render …Run Code Online (Sandbox Code Playgroud) 我正在画布上绘制一些文本,并且正在使用StaticLayout在屏幕上包装文本。我想对齐文本,以使文本的底部位于屏幕的底部。
为此,我需要知道StaticLayout将文本包装成多少行,因此我可以将其乘以字体大小,并将其与组件的高度偏移很多。
这是我的StaticLayout:
main = new TextPaint();
main.setTextSize(textSize);
main.setColor(Color.WHITE);
bottomText = new StaticLayout("Long text that requires wrapping.", main, getWidth(), Layout.Alignment.ALIGN_CENTER, 1f, 1.0f, false);
Run Code Online (Sandbox Code Playgroud)
我通过翻译画布来向下移动它:
canvas.translate(0, getHeight() / 2);
bottomText.draw(canvas);
canvas.restore();
Run Code Online (Sandbox Code Playgroud)
所以; 如何将其对齐至底部,或获取已拆分成的行数?
我在互联网上找到的一个函数中得到了这个,但我不明白它的用途。我在 W3school.com 上读到的内容。他们是这样解释的:
负无穷大可以解释为低于任何其他数字的东西。
所以这是我可以理解的,但我想不出你需要这种常数的时刻。另外,检查一下这个函数:
function setEqualHeight(selector, triggerContinusly) {
var elements = $(selector)
elements.css("height", "auto")
var max = Number.NEGATIVE_INFINITY;
$.each(elements, function(index, item) {
if ($(item).height() > max) {
max = $(item).height()
}
})
$(selector).css("height", max + "px")
if (!!triggerContinusly) {
$(document).on("input", selector, function() {
setEqualHeight(selector, false)
})
$(window).resize(function() {
setEqualHeight(selector, false)
})
}
}
Run Code Online (Sandbox Code Playgroud)
我了解整个功能。但我一开始不明白为什么 max 会是= Number.NEGATIVE_INFINITY这样。然后,他检查高度是否高于可能的最小数字?
这个函数工作完美,所以我猜它是正确的,但我真的不明白这个函数的用途是什么,或者为什么人们通常会使用它。
希望大家能够赐教!