How to create attributed String with rounded label in it?

Hi,

How to create attributed String with rounded label in it?


Sample Image:

Answered by Claude31 in 399129022

So, create the UIImage of the colored circle in code:

https://stackoverflow.com/questions/45015931/swift-3-draw-uiimage-programmatically


Then use it for attachment.


Does that make sense to you ?

What do you mean "with rounded label in it"?

Hi,

i need to create a string with a colored circle before it.

I know that we can attach an image with an attributed string.

So i thought to create an attributed string with a label.

Is it possible?

Yes it is possible.


See details here:

h ttps://www.hackingwithswift.com/example-code/system/how-to-insert-images-into-an-attributed-string-with-nstextattachment


// create an NSMutableAttributedString that we'll append everything to
let fullString = NSMutableAttributedString(string: "Start of text")

// create our NSTextAttachment
let image1Attachment = NSTextAttachment()
image1Attachment.image = UIImage(named: "circleIcon.png")

// wrap the attachment in its own attributed string so we can append it
let image1String = NSAttributedString(attachment: image1Attachment)

// add the NSTextAttachment wrapper to our full string, then add some more text.
fullString.append(image1String)
fullString.append(NSAttributedString(string: "End of text"))

// draw the result in a label
yourLabel.attributedText = fullString

Hi,

I dont want to attach an image in attributed string.

As the color code of the circle comes from api,

i need to attach a label and i need to set the backgorund color of the label with the color comes from api.

How to acheive this?

I don't understand

You said : i need to attach a label

when you say label, that's not a UILabel ? Is that simply a string ?


Attach to what ?

Attaching a UILabel inside an attributedString does not make sense.

What you can do is compute an image of a label with a circle, set its backgroundColor programmatically and include in AttributedString as explained before through attachement.

But I understand you don't want that.


So, do you want to attach to the UILabel where the attributedString is displayed ?


Let's try to draw it

___ _________________________

| 1 | | attributedString |

|___| |________________________|

UILabel UILabel


1 is the label, with a background color.


Is it what you want ?

If not, please try to draw what you want

Hi,

Sample String :

" Starting String ➖ end String " ( hypen not required in the circle).

i will get the color of the circle from api.

According to the color received from api, i need to change the color of the circle.

Accepted Answer

So, create the UIImage of the colored circle in code:

https://stackoverflow.com/questions/45015931/swift-3-draw-uiimage-programmatically


Then use it for attachment.


Does that make sense to you ?

Hi Claude31,

Thank you very much for your support and the solution works for me.

How to create attributed String with rounded label in it?
 
 
Q