题
我要表明,我从服务器获取丰富的文字,所以我用UIWebView的。现在的问题是,我有过这是在UIWebView中使用的字体无法控制。如何更改使用的字体系统字体(使它与应用程序的其余部分一致)?
我在做这样的事情在此刻:
myRichTextView = [[UIWebView alloc] initWithFrame:CGRectMake(x, y, width, height)];
myRichTextView.backgroundColor = [UIColor clearColor];
[myRichTextView setScalesPageToFit:NO];
[myRichTextView loadHTMLString:myString baseURL:[NSURL URLWithString:myBaseURL]];
再次需要控制显示字体。如果我不能控制字体,请让我知道其他办法,以UIWebView的。
由于
解决方案
在HTML文档中参考的CSS样式表或把样式信息到HTML文档本身的顶部
其他提示
它工作时,我修改这样的字符串。您的权利克里斯,它不应该有任何区别。
NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-family: \"%@\"; font-size: %@;}\n"
"</style> \n"
"</head> \n"
"<body>%@</body> \n"
"</html>", @"helvetica", [NSNumber numberWithInt:kFieldFontSize], content];
我用下面的CSS在网页视图做你在说什么。
body
{
font-family:helvetica;
}
但你看起来像它应该正常工作。奇怪。唯一的区别我可以看到的是字体VS字体家庭,但不应的有所作为。
克里斯。
系统字体
<强> - 苹果系统强>在iOS 9的Safari OS X 10
font-family: -apple-system;
font-family: '-apple-system','HelveticaNeue'; // iOS 8 compatible
我提出,包装在一个HTML主体一些文本与正确的样式的方法给定UIColor
和UIFont
。它是基于穆斯塔法答案。
它用于这样的:
NSString * htmlString = [MyClass htmlFromBodyString:@"an <b>example</b>"
textFont:[UIFont systemFontOfSize:10]
textColor:[UIColor grayColor]];
[webView loadHTMLString:htmlString baseURL:nil];
另一种简单的方法为我工作
UIFont *font = [UIFont systemFontOfSize:15];
NSString *htmlString = [NSString stringWithFormat:@"<span style=\"font-family:Helvetica; font-size: %i\">%@</span>",
(int) font.pointSize,
[bodyText stringWithNewLinesAsBRs]];
[myWebView loadHTMLString:htmlString baseURL:nil];
只是改变
font:helvetica
到
font-family:helvetica
我用
body {
font-family: 'Noteworthy-Bold';
}
这工作正常,我对页面(的WebView),作为我的应用程序的其他部分也采用了苹果的“值得关注的魄力。”
如果你想使用自己的字体,使用@字体面,并指TTF或EOT文件。
下面是我的易于使用和易于扩展与更多的字体设置的解决方案:
myHTMLLabel = [[UIWebView alloc] initWithFrame:CGRectMake(myX,myY,myWidth,myHeight)];
myHTMLLabel.userInteractionEnabled=NO;
myHTMLLabel.scalesPageToFit=NO;
[myHTMLLabel setOpaque:NO];
myHTMLLabel.backgroundColor = myBackColor;
NSString *myHTMLText = [NSString stringWithFormat:@"<html>"
"<head><style type='text/css'>"
".main_text {"
" display: block;"
" font-family:[fontName];"
" text-decoration:none;"
" font-size:[fontSize]px;"
" color:[fontColor];"
" line-height: [fontSize]px;"
" font-weight:normal;"
" text-align:[textAlign];"
"}"
"</style></head>"
"<body> <SPAN class='main_text'>[text]</SPAN></body></html>"];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[text]" withString: myText];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[fontName]" withString: myFontName];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[fontSize]" withString: myFontSize];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[fontColor]" withString: myFontColorHex];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[textAlign]" withString: myFontAlign];
NSLog(@"*** renderHTMLText --> myHTMLText: %@",myHTMLText);
[myHTMLLabel loadHTMLString:myHTMLText baseURL:nil];
不隶属于 StackOverflow