How to make this UILabel move the email address to the next Line?

痞子三分冷 提交于 2020-01-06 14:51:31

问题


I have a simple UILabel that is printing text as follows:

John Doe sent a message to blahblah-
blah@blahblah.com

Thus, the email address here : blahblah-blah@blahblah.com is being cut and partially displayed on the next line. What do I need to do to make it display as :

John Doe sent a message to 
blahblah-blah@blahblah.com    

?

I already have the following :

self.mailLabel.lineBreakMode = NSLineBreakModeByWordWrapping;
self.mailLabel.numberOfLines = 0;

The string in question is :

[NSString stringWithFormat:@"You sent a message to %@", emailAddress];

What should I do here? Remember, the name can be long and hence, adding a line break in the string won't work because I don't want the following case :

Jonathon Dawson sent a message
to 
blahblah-blah@blahblah.com   

which should be

Jonathon Dawson sent a message
to blahblah-blah@blahblah.com   

回答1:


You need to add ZWNBSP(Zero-width no-break space) characters to the left and to the right of - (hyphen, aka dash) symbol:

self.mailLabel.text = [self.mailLabel.text stringByReplacingOccurrencesOfString:@"-" withString:@"\u2060-\u2060"];

Remarks:

U+2060 - Zero-width no-break space Unicode character prevents any line breaks that may be inserter to wrap the word before and after it. As soon as UILabel (as well as other text controls in Cocoa) respects Unicode, you will get text without undesired line breaks.

Wikipedia link: http://en.wikipedia.org/wiki/Zero-width_no-break_space




回答2:


self.mailLabel = [NSString StringWithFormat:@"You sent a message to\n%@"];

You use \n to make a new line. Also, make sure you set the numberOfLines to 2 on the label.



来源:https://stackoverflow.com/questions/25393904/how-to-make-this-uilabel-move-the-email-address-to-the-next-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!