2016-03-11 7 views
0

в b4a мы можем легко вычислить высоту ярлыком высоты текста измерения и библиотеки StringUtils так:iOS как вычислить высоту метки?

StringUtils.MeasureMultilineTextHeight 

но в B4i нет такой библиотеки или вариант для этого, так как я могу загрузить длинный TXT в метке (в scrollview)?

я должен иметь Контрактное высоту (зависит от TXT), чтобы добавить другую кнопку и вид снизу этого и сделать мой макет

+4

Что такое b4a? никто не может понять, что вы пытаетесь сказать ... –

+1

@ Zhi-WeiCai может быть, он говорит об этом https://www.b4x.com/b4a.html –

+0

ohhh i'm so sorry b4a означает basic4Android и B4i is Basic4Ios Я не знаю, почему для них нет тегов (я нашел тег b4a как basic4android) – Arman

ответ

0

Вы можете позвонить Label.SizeToFit. Вы также можете измерять ширину и высоту с помощью String.MeasureWidth или String.MeasureHeight.

Хотя наилучшим решением для отображения длинных текстов является CustomListView.AddTextItem.

3

Try This

- (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]; 
+0

Большое спасибо. Вы спасли мой день;) –

+0

Это была копия. По крайней мере, укажите источник. https://stackoverflow.com/a/27374760/1935299 – Shinnyx

0

Вместо расчета UILabel высота высота рассчитайте самого текста и настроить ярлык соответственно.

-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; 
} 

Swift

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 
} 

Используйте этот код. Счастливое кодирование :)