是否可以获取NSMutableAttributedString的属性和范围的列表? [英] Is it possible to get a listing of attributes and ranges for an NSMutableAttributedString?

查看:119
本文介绍了是否可以获取NSMutableAttributedString的属性和范围的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个使用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希望您使用

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.

您还可以使用

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的属性和范围的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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