MCPcopy Create free account
hub / github.com/codenameone/CodenameOne / initAutoResize

Method initAutoResize

CodenameOne/src/com/codename1/ui/Label.java:942–995  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

940 }
941
942 void initAutoResize() {
943 if (autoSizeMode) {
944 Style s = getUnselectedStyle();
945 int p = s.getHorizontalPadding();
946 int w = getWidth();
947 if (w > p + 10) {
948 if (originalFont == null) {
949 originalFont = s.getFont();
950 } else {
951 if (w == widthAtLastCheck) {
952 return;
953 }
954 }
955
956 Font currentFont = getUnselectedStyle().getFont();
957 float fontSize = currentFont.getPixelSize();
958 if (fontSize < 1) {
959 Log.p("Autosize disabled probably because component wasn't using native fonts for UIID: " + getUIID());
960 autoSizeMode = false;
961 return;
962 }
963 widthAtLastCheck = w;
964 autoSizeMode = false;
965 int currentWidth = calcPreferredSize().getWidth();
966 int maxSizePixel = Display.getInstance().convertToPixels(maxAutoSize);
967 while (currentWidth < w) {
968 fontSize++;
969 if (fontSize >= maxSizePixel) {
970 fontSize = maxSizePixel;
971 currentFont = currentFont.derive(maxSizePixel, currentFont.getStyle());
972 getAllStyles().setFont(currentFont);
973 currentWidth = calcPreferredSize().getWidth();
974 break;
975 }
976 currentFont = currentFont.derive(fontSize, currentFont.getStyle());
977 getAllStyles().setFont(currentFont);
978 currentWidth = calcPreferredSize().getWidth();
979 }
980 int minSizePixel = Display.getInstance().convertToPixels(minAutoSize);
981 while (currentWidth > w) {
982 fontSize--;
983 if (fontSize <= minSizePixel) {
984 currentFont = currentFont.derive(minSizePixel, currentFont.getStyle());
985 getAllStyles().setFont(currentFont);
986 break;
987 }
988 currentFont = currentFont.derive(fontSize, currentFont.getStyle());
989 getAllStyles().setFont(currentFont);
990 currentWidth = calcPreferredSize().getWidth();
991 }
992 autoSizeMode = true;
993 }
994 }
995 }
996
997 /// {@inheritDoc}
998 @Override

Callers 4

laidOutMethod · 0.95
paintMethod · 0.95
paintImplMethod · 0.95
paintMethod · 0.80

Calls 15

getHorizontalPaddingMethod · 0.95
getFontMethod · 0.95
getPixelSizeMethod · 0.95
pMethod · 0.95
calcPreferredSizeMethod · 0.95
getInstanceMethod · 0.95
deriveMethod · 0.95
getStyleMethod · 0.95
getWidthMethod · 0.65
getFontMethod · 0.65
convertToPixelsMethod · 0.65
setFontMethod · 0.65

Tested by

no test coverage detected