问题
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