如何将标签的preferredMaxLayoutWidth设置为自动编程? [英] How to set a label's preferredMaxLayoutWidth to automatic programmatically?

查看:422
本文介绍了如何将标签的preferredMaxLayoutWidth设置为自动编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误,当我试图设置标签的 preferred宽度自动在故事板


  

属性不可用:自动preferred最大宽度的布局上没有的iOS版本之前的8.0


因为我需要我的布局在iOS 7和8这两个工作,我打算做到以下几点:


  1. 在故事板将值设置为明确

  2. 在搭载iOS 7,值编程设置为一个明确的,计算宽度。

  3. 在iOS 8的,将值设置为自动编程。

1和2是容易的。我怎么步骤3?有没有一个恒定的,我可以将它设置为?


这里是我试过到目前为止...

如果您将值设置为自动在故事板,你检查 preferredMaxLayoutWidth ,你会看到它是 0

但是,试图将其设置为0,即使它说,这已经是0,无法正常工作(例如标签保持为单行)。例如,我试过的值设置为自动的故事板,并在 viewDidLoad中,我跑了以下内容:

  self.label preferredMaxLayoutWidth = self.label preferredMaxLayoutWidth。;

当我不运行上面的code,标签大小正常。然而,当我运行上面的code(应该什么都不做),它保持为单线(不良行为)。


有关的UILabel头文件说:

  //用于基于约束的布局(自动布局)支持
//如果非零,确定-intrinsicContentSize多行标签时,这是用来
@属性(非原子)CGFloat的preferredMaxLayoutWidth NS_AVAILABLE_IOS(6_0);

至于恒定的,我无法找到任何东西。我能想到的我想要什么最接近的常数 UITableViewAutomaticDimension ,这是行不通的。

下面是连环画的样子....

自动布局宽度:

 <标签不透明=NOuserInteractionEnabled =NOcontentMode =左horizo​​ntalHuggingPriority =251verticalHuggingPriority =1000文本=富lineBreakMode =tailTruncation numberOfLines =0baselineAdjustment =alignBaselinesadjustsFontSizeToFit =否translatesAutoresizingMaskIntoConstraints =否ID =双 -  IG-G4L>
    < RECT键=帧X =20Y =116WIDTH =560HEIGHT =21/>
    <了fontDescription键=了fontDescriptionTYPE =系统的pointsize =17/>
    <彩色钥匙=文字颜色cocoaTouchSystemColor =darkTextColor/>
    <无钥匙=highlightedColor/>
< /标签>

显式布局宽度:

 <标签不透明=NOuserInteractionEnabled =NOcontentMode =左horizo​​ntalHuggingPriority =251verticalHuggingPriority =1000文本=富lineBreakMode =tailTruncation numberOfLines =0baselineAdjustment =alignBaselinesadjustsFontSizeToFit =否preferredMaxLayoutWidth =560translatesAutoresizingMaskIntoConstraints =否ID =双 -  IG-G4L>
    < RECT键=帧X =20Y =116WIDTH =560HEIGHT =21/>
    <了fontDescription键=了fontDescriptionTYPE =系统的pointsize =17/>
    <彩色钥匙=文字颜色cocoaTouchSystemColor =darkTextColor/>
    <无钥匙=highlightedColor/>
< /标签>

唯一的区别是,后者具有:

  preferredMaxLayoutWidth =560


解决方案

我还没有想出一个办法以编程方式做到这一点,但有一个pretty体面的解决办法。

任何有效的解决办法必须满足以下要求:


  • 使用自动<一href=\"https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILabel_Class/index.html#//apple_ref/occ/instp/UILabel/$p$pferredMaxLayoutWidth\"相对=nofollow> preferredMaxLayoutWidth 在iOS 8,并明确在iOS 7。

  • 不能有警告。

办法设置 preferredMaxLayoutWidth 自动通过故事板,所以我们这样做。

为了共进晚餐preSS上述警告,设置的行数为1 即可。

现在,编程,做到以下几点:

  self.label.numberOfLines = 0;
如果功能(iOS8){
    ; //什么都不做,它会通过故事板使用自动
}其他{
    self.label preferredMaxLayoutWidth =屏幕宽度 - 32。
}

有关参考,在这里是如何找出iOS版本,在这里是如何得到的屏幕宽度。请注意,您必须特意去找的iOS 8,而不是对象是否响应 preferredMaxLayoutWidth ,因为在iOS 6中添加的属性。

I get the following errors when I attempt to set a label's Preferred Width to Automatic in a storyboard:

Attribute Unavailable: Automatic Preferred Max Layout Width is not available on iOS versions prior to 8.0

Since I need my layout to work on both iOS 7 and 8, I was planning to do the following:

  1. Set the value to Explicit in the storyboard.
  2. On iOS 7, set the value to an explicit, computed width programatically.
  3. On iOS 8, set the value to automatic programatically.

1 and 2 are easy. How do I do step 3? Is there a constant I can set it to?


Here's what I have tried so far...

If you set the value to automatic on a storyboard and you inspect preferredMaxLayoutWidth, you will see that it is 0.

However, attempting to set it to 0, even if it says it is already 0, doesn't work properly (e.g. the label stays as a single line). For example, I tried setting the value to automatic in the storyboard, and on viewDidLoad, I ran the following:

self.label.preferredMaxLayoutWidth = self.label.preferredMaxLayoutWidth;

When I don't run the above code, the label is sized properly. However, when I run the above code (which should do nothing), it stays as a single line (undesirable behavior).


The header file for UILabel says:

// Support for constraint-based layout (auto layout)
// If nonzero, this is used when determining -intrinsicContentSize for multiline labels
@property(nonatomic) CGFloat preferredMaxLayoutWidth NS_AVAILABLE_IOS(6_0);

As far as a constant, I couldn't find anything. The closest constant I can think of to what I want is UITableViewAutomaticDimension, which doesn't work.

Here is what the storyboard looks like....

Automatic layout width:

<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="1000" text="Foo" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Bis-iG-g4l">
    <rect key="frame" x="20" y="116" width="560" height="21"/>
    <fontDescription key="fontDescription" type="system" pointSize="17"/>
    <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
    <nil key="highlightedColor"/>
</label>

Explicit layout width:

<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="1000" text="Foo" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="560" translatesAutoresizingMaskIntoConstraints="NO" id="Bis-iG-g4l">
    <rect key="frame" x="20" y="116" width="560" height="21"/>
    <fontDescription key="fontDescription" type="system" pointSize="17"/>
    <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
    <nil key="highlightedColor"/>
</label>

The only difference is that the latter has:

preferredMaxLayoutWidth="560"

解决方案

I haven't figured out a way to do this programmatically, but there is a pretty decent workaround.

Any valid workaround must meet these requirements:

The only way to set preferredMaxLayoutWidth to automatic is via a storyboard, so we do so.

In order to suppress the above warning, set the number of Lines to 1.

Now, programmatically, do the following:

self.label.numberOfLines = 0;
if (iOS8) {
    ; // do nothing, it will use automatic via the storyboard
} else {
    self.label.preferredMaxLayoutWidth = screenWidth - 32;
}

For reference, here is how to find out the iOS version, and here is how to get the screen width. Note that you must specifically look for iOS 8 and not whether or not the object responds to preferredMaxLayoutWidth, since that property was added in iOS 6.

这篇关于如何将标签的preferredMaxLayoutWidth设置为自动编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆