iOS Question Please add two ints as input to this OBJC function

Sandman

Expert
Licensed User
Longtime User
About a year ago @JordiCP made a nice solution for adjusting the link color in textviews. It can be found here:

However, he hardcoded the colors in the OBJC code, and I would like them to be inputs instead. I have tried for almost a full day to achieve this seemingly basic adjustment but I just can't seem to figure it out. Therefore I'm asking if someone in the forum can help out?

In my situation I actually have the colors defined as hex strings, but I can easily convert them to ints using this function by Erel:

This is how far I've come by searching the interwebs and the stack overflows, which is not a working solution:
B4X:
Me.As(NativeObject).RunMethod("changeTextViewLinkAttributes:", Array(myTextView, 16711680, 16711680))
(The 16711680 is simply #FF0000 as Int, hardcoded at the moment while testing.)

Objective-C:
#if OBJC
-(void) changeTextViewLinkAttributes:(UITextView *) textView: (int) linkColor: (int) underlineColor {

    NSDictionary *linkAttributes = @{

        NSForegroundColorAttributeName:
            [UIColor
                colorWithRed:((float)((linkColor & 0xFF0000) >> 16))/255.0 \
                green:((float)((linkColor & 0x00FF00) >>  8))/255.0 \
                blue:((float)((linkColor & 0x0000FF) >>  0))/255.0 \
                alpha:1.0],

        NSUnderlineColorAttributeName: 
            [UIColor
                colorWithRed:((float)((underlineColor & 0xFF0000) >> 16))/255.0 \
                green:((float)((underlineColor & 0x00FF00) >>  8))/255.0 \
                blue:((float)((underlineColor & 0x0000FF) >>  0))/255.0 \
                alpha:1.0],

        NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)
        };

    textView.linkTextAttributes = linkAttributes;  
}
#End If

The error for the above is...
B4X:
Method not found: changeTextViewLinkAttributes: <snip>
...so I assume there is something I just don't understand when it comes to the OBJC parameters.
 

Sandman

Expert
Licensed User
Longtime User
Awesome, thank you! :)

And for the readers of the thread that, like me, couldn't really see what the difference was:
  1. The call to the function now uses three colons. (Perhaps one colon per parameter?)
  2. In OBJC it seems a parameter in the signature must end with a space and THEN a colon. I had done the opposite.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. True.
2. The confusing syntax is related to the parameters names kept empty. Another example that will clarify the syntax:
B4X:
-(void) changeTextViewLinkAttributes:(UITextView *) textView LinkColorParam:(int)linkColor UnderlineParam:(int) underlineColor {
Me.As(NativeObject).RunMethod("changeTextViewLinkAttributes:LinkColorParam:UnderlineParam:", Array(myTextView, 16711680, 16711680))
'and calling from Objective C:
[self changeTextViewLinkAttributes:TextView1 LinkColorParam:1234 UnderlineParam:5678]
 
Last edited:
Upvote 0

Sandman

Expert
Licensed User
Longtime User
It sure is a different syntax. It kind of looks like named parameters, but as far as I can tell it really isn't. Found some more to read about it here:

Specifically this answer:

(I realize you know all this, Erel, I'm just adding the links above for other's - and my future - benefit.)
 
Upvote 0
Top