Line break based on character found

I want to break an email in a UILabel into two lines by "@" , for example:

abc123
@gmail.com

If the word is inside a NSString *variable, how can I break it? Should I search the string by character until I found "@"?


You should make use of components(separatedBy separator: String) -> [String] function on String :

Swift :

let stringComponentsArray = yourString.components(separatedBy: "@")

Objective-C :

NSArray *stringComponentsArray = [yourStringObject componentsSeparatedByString:@"@"];

This will separate your string by a character @ into a String components array.

链接地址: http://www.djcxy.com/p/28234.html

上一篇: 在swift中将多行文本添加到UILabel中

下一篇: 根据找到的字符换行