lvnyk / BezierString

Rendering NSAttributedStrings along arbitrary UIBezierPaths
MIT License
196 stars 16 forks source link

Can't access BezierString class from Obj-C #1

Closed rmvz3 closed 9 years ago

rmvz3 commented 9 years ago

Hi. I can access UIBezierLabel from my Obj-C based app without problem but I can't reach BezierString class.

If I change class declaration as: @objc class BezierString I'm able to use the classes and methods like this:

BezierString *bstring = [BezierString initWithBezierPath:bezierPath];
[bstring drawAttributedString:string toContext:UIGraphicsGetCurrentContext() align:NSTextAlignmentCenter yOffset:10.0f];

but I get an "Unrecognized selector +[MyClass.BezierString initWithBezierPath:]" error.

I know this strictly is not a class issue but I wonder if you could help.

Thanks

lvnyk commented 9 years ago

Hi, BezierString does not extend NSObject, which means it is not initialized as one. initWith: is an object function, and as such doesn't work on unallocated objects.

You can make the BezierString an extension of NSObject

class BezierString : NSObject {

and init it via

[[BezierString alloc] initWithBezierPath:bezierPath];

Or, you can keep it a pure Swift object and add a class level initializer:

@objc class BezierString {
    class func bezierStringWithBezierPath(bezierPath: UIBezierPath) -> BezierString {
        return BezierString(bezierPath: bezierPath)
    }

and init it like this:

[BezierString bezierStringWithBezierPath:bezierPath];

The UIBezierLabel already extends NSObject (because UILabel does), so that works without any modifications.

best regards, luka

rmvz3 commented 9 years ago

Hi Ivnyk.

I should have known that one. :) My swift skills are still very low.

Thanks a lot.

Best