【问题标题】:boundingRectWithSize is wrong in HebrewboundingRectWithSize 在希伯来语中是错误的
【发布时间】:2017-06-01 21:59:08
【问题描述】:

我建立的这种方法在英语中非常有效。 现在我也在尝试支持希伯来语,而boundingRectWithSize 错过了高度(给我的东西太短了)

不知道为什么……

+ (void) setTextAndFitLabel:(UILabel *)label text:(NSString *)text
{
    [label setNumberOfLines:0];
    text = [text stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    [label setText:text];
    NSDictionary *attributes = @{NSFontAttributeName: label.font};
    CGRect rect = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width, CGFLOAT_MAX )
                                           options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                        attributes:attributes
                                           context:nil];
    if ([text isEqualToString:@""])
    {
        rect = CGRectMake(0,0,0,0);
    }
    CGRect labelFrame = label.frame;
    labelFrame.size.height = ceil(rect.size.height);

    NSArray *constraints = [label constraints];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d", NSLayoutAttributeHeight];
    NSArray *filteredArray = [constraints filteredArrayUsingPredicate:predicate];
    if(filteredArray.count == 0){
        label.frame = labelFrame;
    }
    else
    {
        NSLayoutConstraint *heightConstraint =  [filteredArray objectAtIndex:0];
        heightConstraint.constant = ceil(rect.size.height);
    }
}

【问题讨论】:

  • 嗨,波阿兹!你想通了吗?这让我疯了啊
  • 3 年过去了...不记得了...抱歉...
  • 无论如何,谢谢,我决定将我的设计更改为固定大小的字体。大声笑

标签: ios nsstring hebrew


【解决方案1】:

-boundingRectWithSize 仅计算打印文本所需的空间量。
当您询问 UI/Label/TextView/Textfield 的大小时,您应该考虑到它们是具有特定属性的视图,例如 UITexView 在打印文本之前包含一些插图。
在分配文本调用-sizeToFit-sizeThatFits: 之后,请求其帧。
我不知道你为什么需要它,但值得一提的是,UILabel 固有内容大小是根据完全显示分配的文本计算的。

【讨论】:

  • 不幸的是,当你使用自动布局时,你需要这个。但是留下那个 - 你没有回答我的问题 - 为什么boundingRectWithSize 在相同的字体上返回一个好的英文高度和一个不好的希伯来文高度......
  • 这不是真的,我在 iOS7 中有自动调整大小的表格视图单元格,它使用 siteToFit 来获取标签的高度,并更新约束使 TVC 高度动态化。我想我确实回答了你的问题,说恕我直言,你没有使用正确的方法。 doing-it-wrong.mikeweller.com/2012/07/…
猜你喜欢
  • 2014-10-04
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多