【问题标题】:Make UIWebview's font bold in iOS在 iOS 中使 UIWebview 的字体加粗
【发布时间】:2015-10-29 14:35:51
【问题描述】:

如何将字体设置为粗体

[cell.itemContentWebView loadHTMLString:[NSString stringWithFormat:@"<div style='font-family:-apple-system','HelveticaNeue-Bold; style='font-size:24px;'>%@",object.title] baseURL:nil];

【问题讨论】:

  • 这是一个 HTML 问题,不是 iOS/Objective-C 问题。

标签: html ios objective-c uiwebview


【解决方案1】:

Rafay 回答你的问题

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   //Font size
   int fontSize = 80;
   NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", fontSize];
  [webview stringByEvaluatingJavaScriptFromString:jsString];
  //Font Family Style - Bold
   NSString *jsWebViewFontFamilyString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.fontFamily = 'HelveticaNeue-Bold'"];
  [webview stringByEvaluatingJavaScriptFromString:jsWebViewFontFamilyString];
  //Font Color or web view text color
  [webview stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.color = 'Green'"]; 

}

【讨论】:

  • 感谢它的工作.. 现在我该如何为其添加自定义颜色。
  • 我更新了我的答案,请检查一下。请投票给我的答案。这对其他人非常有帮助。
  • 奇怪的是,更改字体大小不再适用于 iOS 10.1.1
【解决方案2】:

在加载到 webView 之前,只需为您的字符串添加一个标签。

NSString *body = [plist objectForKey:@"foo"];
NSString *htmlString = 
    [NSString stringWithFormat:@"<font face='GothamRounded-Bold' size='3'>%@", body];
[webView loadHTMLString:htmlString baseURL:nil];

【讨论】:

  • &lt;font&gt; 标签已被弃用多年,早在 2015 年之前。
猜你喜欢
  • 1970-01-01
  • 2023-03-11
  • 2017-01-20
  • 1970-01-01
  • 2019-01-13
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
  • 2021-08-06
相关资源
最近更新 更多