3

我有一个NSString这样的

NSString *string = @"textTextTextTextText<br>textTextTextText<br>TextTextText"

我想将此设置NSString为 my 的文本,并在字符串上找到的UICell每个标签上都有一个新行。
我怎么能那样做?

我试过这个,没有成功:

cell.textLabel.text = [[text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@"<br>"];

4

3 回答 3

13

怎么样:

string = [string stringByReplacingOccurrencesOfString: @"<br>" withString: @"\n"]

或者,如果您使用的是 SwiftStrings

var string = "textTextTextTextText<br>textTextTextText<br>TextTextText"
string = Array(string).reduce("") {$0 + ($1 == "<br>" ? "\n" : $1)}
于 2012-08-23T14:36:01.220 回答
2
NSString * result =
  [string stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
于 2012-08-23T14:35:21.800 回答
0

SWIFT 5: 如果您使用的是 Swift 字符串(您可以将 NSString 转换为字符串):

var string = "line1<br>line2<br>line3"
string = string..replacingOccurrences(of: "<br>", with: "\n"))
于 2020-08-28T09:04:33.457 回答