为纵向制作的故事板不适合横向

时间:2014-06-24 07:08:22

标签: ios uitableview orientation

我有一个需要同时支持横向和纵向方向的iPad应用。我以纵向方式构建了我的故事板并相应地设置了约束。除了桌子视图外,一切正常。在此表视图中,我有自定义单元格具有以下约束:

enter image description here

我填充单元格的代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellIdentifier = @"glossaryCell";
    GlossaryTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    ListItem* glossaryItem = [[self.glossaries objectForKey:[self.glossaryTitles objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

    [cell setLayoutWithGlossaryItem:glossaryItem withTableView:tableView];

    return cell;
}

在GlossaryTableViewCell中:

-(void)setLayoutWithGlossaryItem:(ListItem*)glossaryItem withTableView:(UITableView*)tableView
{
    NSString* name = [NSString stringWithFormat:@"<p><b>%@</b> :</p>", glossaryItem.name];

    NSString* content = glossaryItem.details[0];

    NSMutableAttributedString* nameWithStyle = [HTMLParser parseText:name withFontSize:16 withFontType:@"Light"];

    NSMutableAttributedString* contentWithStyle = [HTMLParser parseText:content withFontSize:16 withFontType:@"Light"];

    [nameWithStyle addAttribute:NSForegroundColorAttributeName value:[Colors blueColor] range:NSMakeRange(0, nameWithStyle.length)];

    [self.glossaryName setAttributedText:nameWithStyle];
    [self.glossaryContent setAttributedText:contentWithStyle];
}

关于上述代码的一些解释:我有包含HTML标记的文本,因此我需要将它们解析为NSAttributedString以放入UILabel&#39}。以下是解析文本的代码:

@implementation HTMLParser

+(NSMutableAttributedString*)parseText:(NSString*)text withFontSize:(NSInteger)size withFontType:(NSString*)type{
    NSDictionary* optionsDictionary = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};

    NSString* font = [NSString stringWithFormat:@"HelveticaNeue%@", type == nil ? @"" : [NSString stringWithFormat:@"-%@",[type capitalizedString] ]];
    NSString* stringToAppend = [NSString stringWithFormat:@"<style> p, li{font-family:'%@';font-size:%d;margin:0px;}</style>", font, size];

    NSString* itemWithFont = [NSString stringWithFormat:@"%@%@", stringToAppend, text];
    NSMutableAttributedString* itemWithStyle = [[NSMutableAttributedString alloc] initWithData:[itemWithFont dataUsingEncoding:NSUTF8StringEncoding] options:optionsDictionary documentAttributes:nil error:nil];

    return itemWithStyle;
}

@end

heightForRowAtIndexPath:功能正常工作并正确计算每行的高度,但标签并不适合它们。例如;标签&#39;高度的计算方式就好像它们处于横向时的纵向方向。这导致丑陋的布局和文字不适合标签&#39; RECT&#39; S

这是我的heightForRowAtIndexPath:方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat tableWidth = [tableView bounds].size.width;
    ListItem* glossaryItem = [[self.glossaries objectForKey:[self.glossaryTitles objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

    NSString* name = [NSString stringWithFormat:@"<p><b>%@ :</b></p>", glossaryItem.name];
    NSString* content = glossaryItem.details[0];
    NSMutableAttributedString* nameWithStyle = [HTMLParser parseText:name withFontSize:16 withFontType:@"Light"];
    NSAttributedString* contentWithStyle = [HTMLParser parseText:content withFontSize:16 withFontType:@"Light"];

    CGRect nameRect = [nameWithStyle boundingRectWithSize:CGSizeMake(tableWidth - 40, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:NULL];

    CGRect contentRect = [contentWithStyle boundingRectWithSize:CGSizeMake(tableWidth - 40, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:NULL];
    CGFloat height = nameRect.size.height + contentRect.size.height + 40;

    return height;
}

计算+ 40时的height是垂直约束的总长度。

任何帮助或建议都将受到赞赏。

0 个答案:

没有答案