• iOS--UILable自适应大小


    #import "ViewController.h"

    @interface ViewController ()

    @property(strong,nonatomic) UILabel *lable;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        [self.lable setNumberOfLines:0];

        self.lable.text=@"最近一年多,尽管交易数据靓丽,但经过一年操作后,老王发现炒房还是没有利润空间。他终于明白,房价已失去大涨的土壤环境,主要是房源库存量大,传统企业生存艰难,年轻人购房欲望减弱,资产配置转向资本市场,“炒房已经到了末日”[2006年至2011年,温州市区商品房销售均价从8045元/平方米一路上涨至34674元/平方米,5年时间涨了3倍左右。老王说,温州房价每平方米从1万元涨到2万元,花了一年多点时间;从2万元涨到3万元,只用了一年不到。][2009年,温州人均GDP仅4604美元,在浙江省排倒数第三,不到杭州的一半,也只有浙江省人均GDP的71%。]";

        self.lable.backgroundColor=[UIColor grayColor];

        // 设置段落风格

        NSMutableParagraphStyle *par=[[NSMutableParagraphStyle alloc]init];

        par.lineHeightMultiple=NSLineBreakByCharWrapping;

        // 设置段落字体风格

        NSDictionary *dic=@{NSFontAttributeName:self.lable.font,NSParagraphStyleAttributeName:par.copy};

        // 计算 lable 的实际大小  返回文本绘制的区域

        CGRect rect=[self.lable.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

        // 设置标签到屏幕顶端的距离

        self.lable.frame=CGRectMake(0, 70, rect.size.width, rect.size.height);

        [self.view addSubview:self.lable];

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    @end

  • 相关阅读:
    python类的特殊成员和方法
    python 之 staticmethod,classmethod,property的区别
    启动Android模拟器问题集锦
    eclipse编译Jmeter源码
    解决 'chromedriver' executable needs to be in PATH.'报错
    登录程序优化
    通过标志位跳出多层循环
    crontab计划任务监控nginx服务器
    httpstatus类的状态有哪些
    python正则表达式
  • 原文地址:https://www.cnblogs.com/bolin-123/p/5433013.html
Copyright © 2020-2023  润新知