一个标签,两种不同的字体?
我需要在标签中设置文本格式,如下所示:
用户名:来自该用户的一些文本。这将
创建额外的文本行
一直不断。
其中“用户名”为粗体。这将进入 UILabel,它位于自定义表格单元格中。有没有办法获得这种类型的布局?
I need to format text in a label like this:
username: some text from this user. This will
create additional lines of text that will go
on and on and on.
Where "username" is bold. This will go into a UILabel, which is in a custom table cell. Is there a way to get this type of layout?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于这个相对简单的情况,您也许可以伪造它。在同一位置放置一个带有粗体用户名的标签,以及另一个带有纯文本的标签。在纯文本前插入足够的空格,为用户名留出空间。您可以使用 UIStringDrawing 方法来测量粗体文本和空格。
For this relatively simple case, you might be able to fake it. Have one label with the bold username, and another label with the plain text in the same position. Insert enough spaces before the plain text to leave room for the username. You can use UIStringDrawing methods to measure the bold text and the spaces.
如果您使用普通 UILabel,则它不可用。使用两个标签来完成此任务。
If you use plain UILabel it's not available. Use two labels for this task.
您需要使用 UIWebView 或 CoreText 来执行这种高级文本布局。 Web 视图有很多开销,但最灵活,您无法在 UITableView 单元格中有效地使用它。 CoreText 是低级别的并且没有很好的文档记录。您可以放弃表格视图,只在 Web 视图中使用 CSS 和 HTML 来布局表格,这就是我的做法。
You need to use either a UIWebView or CoreText to do this kind of advanced text layout. A web view has a lot of overhead but is most flexible and you can't use it effectively in a UITableView cell. CoreText is low level and not that well documented. You could ditch the table view and just lay out the table with CSS and HTML in the web view, which is how I do it.
您仍然可以使用 UITableViewCell,但让单元格使用 UIWebView 子视图。使用巧妙的 setter 方法设置自定义单元格子类,该方法允许您将 nsstring 发送到该方法,并将它们转换为漂亮的格式化视图。
You can still use a UITableViewCell but have the cell use a UIWebView subview. Set up a custom cell subclass with a clever setter method that allows you to send nsstrings to the method with turns those into a pretty formatted view.