in b4a we can easily calculate height of label by measure text height and StringUtils library like this :
StringUtils.MeasureMultilineTextHeight
but in B4i there is not such library or option for do this , so how can i load a long txt in label ( in scrollview ) ?
I must have lable height (depend on txt) to add other button and view bottom of this and make my layout
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;
}
Use Like This
CGFloat lblHeight = [self getLabelHeight:self.yourLable];
You can call Label.SizeToFit. You can also measure the width and height with String.MeasureWidth or String.MeasureHeight.
Though the best solution to show long texts is with CustomListView.AddTextItem.
Instead calculating UILabel
height calculate height of your Text itself and adjust your label accordingly.
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;
}
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
}
Use this code. Happy coding :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.