问题描述
我想知道是否可以在 Raphael 中更改文本对象的子字符串的属性.例如,我想在下面的字符串神奇的巫师统治世界!"中加粗巫师"这个词.在拉斐尔文本对象中.我已经研究过使用 Raphael.print() 方法,我需要一些来自文本的属性用于代码的其他部分.
I was wondering if it's possible to change the attributes of a substring of the text object in Raphael. For example, I want to bold the word wizard in the following string "The magical wizard ruled the world!" in a raphael text object. I have looked at using the Raphael.print() method and I need some of the attributes from text for other portions of the code.
推荐答案
字体是在元素级别设置的,就像在常规 html 中一样.要将单独的字体或样式应用到特定单词,您需要将文本分成单独的元素.
Fonts are set at the element level, much like in regular html. In order to apply a separate font or style to a specific word, you would need to break your text into separate elements.
var start = paper.text(20, 20, "The magical ");
start.attr({ "text-anchor": "start" });
var startBox = start.getBBox();
var bold = paper.text(startBox.width + startBox.x, 20, "wizard ");
bold.attr({ "text-anchor": "start", "font-weight": "bold" });
var boldBox = bold.getBBox();
var end = paper.text(boldBox.width + boldBox.x, 20, "ruled the world!");
end.attr({ "text-anchor": "start" });
这篇关于Raphaeljs 子字符串文本属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!