Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

ios - Using custom font in a UIWebView

I would like to display a custom font inside a UIWebView. I have already put the font in the plist under "Fonts provided by application". The code in use:

        UIWebView *webView = [[UIWebView alloc] initWithFrame:myRect];
        NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
        [webView loadHTMLString:html baseURL:baseURL];
        [self addSubview:webView];

where html is an NSString that has the following contents:

<html><head>
<style type="text/css">
@font-face {
    font-family: gotham_symbol;
    src: local('GOTHAMboldSymbol_0.tff'), format('truetype') 
} 
body { 
 font-family: gotham_symbol;
font-size: 50pt;
}
</style>
</head><body leftmargin="0" topmargin="0">
This is <i>italic</i> and this is <b>bold</b> and this is some unicode: &#1101;
</body></html>

I'm using iOS 4.2 so TTF should be supported. I'd appreciate a bit of html/code that actually works.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After some Try and Error I have found a reliable way to load custom Fonts with a local CSS.

1. Add your Font to the App...make sure that the file is targeted properly to the Application

enter image description here enter image description here

2. Then add your Font to yourApp-Info.plist

enter image description here

3. Run NSLog(@"Available fonts: %@", [UIFont familyNames]); To check which name the font/fontfamily has for the System...

enter image description here

4. Copy that name and use them in your CSS...@font-face is not needed

body {
    font-family:"Liberation Serif";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...