2
$\begingroup$

I have such code to slect prefer font

fonts = {};
Column[Function[font, Button[font, AppendTo[fonts, 
      font]] -> (Style[ToString[#], 30, FontFamily -> font] & /@ 
      Range[0, 9])] /@ $FontFamilies[[;; 10]]]
Dynamic[fonts]

But if I make a mistake, I will click a button more one time somtimes. Then I will get a duplicate element in list fonts. Of course I can delete those duplicate element in the list. But I want to know how to make the button just works one time still, is it possible?

$\endgroup$
  • $\begingroup$ one time ever? at most once per sec? are duplicates never allowed? $\endgroup$ – Kuba Dec 18 '17 at 12:32
7
$\begingroup$

Details aside, TogglerBar seems to fit your needs best:

fonts = {};
TogglerBar[
  Dynamic@fonts
, # -> Row[{Pane[#, 200], Range[0, 9]}, BaseStyle -> {FontFamily -> #, 20}
  ] & /@ $FontFamilies[[;; 10]]
, Appearance -> "Vertical"
]

Dynamic[fonts]

enter image description here

$\endgroup$
  • $\begingroup$ Your solution is better than what I want. $\endgroup$ – yode Dec 19 '17 at 2:13
4
$\begingroup$

Duplicates are handled automatically if you store the chosen fonts in an assocation.

fonts = <||>;
Column[Function[font, Button[font, AppendTo[fonts, font -> 0]] ->
 (Style[ToString[#], 30, FontFamily -> font] & /@ Range[0, 9])] /@ $FontFamilies[[;; 10]]]
Dynamic[Keys[fonts]]
$\endgroup$
3
$\begingroup$

Here's a straightforward way to make sure the button only adds the font once. You can so that by adding an If statement that checks if the font is already there:

fonts = {};
Column[
 Function[font,
   Button[font,
     If[FreeQ[fonts, font, {1}],
      AppendTo[fonts, font]
      ]
     ] -> (Style[ToString[#], 30, FontFamily -> font] & /@ 
      Range[0, 9])] /@ $FontFamilies[[;; 10]]]
Dynamic[fonts]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.