Hi, I'd like to write this Swift code in Kotlin/Na...
# kotlin-native
l
Hi, I'd like to write this Swift code in Kotlin/Native:
Copy code
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 21))
        label.center = CGPoint(x: 160, y: 285)
        label.textAlignment = .center
        label.font = label.font.withSize(25)
        label.text = CommonKt.createApplicationScreenMessage()
        view.addSubview(label)
Per the Obj-C interop doc, I should be able to write
val label = UILabel.create(frame: CGRect.create(x = 0, y = 0, width = 300, height = 21))
for the first line, but there's no
create(…)
function in the autocomplete when using IntelliJ CE 2018.3 EAP with Kotlin 1.3.0-rc-146… Can anyone give me a hint?
r
Try building from command-line anyway. Sometimes the IDE lies to you. Alternatively, RC4 just release (1.3.0-rc-190) so you could try it there
a
I'd recommend looking at the objective-c equivalent and translating from that. Kotlin code can invoke objective-c libraries, not swift libraries. For e.g. the rect creation can use CGRectMake(left, top, width, height)
o
Copy code
val label = UILabel(CGRectMake(x = 0.0, y = 0.0, width = 300.0, height = 21.0)).apply {
        center = CGPointMake(x = 160.0, y = 285.0)
        textAlignment = NSTextAlignmentCenter
        font = font().fontWithSize(25.0)
        text = "Hello"
    }
❤️ 1
not sure if code ^^^ is much different from Swift one
l
Thank you! The code is not much different, but the fact that I write it in Kotlin means that I can expect it from common sources. Do you know why it has a
Make
suffix? Is this a Kotlin/Native or Obj-C convention? Also, I wonder why the initializers overloads (that you translate to the non existing concept of extension constructors) are not exposed as
operator fun invoke(…)
on the generated
companion object
?
171 Views