简体   繁体   中英

UILabel: Intelligent line breaking (dynamically)

I am currently confronted with the following problem:

In my app I have dynamically changing UILabel s containing all sorts of strings. Long, short, one word, etc. The app does not control which strings it display, it just recieves them, resizes to label to fit into the screen properly according to the maximum possible font size. This all works fine except for the line breaking done by the SDK itself. It simply breaks a line when the next word cannot fit because of the UILabel s width. This sometimes causes not so nice line breaks causing a single word to be rendered into the last line. I would like to avoid that.

Are there already any methods/frameworks out there to help out with this or do I have to write my own algorithm for scanning over the string an determining where to insert a \\n ?

尝试使用UITextView代替UILabel的(如果可以的话),它具有该功能。

You could do this, if you want to use a string variable with some predefined text,

var textFieldData:String = "John"
myTextField.text = NSString(format: "Hello User, \n %@",textFieldData) as String
myTextField.numberOfLines = 0!

在此处输入图片说明

It appears this way because I added some formatting as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM