gamemaker吧 关注:13,584贴子:94,424
  • 0回复贴,共1

gms2 中文文本自动换行实现

取消只看楼主收藏回复

今天在做对话框的时候,想要实现一个文本超长自动换行的效果。结果试了半天,发现gms2的draw_text_ext()函数的换行居然是基于空格实现,对中文极度不友好。去git找了半天也没找到啥中文能用的、好用的扩展或者脚本。无奈只好自己造轮子了。
暂时想不到更好的办法,权当抛砖引玉了。
以下是代码:
=====================分隔符=========================
// 绘制文本且自动换行
function draw_txt_ext(_x, _y, _text, _sep, _width) {
// 文本高度
var _text_h = 0;
// 文本长度
var _len = string_length(_text);
// 截取长度
var _j = 1;
//
var _i = 1;
repeat _len {
var _cut = string_copy(_text, 1, _j);
// 循环结束,绘制剩余文本
if _i == _len {
draw_text(_x, _y + _text_h, _text);
_text_h += _sep;
}
// 处理换行符
else if string_count("\n",_cut) > 0 {
_cut = string_replace(_cut, "\n", "");
draw_text(_x, _y + _text_h, _cut);
_text = string_delete(_text, 1, _j);
_text_h += _sep;
_j = 1;
}
// 文本超长时。
else if string_width(_cut) > _width {
var _pre_j = _j - 1;
while is_letter(string_ord_at(_cut, _pre_j)) {
if _pre_j == 0 break;
_pre_j--;
}
if _pre_j == 0 _pre_j = _j - 1;
_j = _j - _pre_j;
_cut = string_copy(_text, 1, _pre_j);
draw_text(_x, _y + _text_h, _cut);
_text = string_delete(_text, 1, _pre_j);
_text_h += _sep;
}
_j += 1;
_i += 1;
}
// 返回最终文本高度
return _text_h;
}
// 判断字符是否是英文字母
function is_letter(_byte) {
return (_byte >= string_byte_at("a",1) && _byte <= string_byte_at("z",1))
|| (_byte >= string_byte_at("A",1) && _byte <= string_byte_at("Z",1));
}
========================分隔符========================
姑且实现了中文换行,英文分词,以及超长英文单词的短横杆连接。
以下是效果图



IP属地:四川1楼2024-04-12 03:41回复