zoukankan      html  css  js  c++  java
  • Label 自适应文本(StoryBoard/xib)

    To make your label automatically resize height you need to do following:

    1. Set layout constrains for label
    2. Set height constraint with low priority. It should be lower than ContentCompressionResistancePriority
    3. Set numberOfLines = 0
    4. Set ContentHuggingPriority higher than label's height priority
    5. Set preferredMaxLayoutWidth for label. That value is used by label to calculate its height

    For example:

    self.descriptionLabel = [[UILabel alloc] init];
    self.descriptionLabel.numberOfLines = 0;
    self.descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
    self.descriptionLabel.preferredMaxLayoutWidth = 200;
    
    [self.descriptionLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self.descriptionLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self.descriptionLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self addSubview:self.descriptionLabel];
    
    NSArray* constrs = [NSLayoutConstraint constraintsWithVisualFormat:@"|-8-[descriptionLabel_]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(descriptionLabel_)];
    [self addConstraints:constrs];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[descriptionLabel_]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(descriptionLabel_)]];
    [self.descriptionLabel addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[descriptionLabel_(220@300)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(descriptionLabel_)]];
    

    Using Interface Builder

    1. Set up four constraints. The height constraint is mandatory. enter image description here

    2. Then go to the label's attributes inspector and set number of lines to 0. enter image description here

    3. Go to the label's size inspector and increase vertical ContentHuggingPriority and vertical ContentCompressionResistancePriority.
      enter image description here

    4. Select and edit height constraint.
      enter image description here

    5. And decrease height constraint priority.
      enter image description here

    Enjoy. :)

  • 相关阅读:
    JS实现动态生成表格并提交表格数据向后端 表格中数据转为json
    JS 添加和删除HTML 标签
    操作系统杂碎
    bootstrap3级下拉菜单 及 CSS实现三级下拉菜单分析
    jquery获取复选框被选中的值
    Mac 命令行启动并连接Redis
    抓包工具不抓包的话记得看看还有没有没关的代理
    Mac OS Sierra 安装PHP扩展 Operation not permitted
    导航栏对于UIScrollview以及子类所做的一些事
    探索static的用处
  • 原文地址:https://www.cnblogs.com/W-Kr/p/5202405.html
Copyright © 2011-2022 走看看