

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>網頁 &#8211; MaxCodeReview</title>
	<atom:link href="https://codereview.max-everyday.com/category/web/feed/" rel="self" type="application/rss+xml" />
	<link>https://codereview.max-everyday.com</link>
	<description>分享Max收集的小程式</description>
	<lastBuildDate>Tue, 18 Aug 2020 12:17:05 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://codereview.max-everyday.com/wp-content/uploads/2020/07/cropped-logo1_x512-32x32.jpg</url>
	<title>網頁 &#8211; MaxCodeReview</title>
	<link>https://codereview.max-everyday.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>如何製作「字體預覽」的網頁功能</title>
		<link>https://codereview.max-everyday.com/webfont-preview-textarea/</link>
		
		<dc:creator><![CDATA[max-code-review-usr1]]></dc:creator>
		<pubDate>Sun, 02 Aug 2020 19:41:52 +0000</pubDate>
				<category><![CDATA[字體]]></category>
		<category><![CDATA[網頁]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[Font]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://codereview.max-everyday.com/?p=1253</guid>

					<description><![CDATA[為什麼要學這個？ 使用者在還沒有下載該字體， [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">為什麼要學這個？</h2>



<ul class="wp-block-list"><li>使用者在還沒有下載該字體，就可以直接線上在網頁裡輸入他想看的文字套用新字體後的效果。</li><li>行動裝置變多，如果想讓你的網頁裡的文字，看起來不是標準的字體，應該要學。</li></ul>



<hr class="wp-block-separator"/>



<h2 class="wp-block-heading">實作步驟</h2>



<p>1：把字體先轉換成 .woff 格式，幸運的話，字體檔案大小會縮小到6成左右。</p>



<p>2：把檔案放到網路上可以被公開存取的空間。<br>（例如自行架設的網站或放在github上）</p>



<p>3：在自己的網頁裡，符用這段 HTML:</p>



<p>&lt;style&gt;<br>@font-face {<br>font-family: SweiB2LegCJKtc-Regular;<br>src: url(https://github.com/max32002/swei-b2-leg/raw/master/WebFont/CJK%20TC/SweiB2LegCJKtc-Regular.woff2) format(&#8220;woff2&#8221;), url(https://github.com/max32002/swei-b2-leg/raw/master/WebFont/CJK%20TC/SweiB2LegCJKtc-Regular.woff) format(&#8220;woff&#8221;);<br>}<br>.sweib2leg_textarea {<br>display: block;<br>width: calc(100% &#8211; 46px);<br>height: 96px;<br>margin: 10px;<br>padding: 12px;<br>background: #fff;<br>border:1px solid #333;<br>border-radius: 2px;<br>box-shadow: inset 3px 3px 3px rgba(0,0,0,0.25);<br>-moz-box-shadow: inset 3px 3px 3px rgba(0,0,0,0.25);<br>-webkit-box-shadow: inset 3px 3px 3px rgba(0,0,0,0.25);<br>font-size: 42px;<br>overflow: auto;<br>resize: none;<br>outline: none;<br>}<br>&lt;/style&gt;<br>&lt;script&gt;<br>function sweib2leg_size_input_change(cnt){<br>var size_val = Number(document.getElementById(&#8220;sweib2leg_size_input_&#8221; + cnt).value);<br>document.getElementById(&#8220;sweib2leg_textarea_&#8221; + cnt).style.fontSize = size_val + &#8220;px&#8221;;<br>}<br>&lt;/script&gt;</p>



<p>&lt;p&gt;測試效果：字體大小&lt;input type=&#8221;number&#8221; id=&#8221;sweib2leg_size_input_0&#8243; value=&#8221;42&#8243; style=&#8221;width: 80px; text-align: right;&#8221; onchange=&#8221;sweib2leg_size_input_change(0);&#8221; onkeyup=&#8221;sweib2leg_size_input_change(0);&#8221;&gt;px&lt;/p&gt;</p>



<p>&lt;textarea class=&#8221;sweib2leg_textarea&#8221; id=&#8221;sweib2leg_textarea_0&#8243; style=&#8221;font-family: &#8216;SweiB2LegCJKtc-Regular'&#8221;&gt;可以輸入文字測試效果&lt;/textarea&gt;</p>



<p>附註：上面在github 上 webfont resource url 無法直接使用，會有CORS（Cross-Origin Resource Sharing）問題，解法參考：<br><a href="https://stackoverflow.max-everyday.com/2020/08/cors/">https://stackoverflow.max-everyday.com/2020/08/cors/</a></p>



<hr class="wp-block-separator"/>



<p>執行畫面：</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1402" height="1094" src="https://codereview.max-everyday.com/wp-content/uploads/2020/08/Screen-Shot-2020-08-03-at-03.38.57.png?v=1596397230" alt="" class="wp-image-1254" srcset="https://codereview.max-everyday.com/wp-content/uploads/2020/08/Screen-Shot-2020-08-03-at-03.38.57.png?v=1596397230 1402w, https://codereview.max-everyday.com/wp-content/uploads/2020/08/Screen-Shot-2020-08-03-at-03.38.57-830x648.png?v=1596397230 830w, https://codereview.max-everyday.com/wp-content/uploads/2020/08/Screen-Shot-2020-08-03-at-03.38.57-230x179.png?v=1596397230 230w, https://codereview.max-everyday.com/wp-content/uploads/2020/08/Screen-Shot-2020-08-03-at-03.38.57-350x273.png?v=1596397230 350w, https://codereview.max-everyday.com/wp-content/uploads/2020/08/Screen-Shot-2020-08-03-at-03.38.57-480x375.png?v=1596397230 480w" sizes="(max-width: 1402px) 100vw, 1402px" /></figure>



<p>展示成品用網址：<br><a href="https://max-everyday.com/2020/07/swei-b2-leg/">https://max-everyday.com/2020/07/swei-b2-leg/</a></p>



<p>測試你的webfont 是否能被載入的線上服務：<br>Did that font really get loaded?<br><a href="http://www.atomicjetpacks.com/project/fontdetect/how-to-detect-if-a-font-really-loaded">http://www.atomicjetpacks.com/project/fontdetect/how-to-detect-if-a-font-really-loaded</a></p>



<hr class="wp-block-separator"/>



<h2 class="wp-block-heading">CSS 網頁字型</h2>



<p>@font-face 這個 CSS 的 at-rule 可以讓網頁設計者可以使用網路上的字型檔來顯示網頁中的文字，@font-face 的基本使用方式：</p>



<pre class="wp-block-preformatted">@font-face {
font-family: YourFontName;
src: url(http://your-font-url/font-name.woff) format("woff");
}</pre>



<p>有了上面的 style 後，就可以使用 font-family 屬性來指派我的網頁元件套用網路字型。</p>



<p>Css 寫法範例2號：</p>



<pre class="wp-block-code"><code>@font-face {
    font-family: "MyWebFont";
    src: url("webfont.woff2") format("woff2"), /* Super Modern Browsers */
    url("webfont.woff") format("woff"), /* Pretty Modern Browsers */
    url("webfont.ttf")  format("truetype") /* Safari, Android, iOS */
    font-weight: normal;
    font-style: normal;
}</code></pre>



<p>Later on in the stylesheet:</p>



<pre class="wp-block-code"><code>body {
    font-family: "MyWebFont";
}</code></pre>



<hr class="wp-block-separator"/>



<p>當 woff2 上 Github 後，可透過以下方式從 CDN 中引入，<br>可大幅加快網頁載入。jsDelivr 在速度上挺不錯的：&nbsp;<a href="http://www.cdnperf.com/#!performance,Asia" rel="noreferrer noopener" target="_blank">www.cdnperf.com/#!performance,Asia</a>&nbsp;。</p>



<p>@font-face{<br>　font-family: naikai;<br>　src: url(&nbsp;<a href="https://cdn.jsdelivr.net/gh/max32002/naikaifont@770d2de358eb3eab35b4918a910cb6b06ff8e15a/webfont/NaikaiFont-Regular.woff2" rel="noreferrer noopener" target="_blank">https://cdn.jsdelivr.net/gh/max32002/naikaifont@770d2de358eb3eab35b4918a910cb6b06ff8e15a/webfont/NaikaiFont-Regular.woff2</a><br>) format(&#8220;woff2&#8221;);<br>}</p>



<hr class="wp-block-separator"/>



<p>MaxCodeReview 影片：<br><a href="https://youtu.be/-LfZ2ka6IYI">https://youtu.be/-LfZ2ka6IYI</a></p>



<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="如何製作「字體預覽」的網頁功能" width="1170" height="658" src="https://www.youtube.com/embed/-LfZ2ka6IYI?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<hr class="wp-block-separator"/>



<h2 class="wp-block-heading">相關網頁：</h2>



<ul class="wp-block-list"><li>「字體」與「FontForge專案」互轉<br><a href="https://codereview.max-everyday.com/font-to-fontforge/">https://codereview.max-everyday.com/font-to-fontforge/</a></li><li>@font-face 使用方法<br><a href="https://developer.mozilla.org/zh-TW/docs/Web/CSS/@font-face">https://developer.mozilla.org/zh-TW/docs/Web/CSS/@font-face</a></li><li>CSS 網頁字型 @font-face 使用教學與範例<br><a href="https://blog.gtwang.org/web-development/css-font-face/">https://blog.gtwang.org/web-development/css-font-face/</a></li></ul>



<p></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
