Changes to Sencha Touch 2.2 Theming Part 1: Using Iconography

来源:互联网 发布:金和网络财报2017 编辑:程序博客网 时间:2024/05/29 05:06

source:http://druckit.wordpress.com/2013/04/04/changes-to-sencha-touch-2-2-theming-part-1-using-iconography/

Changes to Sencha Touch 2.2 Theming Part 1: Using Iconography


Sencha Touch 2.2 handles iconography very differently than its predecessors. Whereas Touch 1.x-2.1 base-64 encoded PNG files into your app’s stylesheet, Touch 2.2 instead uses a web font from http://pictos.cc/font/ to render iconography in your app’s buttons. This new methodology should significantly improve the performance of your app, particularly with respect to memory consumption. Unfortunately, you will likely need to spend a small bit of time refactoring your code as the strings used to identify icons do not match up to the icon library that had been available in previous versions of Touch. More importantly, the procedure for incorporating your own custom iconography has changed as you will now need to encode your custom icons into a web font (covered in part 2 of this blog entry).

Reviewing the Out of the Box Icons

I’ve catalogued the new iconography and their corresponding string identifiers below:

anchorboxcde fgianchorboxuploadforbiddenlightningrss teaminfo k lm n o p q rheartlistlisttablefolderpencilchat2retweettuvw x yABtimeswitchcamerachatsettings2settings attach
ment2birdD E F G H I J K delete_
black1eye file browserhomeinbox networkkeyN P Q R S T U
 Vnewsphotospoweractionfavoritesplaneuservideotruck Z expand refresh check check2 play pausetruckchartexpandrefreshcheckcheck2playpauseforward rewind play2refresh2 minusbatteryleftrightforwardrewindplay2refresh2minusbatteryleftright wirelessspedometer more print download warning_black locate trashwirelessspedo
metermoreprintdown
loadwarning
_blacklocatetrash bankflag add delete lockunlock minus2 upbankflagadddeletelockunlockminus2up screensbellquotevolume_mutevolumehelp arrow_left arrow_rightscreensbellquotevolume
_mutevolumehelparrow
_leftarrow
_right arrow_down organize  star maps reply attachment sarrow
_downorganizebook
marksstarmapsreplyattach
mentsearch C W
Mstopshufflecartdownarrow_upcloudcomposemailstopshufflecartdownarrow
_up

Behind the scenes, the SCSS mixin icon-character-for-name maps the iconCls icon identifier to a specific character in the pictos font. This mixin is located at the following location:

touch-2-2-0/resources/themes/stylesheets/sencha-touch/base/mixins/_Class.scss

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@functionicon-character-for-name($name) {
    //http://pictos.cc/font/
 
    // Row 1
    @if($name =="anchor") { @return"a"; }
    @elseif($name == "box") { @return"b"; }
    @elseif($name == "upload") { @return"c"; }
    @elseif($name == "forbidden") { @return"d"; }
    @elseif($name == "lightning") { @return"e"; }
    @elseif($name == "rss") { @return"f"; }
    @elseif($name == "team") { @return"g"; }
//    @else if ($name == "h") { @return "h"; }
    @elseif($name == "info") { @return"i"; }
    @elseif($name == "attachment") { @return"j"; }
    @elseif($name == "heart") { @return"k"; }
    @elseif($name == "list") { @return"l"; }
    @elseif($name == "music") { @return"m"; }
    @elseif($name == "table") { @return"n"; }
    @elseif($name == "folder") { @return"o"; }
    @elseif($name == "pencil") { @return"p"; }
    @elseif($name == "chat2") { @return"q"; }
    @elseif($name == "retweet") { @return"r"; }
    @elseif($name == "search") { @return"s"; }
    @elseif($name == "time") { @return"t"; }
    @elseif($name == "switch") { @return"u"; }
    @elseif($name == "camera") { @return"v"; }
    @elseif($name == "chat") { @return"w"; }
    @elseif($name == "settings2") { @return"x"; }
    @elseif($name == "settings") { @return"y"; }
 
    // Row 2
    @elseif($name == "attachment2") { @return"A"; }
    @elseif($name == "bird") { @return"B"; }
    @elseif($name == "cloud") { @return"C"; }
    @elseif($name == "delete_black1") { @return"D"; }
    @elseif($name == "eye") { @return"E"; }
    @elseif($name == "file") { @return"F"; }
    @elseif($name == "browser") { @return"G"; }
    @elseif($name == "home") { @return"H"; }
    @elseif($name == "inbox") { @return"I"; }
    @elseif($name == "network") { @return"J"; }
    @elseif($name == "key") { @return"K"; }
//    @else if ($name == "") { @return "L"; }
    @elseif($name == "mail") { @return"M"; }
    @elseif($name == "news") { @return"N"; }
//    @else if ($name == "") { @return "O"; }
    @elseif($name == "photos") { @return"P"; }
    @elseif($name == "power") { @return"Q"; }
    @elseif($name == "action") { @return"R"; }
    @elseif($name == "favorites") { @return"S"; }
    @elseif($name == "plane") { @return"T"; }
    @elseif($name == "user") { @return"U"; }
    @elseif($name == "video") { @return"V"; }
    @elseif($name == "compose") { @return"W"; }
    @elseif($name == "truck") { @return"X"; }
//    @else if ($name == "") { @return "Y"; }
    @elseif($name == "chart") { @return"Z"; }
 
    // Row 3
    @elseif($name == "expand") { @return"`"; }
    @elseif($name == "refresh") { @return"1"; }
    @elseif($name == "check") { @return"2"; }
    @elseif($name == "check2") { @return"3"; }
    @elseif($name == "play") { @return"4"; }
    @elseif($name == "pause") { @return"5"; }
    @elseif($name == "stop") { @return"6"; }
    @elseif($name == "forward") { @return"7"; }
    @elseif($name == "rewind") { @return"8"; }
    @elseif($name == "play2") { @return"9"; }
    @elseif($name == "refresh2") { @return"0"; }
    @elseif($name == "minus") { @return"-"; }
    @elseif($name == "battery") { @return"="; }
    @elseif($name == "left") { @return"["; }
    @elseif($name == "right") { @return"]"; }
    @elseif($name == "shuffle") { @return";"; }
    @elseif($name == "wireless") { @return"'"; }
    @elseif($name == "speedometer") { @return","; }
    @elseif($name == "more") { @return"."; }
    @elseif($name == "print") { @return"/"; }
 
    // Row 4
    @elseif($name == "download") { @return"~"; }
    @elseif($name == "warning_black") { @return"!"; }
    @elseif($name == "locate") { @return"@"; }
    @elseif($name == "trash") { @return"#"; }
    @elseif($name == "cart") { @return"$"; }
    @elseif($name == "bank") { @return"%"; }
    @elseif($name == "flag") { @return"^"; }
    @elseif($name == "add") { @return"&"; }
    @elseif($name == "delete") { @return"*"; }
    @elseif($name == "lock") { @return"("; }
    @elseif($name == "unlock") { @return")"; }
    @elseif($name == "minus2") { @return"_"; }
    @elseif($name == "add2") { @return"+"; }
    @elseif($name == "up") { @return"{"; }
    @elseif($name == "down") { @return"}"; }
    @elseif($name == "screens") { @return"|"; }
    @elseif($name == "bell") { @return":"; }
    @elseif($name == "quote") { @return"\""; }
    @elseif($name == "volume_mute") { @return"<"; }     @elseif($name =="volume") { @return">"; }
    @elseif($name == "help") { @return"?"; }
 
    // Backwards compat; icons that are not in the font
    @elseif($name == "arrow_left") { @return"["; }
    @elseif($name == "arrow_right") { @return"]"; }
    @elseif($name == "arrow_up") { @return"{"; }
    @elseif($name == "arrow_down") { @return"}"; }
    @elseif($name == "organize") { @return"I"; }
    @elseif($name == "bookmarks") { @return"I"; }
    @elseif($name == "star") { @return"S"; }
    @elseif($name == "maps") { @return"@"; }
    @elseif($name == "reply") { @return"R"; }
 
    @else{
        // @debug "#icon: icon with name '#{$name}' not found.";
        @returnfalse;
    }
}

Using the Out of the Box Icons

In prior versions of Sencha Touch you would use the SASS pictos-iconmask() mixin to encode a png file and add appropriate style references into your generated CSS file. In Touch 2.2, however, you’ll need to use the new icon() mixin  that generates a style class referencing the new pictos webfont. The sourcecode for the new icon() mixin is listed below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * Includes an icon to be used on Button or Tab components. The $name is the name of the icon, or the character
 * of the font being used.
 *
 *     @include icon('attachment');
 *
 * @param {string} $name The name of the icon to be included.
 * @param {string} $font-family The `font-family` used for this icon. Defaults to the `Pictos` font.
 */
@mixin icon($name, $font-family:false) {
    .x-tab .x-button-icon.#{$name},
    .x-button .x-button-icon.#{$name} {
        $character: icon-character-for-name($name);
 
        &:before {
            @include absolute-position;
            text-align: center;
 
            @if$font-family {
                font-family: $font-family;
            }
 
            @if$character {
                content:"#{$character}";
            } @else{
                content:"#{$name}";
            }
        }
    }
}

Therefore, if you wanted to include a button that used the “network” icon, you would have to add the following to your .SCSS file:

1
@include icon('network');

Inside of your application’s source files, you would reference the icon name as an iconCls configuration property for a button component or tab as illustrated by the following snippet.

1
2
3
4
5
6
{
 xtype:'button',
 iconCls:'network',
 iconAlign:'top',
 text:'Net'
}

Note: The iconMask configuration property is no longer required on a button torender the icon.

==================================================================================================

source:http://bruno.tavares.me/sencha-touch/adding-custom-font-icons-to-sencha-touch-2-2/

Adding custom font icon to sencha touch 2.2

Adding Custom Font Icons to Sencha Touch 2.2

In Sencha Touch 2.2 instead of using icons with the -webkit-mask trick, Touch is moving to a more cross-browser solution with icon fonts. You can read more about this at Steve Drucker blog Changes to Sencha Touch 2.2 Theming Part 1: Using Iconography.

As Steve mentions on his blog, Touch SDK ships with the Pictos Font, and it already loads some basic icons for you to use directly on buttons with iconCls.

1
2
3
4
{
 xtype:'button',
 iconCls:'home'
}

If you want to define more icons from the same Pictos fonts, you just have to use the mixin icon():

1
@includeicon('network');
1
2
3
4
5
6
{
 xtype:'button',
 iconCls:'network',
 iconAlign:'top',
 text:'Net'
}

Touch Theming Layer already has a big object on SASS mapping keys into characters. So when you inform the key “network”, the theme knows that it has to use the key “J”.

Adding Custom Icons

To add more icons you’ll need to generate your own font! You might be question yourself – How do I create a new font? Sounds complicated but it’s fairly easy.

There’s a few tools, but my favorite is IcoMoon. With this tool you can select from dozens of fonts and create your own font package, or even import your own SVG vectors.

All you need to do is select your icons, click generate font, and download!

Select your icons…

..and download the font!

..and download the font!

After you have your font, it’s time to add to your project. Add the font into {touch_app}/resources/fonts/, and add this path to your config.rb:

1
2
3
4
# Compass configurations
sass_path = dir
css_path = File.join(dir,"..","css")
fonts_path = File.join(dir,"..","fonts")

Hint: Because we’re defining the fonts_path I had an error like “Pictos fonts not found”. It’s also good to copy the pictos folder from touch/resources/themes/fonts to this new fonts/ directory.

For the last step you just need to include the font and use the icons on your app.scss, like this:

1
2
3
4
5
6
7
8
9
@include icon-font('SenchaConIcons', inline-font-files(
  'senchacon-icons/senchacon-icons.woff', woff,
  'senchacon-icons/senchacon-icons.ttf', truetype,
  'senchacon-icons/senchacon-icons.svg', svg
));
 
@includeicon("menu" ,"\e009");
@includeicon("search","\e000");
@includeicon("star" ,"\e001");

Updated June/5: In 2.2.1 you have to specify your font along with icon() mixin. Otherwise it will fallback to ‘Pictos’

1
2
3
@includeicon("menu" ,"\e009","SenchaConIcons");
@includeicon("search","\e000","SenchaConIcons");
@includeicon("star" ,"\e001","SenchaConIcons");

Recap

  • Use a tool to pick icons and generate the font (IcoMoon, Fontello)
  • Add font into new directory {your_app}/resources/fonts
  • Copy Pictos font from Touch directory to the same {your_app}/resources/fonts to avoid errors
  • Add fonts_path into config.rb so Compass finds your font
  • Import the font icon in your app.scss

What about PNG?

PNG is bitmap based, while Fonts are vectorial based. The above mentioned tools provide hundreds of icons, so it’s pretty easy to find an existent font icon pre-built. If that’s not the case, you can convert PNG to SVG and then import into IcoMoon. There are tools for that, like VectorMagic, Online Image Converter or Inkscape. I haven’t try any of these but If you have, please let us know via comment.

- See more at: http://bruno.tavares.me/sencha-touch/adding-custom-font-icons-to-sencha-touch-2-2/#sthash.Dz643eCv.dpuf

0 0
原创粉丝点击