Ios-add attributes to AttributedString and how to retrieve them,

Source: Internet
Author: User

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);}];
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.