Skip to content

Instantly share code, notes, and snippets.

@ViliamKopecky
Created October 31, 2012 14:20
Show Gist options
  • Save ViliamKopecky/3987279 to your computer and use it in GitHub Desktop.
Save ViliamKopecky/3987279 to your computer and use it in GitHub Desktop.
Dynamická velikost fontu podle maximální výšky
// -- funkce vezme TextField, vyzkouší na něj různý velikosti písma a vybere tu nejbližší maxHeight --
var fit = function(txt:TextField, maxHeight:Number) {
var format:TextFormat = text_txt.getTextFormat();
// -- 300 je největší povolená veliksot písma - to by se asi stávat nemělo --
for(var k=300;1<k;k--) {
format.size = k;
text_txt.setTextFormat(format);
if(text_txt.textHeight <= maxHeight) {
trace("text-height: " + text_txt.textHeight);
trace(" font-size: " + k);
break;
}
}
};
// -- tesovací délky textů --
//text_txt.text = "Lorem";
//text_txt.text = "Lorem ipsum dolor sit amet";
//text_txt.text = "Lorem ipsum dolor sit amet consectetur adilpisci elit";
text_txt.text = "Lorem ipsum dolor sit amet consectetur adilpisci elit. Lorem ipsum dolor sit amet consectetur adilpisci elit";
//text_txt.text = "Lorem ipsum dolor sit amet consectetur adilpisci elit. Lorem ipsum dolor sit amet consectetur adilpisci elit. Lorem ipsum dolor sit amet consectetur adilpisci elit";
//text_txt.text = "Lorem ipsum dolor sit amet consectetur adilpisci elit. Lorem ipsum dolor sit amet consectetur adilpisci elit. Lorem ipsum dolor sit amet consectetur adilpisci elit. Lorem ipsum dolor sit amet consectetur adilpisci elit";
// -- nastavení textfieldu: multiline a zalamování řádků --
text_txt.multiline = true;
text_txt.wordWrap = true;
// -- nastaví velikost fontu, na velikost která se vejde do 200 --
fit(text_txt, 200);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment