Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buttons and fonts #109

Open
HynekBaran opened this issue Mar 25, 2017 · 3 comments
Open

Buttons and fonts #109

HynekBaran opened this issue Mar 25, 2017 · 3 comments

Comments

@HynekBaran
Copy link

Hi,
I like to set custom fonts on Adafruit_GFX_Button.
Unfortunately, when custom font is set on the fly, buttons does not respect the text position and size. I think that

  _gfx->setCursor(_x1 + (_w/2) - (strlen(_label) * 3 * _textsize),
    _y1 + (_h/2) - (4 * _textsize));

shoud be replaced using getTextBounds() code.

@HynekBaran
Copy link
Author

HynekBaran commented Mar 25, 2017

My solution (in Adafruit_GFX_Button::drawButton):

  if(_gfx->gfxFont) {
	  int16_t  tx, ty; // HB
	  uint16_t tw, th; // HB
	  _gfx->getTextBounds(_label, _gfx->width()/2, _gfx->height()/2, &tx, &ty, &tw, &th); // HB
	  _gfx->setCursor(_x1 + (_w/2) - tw/2, _y1 + (_h/2) - th/2); // HB
  } else { 		
  	  _gfx->setCursor(_x1 + (_w/2) - strlen(_label)*3*_textsize, _y1 + (_h/2) -  4*_textsize); // HB
  }
@HynekBaran
Copy link
Author

HynekBaran commented Mar 25, 2017

But my solution is just a hint, consider two buttons with labels "bxxx" and "pxxx" :(

@HynekBaran
Copy link
Author

This is probably slightly better:

  if(_gfx->gfxFont) {
      int16_t tx0 =  _gfx->width()/2, ty0=_gfx->height()/2; // HB
	  int16_t  tx, ty; // HB
	  uint16_t tw, th; // HB
	  _gfx->getTextBounds(_label, tx0, ty0, &tx, &ty, &tw, &th); // HB
	  _gfx->setCursor(_x1 + (_w/2) - tw/2 + (tx0-tx), _y1 + (_h/2) - th/2 + (ty0-ty)); // HB
  } else { 		
  	  _gfx->setCursor(_x1 + (_w/2) - strlen(_label)*3*_textsize, _y1 + (_h/2) -  4*_textsize); // HB
  }

but the bp problem still remains...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant