如何以编程方式检查 UILabel 是否具有属性文本或普通文本? [英] How to check if UILabel has attributedText or normal text programmatically?

查看:46
本文介绍了如何以编程方式检查 UILabel 是否具有属性文本或普通文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用 label.attributedTextlabel.text 属性来判断 UILabel 是否设置了文本?

Is there any way to tell if UILabel has its text set using label.attributedText or label.text property?

问题是当您设置attributedText时,text也会更新,反之亦然,因此无法检查这些属性是否为零.

The problem is when you set attributedText, text is also updated and vice versa, so it is not possible to check these properties for nil.

推荐答案

这是我用的.如果范围长度等于无属性文本长度,则文本只有一个属性,因此是无属性的.

This is what I use. If range length equals the unattributed text length then the text only has a single attribute and is therefore unattributed.

NSRange range;
[label.attributedText attributesAtIndex:0 effectiveRange:&range];
BOOL isAttributed = label.text.length==range.length;

这篇关于如何以编程方式检查 UILabel 是否具有属性文本或普通文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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