Alo*_*lok 0 iphone cocoa-touch objective-c uilabel ios
这就是我正在使用的:
它适用于地址,城市,邮编.....长度> 0.(这些领域将来会增长)
self.addressInfoLbl.text = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@", address, city, zip, state, country];(numberofline == 0)
Run Code Online (Sandbox Code Playgroud)
但如果其中任何一个长度= 0,那么我得到了不必要的新行.我正在努力手动准备(附加\n).如果有越来越多的字段然后这样做manuallt真的很难.有没有其他正确的方法.我做得对.
谢谢
请尝试以下代码.它创建字符串数组,删除空字符串,然后将它们连接起来componentsJoinedByString:
NSArray *strings = @[address, city, zip, state, country];
strings = [strings filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]];
NSString *resultString = [strings componentsJoinedByString:@"\n"];
Run Code Online (Sandbox Code Playgroud)