Solution to the problem of automatic line feed of lebel special characters in IOS



Today, I was asked by a colleague at work, if I encounter a special string during label auto line feed, I don’t want to separate the special string line feed, how can I realize it???

Question picture

When I first saw this problem, I thought:

1. I want to convert the event string into a picture. I use nstextattachment combined with nsattributedstring to display the picture on the label, but Baidu will convert the text into a picture. After a look at such a long code, there are too many replies on the list. Considering the performance requirements, I gave up decisively.

2. Calculate the frame of the last character of the reply content, and then calculate the length and sum of time to judge whether to wrap the line. I think it’s OK, but seeing so many calculations, Xiaobian gave up. (Xiao Bian is lazy. I can’t help it.)

I really can’t remember how to realize it. If I can’t, I’ll try the implementation method in step 2. Just at this time, my sister called, chatted with her for half an hour, hung up the phone and couldn’t help it. Think about it, write it, and finally let me think of a simple method

The final result

We can first calculate the width and height a of the reply content, and then calculate the width and height B of the time. Let’s compare the height of a and B. if it’s different, it’s just a line break at the time, so we can add a line break between the content and time


Can’t remember, and sister chat chat, always remember the time, otherwise more waste of youth time!!!

Demo is as follows

let label = UILabel();
 Var text = "seven seconds memory fish note, forward please note seven seconds note, MEDA! ";
 label.numberOfLines = 0;
 label.font = UIFont.systemFont(ofSize: 15);
 let size = text.wj_computedTextHeight(font: 15, size: CGSize.init(width: view.bounds.width, height: CGFloat(MAXFLOAT)));
 var text1 = text;
 text.append (seven seconds is the most handsome);
 let size1 = text.wj_computedTextHeight(font: 15, size: CGSize.init(width: view.bounds.width, height: CGFloat(MAXFLOAT)));
 if size.height != size1.height {
 Text1. Append ("seven seconds is the most handsome");
 text = text1;
 label.text = text;
 label.frame = CGRect.init(x: 10, y: 64, width: size1.width, height: size1.height);

Methods of data extraction

///Calculate the size of the text
///- parameter font: size
///- parameter size: type text
///- Returns: returns size
func wj_computedTextHeight(font:CGFloat,size:CGSize)->CGSize{
 let str = self as NSString;
 let maxSize = str.boundingRect(with: size, options:NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: font)], context: nil).size

 return maxSize;

The final effect

The final effect


The above is the whole content of this article, I hope the content of this article has a certain reference value for your study or work, if you have any questions, you can leave a message to exchange, thank you for your support for developer.