Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 设置自动布局约束以对齐多行标签_Ios_Autolayout - Fatal编程技术网

Ios 设置自动布局约束以对齐多行标签

Ios 设置自动布局约束以对齐多行标签,ios,autolayout,Ios,Autolayout,我不熟悉iOS和自动布局。 在我的一个表视图中,我的单元格包含两个标签(比如标题和副标题) 标题可以是相当长的,所以我希望它扩展到2行,如果需要的话。 字幕总是有一行 我制作了一些屏幕来显示问题(我附加了链接,因为我没有足够的信誉点) 让我们从第一张照片开始。 我希望标签之间有恒定的8px空间,并且(问题从这里开始)我需要它们垂直居中(title和superview.top之间的空间应等于subtitle和superview.bottom之间的空间) 我能做到这一点,但只有当标题标签有一行。

我不熟悉iOS和自动布局。 在我的一个表视图中,我的单元格包含两个标签(比如标题和副标题)

标题可以是相当长的,所以我希望它扩展到2行,如果需要的话。 字幕总是有一行

我制作了一些屏幕来显示问题(我附加了链接,因为我没有足够的信誉点)

让我们从第一张照片开始。 我希望标签之间有恒定的8px空间,并且(问题从这里开始)我需要它们垂直居中(title和superview.top之间的空间应等于subtitle和superview.bottom之间的空间)

我能做到这一点,但只有当标题标签有一行。 要让它看起来像照片2,需要什么约束条件? 此时此刻,我像这样钉住了它们:

8px between title and subtitle (varticaly) - priority 1000

16px between title and superview.top - priority 750

16px between subtitle and superview.bottom - priority 750
但它不起作用

另外,我已将行数设置为0


感谢您的帮助。

UITableViewCellStyleSubtitle
内置了对自我调整大小的支持

它将支持图像、多行标题和副标题,而无需编写或维护任何自定义代码来处理它已经可以做的事情

更新:

下面是一个使用属性文本代替字幕的4个不同“标签”的示例:

在tableView:cellForIndexPath中:

cell.detailTextLabel.attributedText = [self attributedTextForBookSources:sourcesValue];
助手方法:

/**
 The attributed text string corresponding to the referenced gospel books

 @param sourcesValue An integer number representing a bitfield of gospel books that have source details for a pericope

 @return The attributed string identifying the gospel book sources
 */
- (NSAttributedString *)attributedTextForBookSources:(NSInteger)sourcesValue
{
    UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];

    UIColor *textcolor = [self textColorForPericopeSource:sourcesValue & CGIPericopeSourcesMatt];
    NSMutableAttributedString *books = [[NSMutableAttributedString alloc] initWithString:@"Matt " attributes:@{NSFontAttributeName : font, NSForegroundColorAttributeName : textcolor}];

    textcolor = [self textColorForPericopeSource:sourcesValue & CGIPericopeSourcesMark];
    [books appendAttributedString:[[NSAttributedString alloc] initWithString:@" Mark " attributes:@{NSFontAttributeName : font, NSForegroundColorAttributeName : textcolor}]];

    textcolor = [self textColorForPericopeSource:sourcesValue & CGIPericopeSourcesLuke];
    [books appendAttributedString:[[NSAttributedString alloc] initWithString:@" Luke " attributes:@{NSFontAttributeName : font, NSForegroundColorAttributeName : textcolor}]];

    textcolor = [self textColorForPericopeSource:sourcesValue & CGIPericopeSourcesJohn];
    [books appendAttributedString:[[NSAttributedString alloc] initWithString:@" John" attributes:@{NSFontAttributeName : font, NSForegroundColorAttributeName : textcolor}]];

    return [[NSAttributedString alloc] initWithAttributedString:books];
}

- (UIColor *)textColorForPericopeSource:(BOOL)included
{
    return included ? [UIColor darkGrayColor] : [UIColor clearColor];
}
我的代码只是隐藏或显示一本书,但您也可以使用基于四个标签的属性字符串

无论何时,只要您可以使用内置样式,这通常意味着您编写、维护和支持的代码就会减少,并且在将来的iOS版本中仍然可以使用的可能性也会增加

该应用程序的单元格自行调整大小,您可以根据需要处理多行标题


UITableViewCellStyleSubtitle
内置了对自我调整大小的支持

它将支持图像、多行标题和副标题,而无需编写或维护任何自定义代码来处理它已经可以做的事情

更新:

下面是一个使用属性文本代替字幕的4个不同“标签”的示例:

在tableView:cellForIndexPath中:

cell.detailTextLabel.attributedText = [self attributedTextForBookSources:sourcesValue];
助手方法:

/**
 The attributed text string corresponding to the referenced gospel books

 @param sourcesValue An integer number representing a bitfield of gospel books that have source details for a pericope

 @return The attributed string identifying the gospel book sources
 */
- (NSAttributedString *)attributedTextForBookSources:(NSInteger)sourcesValue
{
    UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];

    UIColor *textcolor = [self textColorForPericopeSource:sourcesValue & CGIPericopeSourcesMatt];
    NSMutableAttributedString *books = [[NSMutableAttributedString alloc] initWithString:@"Matt " attributes:@{NSFontAttributeName : font, NSForegroundColorAttributeName : textcolor}];

    textcolor = [self textColorForPericopeSource:sourcesValue & CGIPericopeSourcesMark];
    [books appendAttributedString:[[NSAttributedString alloc] initWithString:@" Mark " attributes:@{NSFontAttributeName : font, NSForegroundColorAttributeName : textcolor}]];

    textcolor = [self textColorForPericopeSource:sourcesValue & CGIPericopeSourcesLuke];
    [books appendAttributedString:[[NSAttributedString alloc] initWithString:@" Luke " attributes:@{NSFontAttributeName : font, NSForegroundColorAttributeName : textcolor}]];

    textcolor = [self textColorForPericopeSource:sourcesValue & CGIPericopeSourcesJohn];
    [books appendAttributedString:[[NSAttributedString alloc] initWithString:@" John" attributes:@{NSFontAttributeName : font, NSForegroundColorAttributeName : textcolor}]];

    return [[NSAttributedString alloc] initWithAttributedString:books];
}

- (UIColor *)textColorForPericopeSource:(BOOL)included
{
    return included ? [UIColor darkGrayColor] : [UIColor clearColor];
}
我的代码只是隐藏或显示一本书,但您也可以使用基于四个标签的属性字符串

无论何时,只要您可以使用内置样式,这通常意味着您编写、维护和支持的代码就会减少,并且在将来的iOS版本中仍然可以使用的可能性也会增加

该应用程序的单元格自行调整大小,您可以根据需要处理多行标题


好的,我简化了我的问题,因为我不知道这很重要。实际上,我的底部标签由4个分开的标签组成。然后呢?内置标签支持属性文本。如果这仍然不能满足您的需求,请参阅。它将引导您了解自调整大小的所有细节,他的GitHub存储库为您可能遇到的任何问题提供了更多答案。好的,我将我的问题简化了一点,因为我不知道这很重要。实际上,我的底部标签由4个分开的标签组成。然后呢?内置标签支持属性文本。如果这仍然不能满足您的需求,请参阅。它将引导您了解自我调整的所有细节,他的GitHub存储库为您可能遇到的任何问题提供了更多答案。另外,苹果正在iOS 9中引入
UIStackView
。它将消除您当前需要添加的许多自动布局约束。根据您在评论中提供的信息,您可以使用嵌套堆栈视图。外部(垂直)堆叠将处理标题和(水平堆叠)副标题;内部(水平)堆栈将处理4个字幕标签。另外,苹果正在iOS 9中引入
UIStackView
。它将消除您当前需要添加的许多自动布局约束。根据您在评论中提供的信息,您可以使用嵌套堆栈视图。外部(垂直)堆叠将处理标题和(水平堆叠)副标题;内部(水平)堆栈将处理4个字幕标签。