Skip to content

Commit fa80dfc

Browse files
author
Dhaval Thanki
committed
- Code optimized
1 parent f60823d commit fa80dfc

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

DTTextField/Classes/DTTextField.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,11 @@ public class DTTextField: UITextField {
8585
return paddingX
8686
}
8787

88+
fileprivate var fontHeight:CGFloat{
89+
return ceil(font!.lineHeight)
90+
}
91+
8892
fileprivate var dtLayerHeight:CGFloat{
89-
9093
return showErrorLabel ? floor(bounds.height - lblError.bounds.size.height - paddingYErrorLabel) : bounds.height
9194
}
9295

@@ -271,10 +274,10 @@ public class DTTextField: UITextField {
271274

272275
guard showErrorLabel else { return CGRect(x: x, y: 0, width: rect.width - paddingX, height: rect.height) }
273276

274-
let topInset = (rect.size.height - lblError.bounds.size.height - paddingYErrorLabel - ceil(font!.lineHeight)) / 2.0
275-
let textY = ((rect.height - ceil(font!.lineHeight)) / 2.0) - topInset
277+
let topInset = (rect.size.height - lblError.bounds.size.height - paddingYErrorLabel - fontHeight) / 2.0
278+
let textY = topInset - ((rect.height - fontHeight) / 2.0)
276279

277-
return CGRect(x: x, y: -ceil(textY), width: rect.size.width - paddingX, height: rect.size.height)
280+
return CGRect(x: x, y: floor(textY), width: rect.size.width - paddingX, height: rect.size.height)
278281
}
279282

280283
fileprivate func insetRectForBounds(rect:CGRect) -> CGRect {
@@ -289,9 +292,12 @@ public class DTTextField: UITextField {
289292
return insetRectForEmptyBounds(rect: rect)
290293
}else{
291294
let topInset = paddingYFloatLabel + lblFloatPlaceholder.bounds.size.height + (paddingHeight / 2.0)
292-
let textOriginalY = (rect.height - font!.lineHeight) / 2.0
293-
let textY = textOriginalY - topInset
294-
return CGRect(x: x, y: -ceil(textY), width: rect.size.width - paddingX, height: rect.size.height)
295+
let textOriginalY = (rect.height - fontHeight) / 2.0
296+
var textY = topInset - textOriginalY
297+
298+
if textY < 0 { textY = topInset }
299+
300+
return CGRect(x: x, y: ceil(textY), width: rect.size.width - paddingX, height: rect.height)
295301
}
296302
}
297303
}
@@ -354,10 +360,13 @@ public class DTTextField: UITextField {
354360
override public func layoutSubviews() {
355361
super.layoutSubviews()
356362

363+
CATransaction.begin()
364+
CATransaction.setDisableActions(true)
357365
dtLayer.frame = CGRect(x: bounds.origin.x,
358366
y: bounds.origin.y,
359367
width: bounds.width,
360368
height: dtLayerHeight)
369+
CATransaction.commit()
361370

362371
if showErrorLabel {
363372

0 commit comments

Comments
 (0)