IOS IM development suggestion (2) Calculation of TableViewCell Height: text-and-image mixing, iostableviewcell

Source: Internet
Author: User

IOS IM development suggestion (2) Calculation of TableViewCell Height: text-and-image mixing, iostableviewcell

Computing Cell height has always been a hot issue. After the emergence of a large number of IM apps, this problem becomes more frequent. Here I will talk about the NSAttributedString height calculation method, which is pure code.

First, the common text sizetofit is good, so there is no difficulty. What about text and text mixing? Generally, CoreText is used, but once you use it, you will know who uses it. IOS 7 started with Apple providing TextKit to process text-and-text multiplexing. This method is simple and intuitive.

TextKit implements text-and-text Mixing

I will post the code first, and then explain it slowly.

Step 1: splice string

1 /*! 2 * Insert emoticon 3*4 * @ param rangeArray location of the emoticon to be inserted. Each element includes (loc) 5 * @ param facesArray information of the emoticon to be inserted. Each model includes (id imageName) 6 * @ param pStr original string 7 * @ param sourceArray table package 8*9 * @ return concatenated string 10 */11 + (NSMutableAttributedString *) setupEmojiByRangeArray :( NSArray *) rangeArray facesArray :( NSArray *) facesArray plainStr :( NSAttributedString *) pStr sourceArray :( NSArray *) sourceArray {12 bytes * mutStr = [pStr mutableCopy]; 13 _ block int offset = 0; // after an expression is added each time, the string will become longer. This variable will record the added length, otherwise the inserted position will be misplaced by 14 if (rangeArray. count = facesArray. count) {// check whether the number of locations to be inserted is the same as the number of emotices to be inserted. 15 [facesArray enumerateObjectsUsingBlock: ^ (id obj, NSUInteger idx, BOOL * stop) {// array enumeration 16 for (FaceModel * fm in sourceArray) {// This is not in the table package 17 if (fm. id = [obj [@ "ID"] intValue]) {
18 UIImage * image1 = [UIImage imageNamed: fm. imageName]; 19 NSTextAttachment * attachment1 = [[NSTextAttachment alloc] init]; // This is the object to be inserted 20 attachment1.bounds = CGRectMake (0,-3, 20, 20 ); // The size of the inserted expression 21 attachment1.image = image1; 22 NSAttributedString * attachStr1 = [NSAttributedString attributedStringWithAttachment: attachment1]; // convert the object to be inserted into string23 NSDictionary * dic = [rangeArray objectAtIndex: idx]; // you can see which location is 24 [mutStr insertAttributedString: attachStr1 atIndex: offset + [dic [@ "loc"] intValue]; 25 offset + = (int) attachStr1.length; // calculate the offset 26} 27} 28} after the insertion ends. 29} 30 // Add link 31 // NSURL * url = [NSURL URLWithString: @ "http://www.baidu.com"]; 32 // [mutStr addAttribute: NSLinkAttributeName value: url range: NSMakeRange (loc, length)]; 33 // mainText. attributedText = [mutStr copy]; 34 35 return mutts; // return the processed string 36}

Taking a closer look at the above code, I inserted an emoticon. This is a necessary requirement for IM. With the use of NSTextAttachment, We can insert the image of the emoticon. Because this is batch processing, I insert expressions with the same specifications. Likewise, we can insert an image... Yes, it's an image. This is a mix of text and text.

 

Step 2: Calculate NSMutableAttributedString Rect

1 // font size 2 UIFont * font = [UIFont systemFontOfSize: 14];
3 // section settings 4 NSMutableParagraphStyle * p = [[NSMutableParagraphStyle alloc] init]; 5 p. lineBreakMode = NSLineBreakByCharWrapping; 6 p. lineSpacing = 0.1f; 7 p. paragraphSpacing = 0.1f; 8 p. alignment = align; 9 // set the NSAttributedString style 10 NSAttributedString * str = [[NSAttributedString alloc] initWithString: rString attributes :@{ NSFontAttributeName: font, direction: p}]; 11 // calculate Rect12 // w represents the width h you want represents the maximum supported height of your string 13 CGRect rect = [sstr boundingRectWithSize: CGSizeMake (w, h) options: NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin context: nil];

Based on the calculated rect, we know how to set the height of the cell: Put the string in a textView and put the textvView on the cell. Everything is OK...

 

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.