2012-05-07 29 views
48

Me gustaría mostrar una fuente personalizada dentro de UIWebView. Ya coloqué la fuente en el plist debajo de "Fuentes provistas por la aplicación". El código en uso:Uso de fuente personalizada en UIWebView

 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 con el fin de TTF debe ser apoyada. Apreciaría un poco de html/code que realmente funciona.

+1

'GOTHAMboldSymbol_0.tff' - extensión debe ser' ttf' (a menos que la fuente realmente tenga una extensión 'tff' en el disco). –

+0

Estoy tratando de encontrar una solución a esto también, pero: creo que quiere pasar 'nil' para baseURL. – MusiGenesis

+0

@Joris Lucho con el mismo problema en este momento y sería interesante escuchar si ha encontrado una solución. – Lindemann

Respuesta

93

Después de un cierto intento y error que he encontrado una manera confiable para cargar fuentes personalizadas con CSS local.

1. Añadir la fuente a la App ... asegurarse de que el archivo está dirigido correctamente a la aplicación

enter image description here enter image description here

2. Publique sus fuentes de yourapp-Info.plist

enter image description here

3. Ejecutar NSLog(@"Available fonts: %@", [UIFont familyNames]); para comprobar qué nombre de la fuente/fontfamily tiene para el Syste m ...

enter image description here

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

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

Perdón por haber incluido ese paso. De alguna manera había asumido que ya habías hecho ese paso ... –

+0

¡Gracias! ¿Está esto documentado en algún lugar? – ozz

+4

El paso 1-3 es básicamente la forma estándar de agregar fuentes personalizadas a una aplicación ... así que sí, está documentado. Y en el último paso usa la forma estándar de llamar a una fuente que está disponible en un sistema ... :-) – Lindemann

10

Terminé haciéndolo funcionar aunque no estoy seguro de lo que hice mal anteriormente. Aquí está el HTML que uso (NSString * htmll). Agregué todo, algunos de ellos podrían no ser relevantes 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 cargar 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]; 
+0

Gracias Joris! <3 – Lindemann

Cuestiones relacionadas