Uso de fuentes personalizadas en un UIWebView


Me gustaría mostrar una fuente personalizada dentro de un UIWebView. Ya he puesto la fuente en el plist bajo "Fuentes proporcionadas por la aplicación". El código en uso:

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

Donde html es un NSString que tiene el siguiente contenido:

<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>

Estoy usando iOS 4.2 por lo que TTF debe ser compatible. Apreciaría un poco de html / código que realmente funciona.

Author: Tshepang, 2012-05-08

2 answers

Después de algunos Intentos y errores, he encontrado una forma confiable de cargar fuentes personalizadas con un CSS local.

1. Añade tu fuente a la Aplicación...asegúrese de que el archivo está dirigido correctamente a la Aplicación

introduzca la descripción de la imagen aquíintroduzca la descripción de la imagen aquí

2. A continuación, añade tu Fuente a yourApp-Info.plist

introduzca la descripción de la imagen aquí

3. Ejecute NSLog(@"Available fonts: %@", [UIFont familyNames]); Para comprobar qué nombre tiene la fuente/familia de fuentes para el Sistema...

introduzca la descripción de la imagen aquí

4. Copia ese nombre y úsalos en tu CSS...@font-face no es necesario

body {
    font-family:"Liberation Serif";
}
 97
Author: Lindemann,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-11 11:04:20

Terminé haciendo que funcionara aunque no estoy seguro de lo que hice mal anteriormente. Aquí está el HTML que uso (NSString * htmll). He añadido todo, algunos de ellos podría no ser relevante para la solución.

<html><head><style type="text/css">
@font-face {
font-family: 'gotham_symbol';
src: url('GOTHAMboldSymbols_0.ttf')  format('truetype') 
}
@font-face {
font-family: 'gotham_symbol_italic';
src: url('GothamBoldItalic.ttf')  format('truetype') 
}
#w {display:table;}
#c {display:table-cell; vertical-align:middle;}
i { font-family: 'Helvetica-BoldOblique'; }
</style></head><body topmargin="0" leftmargin="0">
<div style="display: table; width: 320px; height: 50px; #position: relative; overflow: hidden;">
<div style=" #position: absolute; #top: 50%;display: table-cell; vertical-align: middle;">
<div style=" #position: relative; #top: -50%">
<p style="font-size:20px;font-family:'gotham_symbol';color:#97371f;">THE TEXT GOES HERE</p></div></div></div></body></html>

Y cargo el UIWebView de la siguiente manera:

UIWebView *webView = [[UIWebView alloc] initWithFrame:rect];
[webView makeTransparent];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL:baseURL];
 12
Author: Joris Weimar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-02 05:47:18