How to check if a UILabel is a Text attribute or normal text programmatically?

Is it possible to indicate whether UILabel has its own text using the label.attributedText or label.text ?

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

+8
source share
5 answers

This is what I use. If the length of the range is equal to the length of the unallocated text, then the text has only one attribute and therefore has no attributes.

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

Inspired by @ lukas-o I wrote an extension on UILabel that determines whether it contains attributedText or not. In fact, if the NSAttributedString does not contain any attributes, this computed property will evaluate it so that it is not assigned.

 extension UILabel { var isAttributed: Bool { guard let attributedText = attributedText else { return false } let range = NSMakeRange(0, attributedText.length) var allAttributes = [Dictionary<String, Any>]() attributedText.enumerateAttributes(in: range, options: []) { attributes, _, _ in allAttributes.append(attributes) } return allAttributes.count > 1 } } 
+3
source

From apple documents :

By default, this property is zero. Assigning a new value to this property also replaces the value of the text property with the same string data, although without any formatting information. In addition, assigning a new value updates the values ​​in the font, textColor, and other properties associated with the style, so that they reflect style information starting at location 0 in the attribute string.

You are right, it is impossible to find to verify that one or the other is for nil. One way to find out that the text is attributed is to use something like:

 NSMutableArray *strAttrs = [NSMutableArray new]; NSMutableArray *strRanges = [NSMutableArray new]; [label.attributedText enumerateAttributesInRange:NSMakeRange(0, label.attributedText.length) options:0 usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { [strAttrs addObject:attrs]; [strRanges addObject:[NSValue valueWithRange:(range)]]; }]; 

This way you can see if more than one attribute exists. You can also compare attributes to match your standard attributes and assume that the text property was set only in this case.

+2
source

Here is an implementation that I came up with a slightly modified logic. This is Swift hosted @Vallette port with additional defensive statements.

The function will return true only if attributedText not zero, is not empty, and has at least one attribute that does not apply in the entire range of text:

 extension UILabel { var isPartiallyAttributed: Bool { guard let attributedText = attributedText else { return false } guard !attributedText.string.isEmpty else { return false } var range = NSRange() attributedText.attributes(at: 0, effectiveRange: &range) return attributedText.string.count != range.length } } 
0
source

I did it with the storyboard.

1) Click on the label, and then in the right window, set the text for the attribute. 2) Then you select the text and change its font or text color. enter image description here

-1
source

Source: https://habr.com/ru/post/984666/


All Articles