问题描述
我已经创建了一个使用NSAttributedString的方法,并且我正在寻找动态创建子视图和标签以将该字符串放入其中的方法.因为需要确定诸如字体和大小之类的属性才能正确确定标签的大小,所以我需要确定是否可以遍历应用于属性字符串的值和范围?
I've created a method that takes a NSAttributedString and I'm looking to dynamically create a subview and label to put the string into. Because attributes like font and size need to be determined to correctly determine the size of the label, I need to determine if it is possible to iterate through values and ranges that have been applied to the attributed string?
我知道我可以分别传递属性,但是为了可重用性,我希望能够将尽可能少的参数传递给方法.
I understand that I could pass the attributes separately, but for sake of reusability, i'd like to be able to pass as few parameters to the method as possible.
推荐答案
Apple希望您使用 enumerateAttributesInRange:options:usingBlock:
.您提供的块将接收范围和适用于该范围的属性.
Apple expects you to use enumerateAttributesInRange:options:usingBlock:
. The block you supply will receive ranges and the attributes applicable for that range.
我已在代码中使用它来创建不可见的按钮,这些按钮位于文本后面,以便充当超链接.
I've used that in my code to create invisible buttons that are placed behind text so that it acts as a hyperlink.
您还可以使用 enumerateAttribute:inRange:options:usingBlock:
如果您只有一个感兴趣的地方,但没有提供您可能感兴趣的中途度假屋,例如,有两个属性,但不是每个属性.
You could also use enumerateAttribute:inRange:options:usingBlock:
if there's only one you're interested in, but no halfway house is provided where you might be interested in, say, two attributes but not every attribute.
这篇关于是否可以获取NSMutableAttributedString的属性和范围的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!