iOS Question CSBuilder : Adding an image..

Mashiane

Expert
Licensed User
Ola

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

B4X:
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.

B4X:
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...

B4X:
   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

#Converting_From_B4A_2_B4I
 

Semen Matusovskiy

Well-Known Member
Licensed User
SetBounds is enough strange. Actually OBJC is not needed. You can use something like
B4X:
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
Top