Good evening, tell me how you can get the height of the text in pixels, setting the desired width.

For example:

I have a STRING variable with contents: "The priest had a dog. He loved her. She ate a piece of meat. He killed her."

I need to create a TextView with a certain width, for example 800 wide, and I don’t know the height, because String variable can be 1000 words.

I need to calculate the height, to create a TextView, and place a variable in the text.

In Objective-C, I did it like this:

UIFont *cellFont = [UIFont systemFontOfSize:12]; CGSize constraintSize = CGSizeMake(self.view.frame.size.width-70, MAXFLOAT); CGSize labelSize = [STRING sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping]; return labelSize.height + 37.0f; 
  • Perhaps you just need to set the height of the content, i.e. wrap_content ? - Yuriy SPb
  • @YuriSPb, it could be. But I have below the buttons that I move through topMargin, so I definitely need to know the height of the text. - developer
  • In general, we don’t do this on android ... Maybe you should not just indent the margin, but put the buttons and test field in the LinearLayout vertical? So they will always be exactly below. Or in RelativeLayout and there assign them to be below the text - Yuriy SPb
  • @Yuriy SPb, now I did. But now I need to know the height of RelativeLayout, because besides the text I also have an image) Yes, and a lot of nuances. - developer
  • @YuriSPb look, I found this: stackoverflow.com/questions/5829703/… user response WhiteFang34, but I do not understand what a Font ?? - developer

0