Skip to main content
Question

Does anybody have an example of successful implementation of setRangeFills()?

  • April 14, 2023
  • 11 replies
  • 546 views

aamir.shaikh

I’m trying this format but it’s not working
text.setRangeFills(start, end, object.fills)

This topic has been closed for replies.

11 replies

tank666
  • April 14, 2023

It must be an array of objects.


aamir.shaikh

Yes it is, but it still doesn’t work. Here’s object.fills:

[
    {
        "type": "SOLID",
        "visible": true,
        "opacity": 1,
        "blendMode": "NORMAL",
        "color": {
            "r": 0.9416666626930237,
            "g": 0,
            "b": 3.367662486652989e-7
        }
    }
]

tank666
  • April 14, 2023

What error do you see in the console?

figma.com

aamir.shaikh

I don’t see any error.

I am trying to get and set fills for a text node.
The reference text is “hello”, where “he” is white and “llo” is red.
When I apply the fills to the output, it is all white and not according to the reference text.


aamir.shaikh

image
it is between 0 and 1


tank666
  • April 14, 2023

How many times do you call the setRangeFills method?


aamir.shaikh

20 times in the above case (string.length * 4)


tank666
  • April 14, 2023

Sorry, but you are doing something wrong. You only need to call this method 2 times. The first time to set the fill to white, the second time to set it to red.

text.setRangeFills(0, 2, [whiteFill]);
text.setRangeFills(3, 5, [redFill]);

aamir.shaikh

Sorry for the confusion.

I am calling it 2 times per Text Node. I have 2 text nodes for each letter in the string.
So, 2 x 2 x 5 = 20


tank666
  • April 14, 2023

At the moment, I can only suggest that you revise your code that was left behind the scenes. Or share enough code so that anyone can reproduce what you’re experiencing.


aamir.shaikh

I rewrote my code snippet separately, and it worked fine.

let refText = figma.currentPage.findOne(n => n.name === "Testing...") as TextNode
let styles = refText.getStyledTextSegments(['fills'])

let subject = figma.currentPage.selection[0] as TextNode
for (let style = 0; style < styles.length; style++) {
  const object = styles[style]
  subject.setRangeFills(object.start, object.end, object.fills)
}

figma.closePlugin()

Now I will compare and see what mistake I am making. Thanks for the help, @tank666