lv_draw_label: trim the traling whitespaces when aligned to the middle
This commit is contained in:
@@ -66,7 +66,7 @@ void lv_draw_label(const lv_area_t * coords, const lv_area_t * mask, const lv_st
|
||||
}
|
||||
|
||||
/*Init variables for the first line*/
|
||||
lv_coord_t line_length = 0;
|
||||
lv_coord_t line_width = 0;
|
||||
uint32_t line_start = 0;
|
||||
uint32_t line_end = lv_txt_get_next_line(txt, font, style->text.letter_space, w, flag);
|
||||
|
||||
@@ -76,9 +76,20 @@ void lv_draw_label(const lv_area_t * coords, const lv_area_t * mask, const lv_st
|
||||
|
||||
/*Align the line to middle if enabled*/
|
||||
if(flag & LV_TXT_FLAG_CENTER) {
|
||||
line_length = lv_txt_get_width(&txt[line_start], line_end - line_start,
|
||||
line_width = lv_txt_get_width(&txt[line_start], line_end - line_start,
|
||||
font, style->text.letter_space, flag);
|
||||
pos.x += (w - line_length) / 2;
|
||||
/*Trim closing spaces.*/
|
||||
uint16_t i;
|
||||
for(i = line_end - 1; i > 0; i--) {
|
||||
if(txt[i] == ' ' || txt[i] == '\n' || txt[i] == '\r') {
|
||||
line_width -= lv_font_get_width(font, txt[i]);
|
||||
line_width -= style->text.letter_space;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pos.x += (w - line_width) / 2;
|
||||
}
|
||||
|
||||
lv_opa_t opa = (uint16_t)((uint16_t) style->text.opa * opa_scale) >> 8;
|
||||
@@ -166,9 +177,21 @@ void lv_draw_label(const lv_area_t * coords, const lv_area_t * mask, const lv_st
|
||||
pos.x = coords->x1;
|
||||
/*Align to middle*/
|
||||
if(flag & LV_TXT_FLAG_CENTER) {
|
||||
line_length = lv_txt_get_width(&txt[line_start], line_end - line_start,
|
||||
line_width = lv_txt_get_width(&txt[line_start], line_end - line_start,
|
||||
font, style->text.letter_space, flag);
|
||||
pos.x += (w - line_length) / 2;
|
||||
|
||||
/*Trim closing spaces.*/
|
||||
uint16_t i;
|
||||
for(i = line_end - 1; i > line_start; i--) {
|
||||
if(txt[i] == ' ' || txt[i] == '\n' || txt[i] == '\r') {
|
||||
line_width -= lv_font_get_width(font, txt[i]);
|
||||
line_width -= style->text.letter_space;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pos.x += (w - line_width) / 2;
|
||||
}
|
||||
/*Go the next line position*/
|
||||
pos.y += lv_font_get_height(font);
|
||||
|
||||
Reference in New Issue
Block a user