iOS Question CSBuilder : Adding an image..


Licensed User

In B4A, adding an image to the CSBuilder is easy peasy as this example below.

eachD.Image(LoadBitmap(File.DirAssets, "sos.png"), 70dip, 65dip, False).Append(CRLF)

In b4i, there is no .Image call for the CSBuilder, but this code found here, seems to do the trick.

Sub AppendImage(cs As CSBuilder, bmp As Bitmap)
   Dim attachment As NativeObject
   attachment = attachment.Initialize("NSTextAttachment").RunMethod("new", Null)
   attachment.SetField("image", bmp)
   Dim nme As NativeObject = Me
   'set bounds
   nme.RunMethod("SetBounds::", Array(attachment, attachment.MakeRect(0, 0, 50, 100)))
   Dim attributedString As NativeObject
   attributedString = attributedString.Initialize("NSAttributedString") _
     .RunMethod("attributedStringWithAttachment:", Array(attachment))
   Dim no As NativeObject = cs
   no.RunMethod("appendAttributedString:", Array(attributedString))
End Sub

#if OBJC
-(void)SetBounds:(NSTextAttachment*) attachment :(NSData*)data {
   attachment.bounds = *(CGRect*)data.bytes;
#End If

I understand that perhaps this line...

   nme.RunMethod("SetBounds::", Array(attachment, attachment.MakeRect(0, 0, 50, 100)))

Resizes the image, so in my case, I have to pass 70 and 65 as above? Thanks


Semen Matusovskiy

Well-Known Member
Licensed User
SetBounds is enough strange. Actually OBJC is not needed. You can use something like
attachment.RunMethod ("setBounds:", Array (attachment.MakeRect (imageOffsetX, imageOffsetY, imageWidth, imageHeight)))
imageOffsetX is a distance between previous text and an image. Positive imageOffsetY raises an image. Negative - lowers.

You can use LoadBitmapResize and do not use setBounds at all (of course, if you do not need to shift an image).
Upvote 0