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:@"\n"];

    }
    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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值