Add lv_label_set_text_fmt

This commit is contained in:
Themba Dube
2019-08-06 09:28:50 -04:00
parent e53a30381a
commit ba1fba1f10
3 changed files with 54 additions and 10 deletions

View File

@@ -139,16 +139,6 @@ static inline void _out_null(char character, void* buffer, size_t idx, size_t ma
}
// internal _putchar wrapper
static inline void _out_char(char character, void* buffer, size_t idx, size_t maxlen)
{
(void)buffer; (void)idx; (void)maxlen;
if (character) {
_putchar(character);
}
}
// internal output function wrapper
static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen)
{

View File

@@ -13,6 +13,7 @@
#include "../lv_core/lv_group.h"
#include "../lv_misc/lv_color.h"
#include "../lv_misc/lv_math.h"
#include "../lv_misc/lv_printf.h"
/*********************
* DEFINES
@@ -203,6 +204,51 @@ void lv_label_set_text(lv_obj_t * label, const char * text)
lv_label_refr_text(label);
}
/**
* Set a new formatted text for a label. Memory will be allocated to store the text by the label.
* @param label pointer to a label object
* @param fmt `printf`-like format
*/
void lv_label_set_text_fmt(lv_obj_t * label, const char * fmt, ...)
{
lv_obj_invalidate(label);
lv_label_ext_t * ext = lv_obj_get_ext_attr(label);
/*If text is NULL then refresh */
if(fmt == NULL) {
lv_label_refr_text(label);
return;
}
if(ext->text != NULL && ext->static_txt == 0) {
lv_mem_free(ext->text);
ext->text = NULL;
}
va_list ap, ap2;
va_start(ap, fmt);
va_copy(ap2, ap);
/*Allocate space for the new text by using trick from C99 standard section 7.19.6.12 */
uint32_t len = lv_vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
ext->text = lv_mem_alloc(len+1);
lv_mem_assert(ext->text);
if(ext->text == NULL) return;
ext->text[len-1] = 0; /* Ensure NULL termination */
lv_vsnprintf(ext->text, len, fmt, ap2);
va_end(ap2);
ext->static_txt = 0; /*Now the text is dynamically allocated*/
lv_label_refr_text(label);
}
/**
* Set a new text for a label from a character array. The array don't has to be '\0' terminated.
* Memory will be allocated to store the array by the label.

View File

@@ -21,6 +21,7 @@ extern "C" {
#if LV_USE_LABEL != 0
#include <stdarg.h>
#include "../lv_core/lv_obj.h"
#include "../lv_font/lv_font.h"
#include "../lv_font/lv_symbol_def.h"
@@ -124,6 +125,13 @@ lv_obj_t * lv_label_create(lv_obj_t * par, const lv_obj_t * copy);
*/
void lv_label_set_text(lv_obj_t * label, const char * text);
/**
* Set a new formatted text for a label. Memory will be allocated to store the text by the label.
* @param label pointer to a label object
* @param fmt `printf`-like format
*/
void lv_label_set_text_fmt(lv_obj_t * label, const char * fmt, ...);
/**
* Set a new text for a label from a character array. The array don't has to be '\0' terminated.
* Memory will be allocated to store the array by the label.