UILabel自動換行及高度自適應

#pragma mark -- UILabel 自動換行 高度自適應app


    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 200, 50)];字體

    label.text = @"      今天下午全市多雲到陰有陣雨或雷雨由於愛情來得不容易";spa

    //清空背景顏色3d

    label.backgroundColor = [UIColor clearColor];orm

    //設置字體顏色爲白色it

    label.textColor = [UIColor whiteColor];io

    //設置label的背景色爲黑色自適應

    label.backgroundColor = [UIColor cyanColor];im

    //文字居中顯示margin

    label.textAlignment = NSTextAlignmentLeft;

    //自動折行設置

    label.lineBreakMode = NSLineBreakByWordWrapping;

    label.numberOfLines = 0;

    //自適應高度

    CGRect txtFrame = label.frame;

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:label.font,NSFontAttributeName, nil];

    CGSize size = CGSizeMake(txtFrame.size.width, CGFLOAT_MAX);

    txtFrame.size.height =[label.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading

                                                attributes:attributes context:nil].size.height;

    label.frame = CGRectMake(10, 100, 200, txtFrame.size.height);

    [self.view addSubview:label];

相關文章
相關標籤/搜索