ios 根据字符串中的逗号分行显示

//自动计算高宽

  • (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
    {
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    }
  • (void)viewDidLoad {
    [super viewDidLoad];
    NSString *strname = @"123456,000000,333333,223344";

pragma mark 判断是否包含逗号

if([strname rangeOfString:@","].location !=NSNotFound){

pragma mark 替换逗号用换行

    strname=[strname stringByReplacingOccurrencesOfString:@"," withString:@"
"];
}
UILabel *name = [[UILabel alloc]init];
name.text = strname;
name.numberOfLines = 0;
CGSize size = [self sizeWithText:name.text font:[UIFont systemFontOfSize:13] maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
name.backgroundColor=[UIColor redColor];
name.font=[UIFont systemFontOfSize:13];
name.frame=CGRectMake(30,200,size.width,size.height);
[self.view addSubview:name];

}

原文地址:https://www.cnblogs.com/shao621/p/5278706.html