在Android下,我习惯了Html.fromHtml
方法,以便我可以在翻译中使用伪 html,例如 <string name="xxx"><b>%s</b> @%s</string>
将用户名格式设置为她的公司,并将用户名显示为粗体。
如何在 iOS 下做同样的事情?
我需要连接多个 NSAttributedString
在代码中手动?
最佳答案
我不太熟悉 Android,但您可以在 iOS 上执行以下操作。
假设你有这样一个字符串:
NSString *myString = @"<html>
<meta charset="UTF-8">
<head>
<style type="text/css">
body { font: 12pt 'Avenir'; color: #300c78; }
i { color: #4223ae; }
</style>
</head>
<body>Check-in: <i>2018-07-29 13:16:28</i><br>Duration: <i>4 Minute, 1 Second</i><br>Venue (Near) <i>-----</i></body>
</html>";
然后你可以用一个 UILabel 来做到这一点
myLabel.attributedText =
[[NSAttributedString alloc]
initWithData: [myString dataUsingEncoding:NSUTF8StringEncoding]
options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
documentAttributes: nil
error: &err];
您将获得一个格式良好且能正确显示的属性字符串。
关于ios - 处理 iOS 中的字体格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51591060/