iOS: Support colored text in labels in launcher

This commit is contained in:
UnknownShadow200 2022-05-13 22:57:35 +10:00
parent 64e892741a
commit 40183b6059
3 changed files with 39 additions and 14 deletions

View file

@ -323,7 +323,7 @@ cc_bool Drawer2D_ValidColorCodeAt(const cc_string* text, int i) {
return BitmapCol_A(Drawer2D_GetColor(text->buffer[i])) != 0;
}
cc_bool Drawer2D_NextPart(cc_string* left, cc_string* part, BitmapCol* color) {
cc_bool Drawer2D_UNSAFE_NextPart(cc_string* left, cc_string* part, BitmapCol* color) {
BitmapCol c;
int i;
@ -356,7 +356,7 @@ cc_bool Drawer2D_IsEmptyText(const cc_string* text) {
cc_string left = *text, part;
BitmapCol color;
while (Drawer2D_NextPart(&left, &part, &color))
while (Drawer2D_UNSAFE_NextPart(&left, &part, &color))
{
if (part.length) return false;
}
@ -367,7 +367,7 @@ void Drawer2D_WithoutColors(cc_string* str, const cc_string* src) {
cc_string left = *src, part;
BitmapCol color;
while (Drawer2D_NextPart(&left, &part, &color))
while (Drawer2D_UNSAFE_NextPart(&left, &part, &color))
{
String_AppendString(str, &part);
}
@ -565,9 +565,10 @@ static int MeasureBitmappedWidth(const struct DrawTextArgs* args) {
}
width += Drawer2D_Width(point, c) + xPadding;
}
if (!width) return 0;
/* Remove padding at end */
if (width && !(args->font->flags & FONT_FLAGS_PADDING)) width -= xPadding;
if (!(args->font->flags & FONT_FLAGS_PADDING)) width -= xPadding;
if (args->useShadow) { width += Drawer2D_ShadowOffset(point); }
return width;
@ -582,7 +583,6 @@ void Drawer2D_DrawText(struct Bitmap* bmp, struct DrawTextArgs* args, int x, int
}
int Drawer2D_TextWidth(struct DrawTextArgs* args) {
if (Drawer2D_IsEmptyText(&args->text)) return 0;
if (Font_IsBitmap(args->font)) return MeasureBitmappedWidth(args);
return Font_SysTextWidth(args);
}
@ -755,7 +755,7 @@ static int Font_SysTextWidth(struct DrawTextArgs* args) {
BitmapCol color;
interop_SetFont(font->handle, font->size, font->flags);
while (Drawer2D_NextPart(&left, &part, &color))
while (Drawer2D_UNSAFE_NextPart(&left, &part, &color))
{
char buffer[NATIVE_STR_LEN];
int len = Platform_EncodeUtf8(buffer, &part);
@ -776,7 +776,7 @@ static void Font_SysTextDraw(struct DrawTextArgs* args, struct Bitmap* bmp, int
y += (args->font->height - args->font->size) / 2;
interop_SetFont(font->handle, font->size, font->flags);
while (Drawer2D_NextPart(&left, &part, &color))
while (Drawer2D_UNSAFE_NextPart(&left, &part, &color))
{
char buffer[NATIVE_STR_LEN];
int len = Platform_EncodeUtf8(buffer, &part);
@ -1179,6 +1179,7 @@ static int Font_SysTextWidth(struct DrawTextArgs* args) {
}
width += charWidth;
}
if (!width) return 0;
width = TEXT_CEIL(width);
if (args->useShadow) width += 2;

View file

@ -95,7 +95,7 @@ void Drawer2D_WithoutColors(cc_string* str, const cc_string* src);
char Drawer2D_LastColor(const cc_string* text, int start);
/* Returns whether the color code is f, F or \0 */
cc_bool Drawer2D_IsWhiteColor(char c);
cc_bool Drawer2D_NextPart(cc_string* left, cc_string* part, BitmapCol* color);
cc_bool Drawer2D_UNSAFE_NextPart(cc_string* left, cc_string* part, BitmapCol* color);
void Drawer2D_ReducePadding_Tex(struct Texture* tex, int point, int scale);
void Drawer2D_ReducePadding_Height(int* height, int point, int scale);

View file

@ -528,6 +528,28 @@ static NSString* ToNSString(const cc_string* text) {
return [NSString stringWithUTF8String:raw];
}
static NSMutableAttributedString* ToAttributedString(const cc_string* text) {
cc_string left = *text, part;
BitmapCol color = Drawer2D.Colors['f'];
NSMutableAttributedString* str = [[NSMutableAttributedString alloc] init];
while (Drawer2D_UNSAFE_NextPart(&left, &part, &color))
{
char raw[NATIVE_STR_LEN];
Platform_EncodeUtf8(raw, &part);
NSString* bit = [NSString stringWithUTF8String:raw];
NSDictionary* attrs =
@{
//NSFontAttributeName : font,
NSForegroundColorAttributeName : ToUIColor(color, 1.0f)
};
NSAttributedString* attr_bit = [[NSAttributedString alloc] initWithString:bit attributes:attrs];
[str appendAttributedString:attr_bit];
}
return str;
}
static void FreeContents(void* info, const void* data, size_t size) { Mem_Free(data); }
// TODO probably a better way..
static UIImage* ToUIImage(struct Bitmap* bmp) {
@ -758,7 +780,7 @@ void LBackend_DrawLogo(struct Bitmap* bmp, const char* title) {
NSDictionary* attrs_bg =
@{
NSFontAttributeName : font,
NSForegroundColorAttributeName : [UIColor blackColor]
NSForegroundColorAttributeName : UIColor.blackColor
};
NSAttributedString* str_bg = [[NSAttributedString alloc] initWithString:text attributes:attrs_bg];
DrawText(str_bg, bmp, 4, 42);
@ -766,7 +788,7 @@ void LBackend_DrawLogo(struct Bitmap* bmp, const char* title) {
NSDictionary* attrs_fg =
@{
NSFontAttributeName : font,
NSForegroundColorAttributeName : [UIColor whiteColor]
NSForegroundColorAttributeName : UIColor.whiteColor
};
NSAttributedString* str_fg = [[NSAttributedString alloc] initWithString:text attributes:attrs_fg];
DrawText(str_fg, bmp, 0, 38);
@ -850,7 +872,7 @@ void LBackend_CheckboxInit(struct LCheckbox* w) {
[swt addTarget:ui_controller action:@selector(handleValueChanged:) forControlEvents:UIControlEventValueChanged];
UILabel* lbl = [[UILabel alloc] init];
lbl.textColor = [UIColor whiteColor];
lbl.textColor = UIColor.whiteColor;
lbl.text = ToNSString(&w->text);
lbl.translatesAutoresizingMaskIntoConstraints = false;
[lbl sizeToFit]; // adjust label to fit text
@ -923,7 +945,7 @@ void LBackend_InputInit(struct LInput* w, int width) {
UITextField* fld = [[UITextField alloc] init];
fld.frame = CGRectMake(0, 0, width, LINPUT_HEIGHT);
fld.borderStyle = UITextBorderStyleBezel;
fld.backgroundColor = [UIColor whiteColor];
fld.backgroundColor = UIColor.whiteColor;
fld.delegate = ui_controller;
[fld addTarget:ui_controller action:@selector(handleTextChanged:) forControlEvents:UIControlEventEditingChanged];
@ -951,12 +973,14 @@ void LBackend_LabelInit(struct LLabel* w) {
UILabel* lbl = [[UILabel alloc] init];
lbl.textColor = [UIColor whiteColor];
if (w->small) lbl.font = [UIFont systemFontOfSize:14.0f];
AssignView(w, lbl);
}
void LBackend_LabelUpdate(struct LLabel* w) {
UILabel* lbl = (__bridge UILabel*)w->meta;
lbl.text = ToNSString(&w->text);
lbl.attributedText = ToAttributedString(&w->text);
[lbl sizeToFit]; // adjust label to fit text
}
void LBackend_LabelDraw(struct LLabel* w) { }
@ -1003,7 +1027,7 @@ void LBackend_TableInit(struct LTable* w) {
UITableView* tbl = [[UITableView alloc] init];
tbl.delegate = ui_controller;
tbl.dataSource = ui_controller;
LTable_UpdateCellColor(tbl, NULL, 0, false);
LTable_UpdateCellColor(tbl, NULL, 1, false);
//[tbl registerClass:UITableViewCell.class forCellReuseIdentifier:cellID];
AssignView(w, tbl);