iOS如何计算标签的高度?

时间:2016-03-11 11:54:10

标签: ios iphone basic4android

b4a中的

我们可以通过测量文本高度和StringUtils库轻松计算标签的高度,如下所示:

StringUtils.MeasureMultilineTextHeight

但在B4i中没有这样的库或选项,所以如何在标签中加载长txt(在scrollview中)?

我必须有标签高度(取决于txt)添加其他按钮并查看底部并制作我的布局

3 个答案:

答案 0 :(得分:3)

试试这个

- (CGFloat)getLabelHeight:(UILabel*)label
{
    CGSize constraint = CGSizeMake(label.frame.size.width, CGFLOAT_MAX);
    CGSize size;

    NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
    CGSize boundingBox = [label.text boundingRectWithSize:constraint
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:label.font}
                                                  context:context].size;

    size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));

    return size.height;
}

像这样使用

CGFloat lblHeight = [self getLabelHeight:self.yourLable];

答案 1 :(得分:0)

您可以调用Label.SizeToFit。您还可以使用String.MeasureWidth或String.MeasureHeight测量宽度和高度。

虽然显示长文本的最佳解决方案是使用CustomListView.AddTextItem。

答案 2 :(得分:0)

而是计算UILabel高度计算文本本身的高度并相应地调整标签。

<强> Objectiv-C

// *** I have created a dynamic label and calculated its height ***
UILabel *lblDynamicHeight = [[UILabel alloc] init];
[lblDynamicHeight setText:@"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."];
[lblDynamicHeight setFrame:CGRectMake(0, 0, 200, [self textHeight:lblDynamicHeight.text])];
[lblDynamicHeight setFont:[UIFont fontWithName:@"Arial" size:15]];
[self.view addSubview:lblDynamicHeight];

- (CGFloat)textHeight:(NSString *)text
{
    CGFloat maxWidth = 200; // set Max width for your control here. (i have used 200)
    CGRect rect = [text boundingRectWithSize:CGSizeMake(maxWidth, CGFLOAT_MAX)
                                                          options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                                       attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:15]}   // Set your label's font here
                                                          context:nil];
    CGFloat textHeight = rect.size.height;
    return textHeight;
}

<强>夫特

func addLabel() {
    // *** I have created a dynamic label and calculated its height ***
    var lblDynamicHeight: UILabel = UILabel()
    lblDynamicHeight.text = "Lorem Ipsum."
    lblDynamicHeight.frame = CGRectMake(0, 0, 200, self.textHeight(lblDynamicHeight.text!))
    lblDynamicHeight.font = UIFont(name: "Arial", size: 15)
    self.view!.addSubview(lblDynamicHeight)
}

func textHeight(text: String) -> CGFloat {
    var maxWidth: CGFloat = 200
    // set Max width for your control here. (i have used 200)
    var rect: CGRect = text.boundingRectWithSize(CGSizeMake(maxWidth, CGFLOAT_MAX), options: ([.UsesLineFragmentOrigin, .UsesFontLeading]), attributes: [NSFontAttributeName: UIFont(name: "Arial", size: 15)], context: nil)
    var textHeight: CGFloat = rect.size.height
    return textHeight
}

使用此代码。快乐的编码:)