ios - 处理 iOS 中的字体格式

标签 ios

在Android下,我习惯了Html.fromHtml方法,以便我可以在翻译中使用伪 html,例如 <string name="xxx">&lt;b>%s&lt;/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/

相关文章:

ios - shouldAutorotateToInterfaceOrientation 没有被调用

ios - 在 iOS 10 中安排和处理本地通知

iphone - TableView 不会显示提供的数据

ios - SCNetworkReachabilityGetFlags 导致崩溃?

iphone - 如何在 Xcode 4.5 中将内容连接到不同页面。 iOS 6

iphone - 为什么 MPMoviePlayerController 只在 iPhone 中显示这些消息?

iphone - 当 UILocalNotification 触发时,自动将应用程序从后台转到前台?

iphone - 在 iOS 应用程序中显示服务器端内容 - 需要的方法

ios - 错误 EXC 错误指令试图从另一个类获取当前位置?

objective-c - 将 UITextField 置于 UITableViewCell 顶部