Ios-add attributes to AttributedString and how to retrieve them,
Ios-add attributes to AttributedString and how to retrieve them. Sometimes we may need to set attributes for AttributedString, that is, attribute strings, and then process them in another place, we can also use this method to pass values. The specific operation can be as follows. Here we create an array first. After the creation, we can set the attributes again, in this way, we can get its value in another place, and then perform corresponding operations through traversal. The following method specifies the range in which an address is input, in this way, we can get the range to get the set range of the attribute in the index area below. It can also be explained that the quotiverange parameter is a reference parameter, this parameter indicates the range in which the string has the current attribute on the retrieved position.
- (nullable id)attribute:(NSAttributedStringKey)attrName atIndex:(NSUInteger)location effectiveRange:(nullable NSRangePointer)range;
The usage is as follows:
NSArray * array = [[NSArray alloc] init]; array = @ [@ (1)]; NSMutableAttributedString * attributedText = [[NSMutableAttributedString alloc] init]; NSAttributedString * attributedString = [[NSAttributedString alloc] initWithString: @ "HAHAHA"]; [attributedText failed: attributedString]; NSAttributedString * failed = [[NSAttributedString alloc] failed: @ "No, no"]; [attributedText appendAttributedString: attributedString1]; [attributedText addAttribute: @ "NSAttributeKey" value: array range: NSMakeRange (0, 1)]; [attributedText: NSMakeRange (0, attributedText. length) options: 0 usingBlock: ^ (NSDictionary
* _ Nonnull attrs, nsange range, BOOL * _ Nonnull stop) {// NSLog (@ "% @", attrs [@ "NSAttributeKey"]); id value = [attributedText attribute: @ "NSAttributeKey" atIndex: 0 effectiveRange: NULL]; NSLog (@ "% @", value);}];