iOS Question Draw a dashed or dotted line around a view

andyr00d

Member
Licensed User
Longtime User
Hi there,

I want to draw a dashed or dotted line around an imageview, which could also be a panel set to transparent. I was able to achieve it easily in B4A but I'm finding it a bit difficult in B4i... I've tried using a NativeObject but no luck:

B4X:
'Dim no As NativeObject = iv
'no.SetField("borderStyle","dashed")

which throws: setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key borderStyle.

Maybe this value isn't valid for this view, but I saw it somewhere on the Apple documentation... is there any way to achieve this?

Thanks!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is a bit more complicated in iOS. You can use this code:
B4X:
Sub AddDashedBorder(v As View, clr As Int)
   Dim no As NativeObject = Me
   no.RunMethod("addDashedBorder::", Array(v, no.ColorToUIColor(clr)))
End Sub

#if OBJC
- (void) addDashedBorder:(UIView*)v :(UIColor*) clr {
   CAShapeLayer* border;
   NSArray* layers = v.layer.sublayers;
   if ([layers count] == 0 || [layers.lastObject isKindOfClass:[CAShapeLayer class]] == false) {
     border = [CAShapeLayer layer];
     [v.layer addSublayer:border];
   }
   else
     border = layers.lastObject;
   border.strokeColor = clr.CGColor;
   border.fillColor = nil;
   border.lineDashPattern = @[@4, @2];
   
   border.path = [UIBezierPath bezierPathWithRect:v.bounds].CGPath;
   border.frame = v.bounds;
}
#end if

Call AddDashedBorder from the Resize event.
 
Upvote 0
Top