如何在 UILabel 中的段落周围绘制边框? [英] How to draw a border around a paragraph in UILabel?

查看:118
本文介绍了如何在 UILabel 中的段落周围绘制边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用可以显示丰富内容的 UILabel.为此,我使用 UILabel 中的 attributedText 属性和一个 NSAttributedString 我用 NSHTMLTextDocumentType 分配.

I am attempting to use a UILabel that can show rich content. For this I use the attributedText property in UILabel and a NSAttributedString I alloc with NSHTMLTextDocumentType.

我想要实现的一种格式是在段落周围绘制边框(用于引号).字体、粗体、斜体等普通文本格式似乎工作正常,但是当我使用诸如边框之类的 CSS 属性时,它无法正常工作.看图;仅显示 background-color 属性.

One type of formatting I want to achieve is to draw a border around paragraphs (for quotes). Normal text formatting like font, bold, italic etc seems to work fine, but when I use CSS properties like border it does not work as I would expect. See picture; only the background-color property is shown.

这个 UILabel 的 attributedText 属性是:

The attributedText property of this UILabel is:

<style>
  body{font:10pt Verdana;} .quote {background-color:#ddd;border:1px solid #aaa;}
</style>
<body>
  <div class="quote">This is a quote</div>
  <br/>
  Bla bla bla
</body>

我期望的是 UILabel 内第一个句子/段落周围的边框 - 而不是整个 UILabel 周围的边框.

文本背景显示,但预期的边框没有.甚至有可能实现这一目标吗?我更喜欢使用 UILabel 来保持我的 UITableView 速度.

The text background shows, but the expected border does not. Is it even possible to achieve this? I'd prefer to use UILabel to keep my UITableView speedy.

推荐答案

我不知道在 SDK 中实现了这一点,即使您可以限制为 iOS 7.不过至少有两个通用选项.

I'm not aware of this being implemented in the SDK, even if you can restrict to iOS 7. There are at least two general options though.

UILabel 的子类中,您可以使用 NSAttributedString 的大小调整方法,例如 boundingRectWithSize:options:context:size,计算潜台词出现的位置,并覆盖 drawTextInRect: 画边框.您可以通过计算前面的文本和附加的潜台词的框架,然后取它们的差异来推导出所需的边框框架.

In a subclass of UILabel you can use the sizing methods of NSAttributedString, such as boundingRectWithSize:options:context: and size, to calculate where the subtext is to appear, and in an override of drawTextInRect: draw the border. You might deduce the required border frame by calculating the frame for the preceding text and for the subtext appended and then taking their difference.

另一种选择是在 NSAttributedStrings 上设置自定义属性,这是 Apple 公开鼓励的,来自 Apple 的概述:

Another option is to set custom attributes on your NSAttributedStrings, something Apple openly encourages, from Apple's overview:

您可以将您希望的任何属性名称/值对分配给一系列字符——由您的应用程序来解释自定义属性(请参阅属性字符串编程指南).

You can assign any attribute name/value pair you wish to a range of characters—it is up to your application to interpret custom attributes (see Attributed String Programming Guide).

然后在NSAttributedStrings的一个子类中,覆盖drawInRect: 并根据第一个建议实现类似的自定义绘图逻辑您的自定义属性,否则依赖于 super.

Then in a subclass of NSAttributedStrings, override the drawing methods such as drawInRect: and implement similar custom drawing logic per the first suggestion for your custom attribute, otherwise rely on super.

这篇关于如何在 UILabel 中的段落周围绘制边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆