iOS Question CSBuilder Size AppendImage

MarcoRome

Expert
Licensed User
Longtime User
Hi all.
I need to change the size of an image in a button contained in a ScroollView.
I used:

B4X:
Dim cs1 As CSBuilder
                    cs1.Initialize
                    AppendImage(cs1, LoadBitmap(File.DirAssets, $"${m.Get("immagine")}"$))
                    cs1.Append(CRLF)
                    cs1.Color(Colors.White).Font(Font.CreateNewBold(10)).Append($"${m.Get("categoria")}"$)
                    cs1.PopAll

                    ButtonSetAttributedText(btnInside,cs1, cs1, cs1.Initialize.Append("disabled"))

B4X:
Sub AppendImage(cs As CSBuilder, bmp As Bitmap)
    Dim attachment As NativeObject
    attachment = attachment.Initialize("NSTextAttachment").RunMethod("new", Null)
    attachment.SetField("image", bmp )
    attachment.MakeSize(30.0,30.0)
    Dim attributedString As NativeObject
    attributedString = attributedString.Initialize("NSAttributedString") _
     .RunMethod("attributedStringWithAttachment:", Array(attachment))
    Dim no As NativeObject = cs
    no.RunMethod("appendAttributedString:", Array(attributedString))
End Sub

Sub ButtonSetAttributedText(btn As Button, NormalText As AttributedString, HighlightedText As AttributedString, _
     DisabledText As AttributedString)
    Dim no As NativeObject = btn
    no.RunMethod("setAttributedTitle:forState:", Array(NormalText, 0))
    no.RunMethod("setAttributedTitle:forState:", Array(HighlightedText, 1))
    no.RunMethod("setAttributedTitle:forState:", Array(DisabledText, 2))
End Sub

if i have
attachment.MakeSize(30.0,30.0)

or this

attachment.MakeSize(80.0,80.0)

dont change nothing.
Any idea ?
Thank you
 
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
As this ( in attachment ) 100 x 100

 

Attachments

  • elettricista.png
    4.3 KB · Views: 305
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
if i write:

B4X:
Sub AppendImage(cs As CSBuilder, bmp As Bitmap)
    Dim attachment As NativeObject
    attachment = attachment.Initialize("NSTextAttachment").RunMethod("new", Null)
    attachment.SetField("image", bmp )
    'attachment.SetField("bounds", attachment.MakeRect(0.0,0.0,80.0,80.0))
    attachment.SetField("bounds", attachment.MakeSize(80.0,80.0))
    Dim attributedString As NativeObject
    attributedString = attributedString.Initialize("NSAttributedString") _
     .RunMethod("attributedStringWithAttachment:", Array(attachment))
    Dim no As NativeObject = cs
    no.RunMethod("appendAttributedString:", Array(attributedString))
   

End Sub

i have this error:


The line 196 is:
attachment.SetField("bounds", attachment.MakeSize(80.0,80.0))

same thing if i use:
attachment.SetField("bounds", attachment.MakeRect(0.0,0.0,80.0,80.0))
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
B4X:
Sub AppendImage(cs As CSBuilder, bmp As Bitmap)
    Dim attachment As NativeObject
    attachment = attachment.Initialize("NSTextAttachment").RunMethod("new", Null)
    attachment.SetField("image", bmp )
    attachment.SetField("bounds", attachment.MakeRect(0.0,0.0,80.0,80.0))
    Dim attributedString As NativeObject
    attributedString = attributedString.Initialize("NSAttributedString") _
     .RunMethod("attributedStringWithAttachment:", Array(attachment))
    Dim no As NativeObject = cs
    no.RunMethod("appendAttributedString:", Array(attributedString))
End Sub

The error is:

 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This should have been the first post

Correct code:
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
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…