Equivalent of Obj-C alloc statement in swift - objective-c

I want to use This library in swift. It's written in Obj-C.
I've added header and Bridging and etc, now, I don't know what is equivalent of this statement :
//Color did change block declaration
NKOColorPickerDidChangeColorBlock colorDidChangeBlock = ^(UIColor *color){
//Your code handling a color change in the picker view.
};
NKOColorPickerView *colorPickerView = [[NKOColorPickerView alloc] initWithFrame:CGRectMake(0, 0, 300, 340) color:[UIColor blueColor] andDidChangeColorBlock:colorDidChangeBlock];
//Add color picker to your view
[self.view addSubview:colorPickerView];

let colorPickerView = NKOColorPickerView(frame: CGRect(x: 0, y: 0, width: 300, height: 340), color: UIColor.blueColor()) { (color) -> Void in
// Your code handling a color change in the picker view.
}
view.addSubview(colorPickerView)
or
var colorDidChangeBlock: NKOColorPickerDidChangeColorBlock = { (color) in
// Your code handling a color change in the picker view.
}
let colorPickerView = NKOColorPickerView(frame: CGRect(x: 0, y: 0, width: 300, height: 340), color: UIColor.blueColor(), andDidChangeColorBlock: colorDidChangeBlock)
view.addSubview(colorPickerView)
I prefer the first way. Its more clear and easier to read.

Related

Adding target to UIButton without #objc reference

When we make a UIButton programmatically and add a target for its click action.
And for the selector we need a function with "#objc" reference, even if we are working in a purely swift project.
override func viewDidLoad() {
let requiredButton = UIButton(frame: CGRect(x: 100, y: 100, width: 50, height: 30))
self.view.addSubview(requiredButton)
requiredButton.addTarget(self, action: #selector(self.buttonTapAction(sender:)), for: UIControl.Event.touchUpInside)
}
#objc func buttonTapAction(sender:UIButton) {
// button action implementation here
}
Is there a way to do this without the "#objC" reference???
If you target iOS 14+ there is new API that you can use without "#objc" references:
override func viewDidLoad() {
let requiredButton = UIButton(frame: CGRect(x: 100, y: 100, width: 50, height: 30), primaryAction: .init(handler: { _ in
// button action implementation here
}))
self.view.addSubview(requiredButton)
}
If you target iOS 13+ you can build UI using SwiftUI with Button controls. SwiftUI is currently internally based on UIKit, but you can write whole apps in swift using SwiftUI without a single "#objc" reference
UIButton is a foundation object from NSObject subclass so it executes on the Objective-c runtime, so you need to tell Swift to expose the code to objective-c
Reference: https://craiggrummitt.com/2016/09/16/any-vs-anyobject-vs-nsobject-in-swift-3/

JSPdf autotable header border

How to create border in Header? I am using jspdf autotable to create table but cannot find any idea to apply border to header. Is there any hook that can be used to create header border?
You can use the header styles:
doc.autotable(columns, data, {
headerStyles: {
lineWidth: 1,
lineColor: [255, 0, 0]
}
});
As of today (version 3.5.25) the property name is headStyles and still different border widths is not supported.
Example:
autoTable(doc, {
headStyles: {
lineWidth: 0.5, // 1 is too thick for me
lineColor: [255, 0, 0] // Or gray level single value from 0-255
}
})
I use the imported method version (autoTable) in TypeScript.

Remove kendo Area chart padding

There is some default padding in areachart.
I tried my best to force it make 0 padding, but no luck.
Any idea how areachart touches the edges of the box
( base css framework is bootstrap3 ).
after using ( as #suraj suggestion in comments )
Try specifying the padding inside the chart initialization. Something like this:
$("#chart").kendoChart({
//....
chartArea: {
margin: 0,
padding:0
},
plotArea: { margin: 0, padding:0 }
});

titanium: Adding things to a scrollview

So i have this code:
var answerView = Ti.UI.createScrollView({ //var added
top: Ti.Platform.displayCaps.platformHeight*0.55,
left:Ti.Platform.displayCaps.platformWidth*0.1,
width: Ti.Platform.displayCaps.platformWidth*0.8,
backgroundImage: '/images/labelBackground.png',
borderRadius: 8,
height: Ti.Platform.displayCaps.platformHeight*0.5,
contentHeight:'auto',
showHorizontalScrollIndicator:true,
scrollType:'vertical',
});
for (var j = 0; j < question.answers.length; j++){
var row = createRow(question.answers[j]);
answerView.add(row);
}
and this function:
function createRow(answer) {
var row = Ti.UI.createView({
width:'100%',
height: 'auto',
});
var answerButton = Ti.UI.createButton({
top: '1%',
left: '1%',
title: answer.answer,
value: answer.order,
width:'98%',
font : {fontSize:'12sp'},
});
row.add(answerButton);
return row;
}
The problem is, the darn thing overlays all the buttons into one... that is, it isn't "pushing down" the rows. From the titanium tutorial here:
http://docs.appcelerator.com/titanium/2.1/index.html#!/api/Titanium.UI.ScrollView
I would have thought this would work, but it doesn't. I know i can do some magic with the numbers and send each row the position it should have, but I thought maybe titanium would be clever enough to do that? Am i missing something?
Oh jesus.
Titanium is moronic in this instance - the problem was I had
height: 'auto' in the definition of each row - that is:
function createRow(answer) {
var row = Ti.UI.createView({
width:'100%',
height: 'auto',
});
...
And funnily enough, that makes each row REALLY BIG, probably as big as the entire space alloted for the row. I don't know, i never tried to scroll through it. So just change the height value for the row to something sane - i always base mine off the display height.
Now I have
function createRow(answer) {
var row = Ti.UI.createView({
width:'100%',
height: Ti.Platform.displayCaps.platformHeight*0.1,
});
...
and all is well.

Dojo - How to to position object relative to another object

Just like the title says. Because buildin widgets do not really fit, what I want to do, I need to make my own tooltipdialog implementation:
To start simple:
dojo.query(".small-avatar").connect("onmouseenter", function () {
var pos = dojo.position(this, true);
dojo.query("#user-tooltip").style({ left: pos.x, top: pos.y, visibility:"visible" });
});
I've come with this. Well I guess the problem is with pos. I've tried to digg with documentation, but honestly there is no word, on how access x and y position so I assumed it's with ".".
UPDATE:
After more checking, I figured out that problem lie in position it self, or style.
For some reason Dojo do not add coordinates to targeted node "#user-tooltip". It just change visibility.
You have the pos.x and pos.y correctly referenced since dojo.position() returns an object literal. From the Dojo docs, The return object looks like:
{ w: 300: h: 150, x: 700, y: 900, }
You may need to set position: absolute or position: relative on #user-tooltip.
I finally got it working:
dojo.query(".small-avatar").connect("onmouseenter", function (e) {
var pos = dojo.position(this, true);
dojo.style(dojo.byId('user-tooltip'), { visibility: "visible", "left": pos.x+pos.w+'px', "top": pos.y+pos.h+'px' });
});