0

I have a label in which content is loaded dynamically. I have tried almost all methods available in Stack overflow but i am not getting correct result.

Here is my code which i use right now, but it gives height less than the needed height.

var attributes = [UIFont(): UIFont.systemFontOfSize(13.0)]
var attrString:NSAttributedString? = NSAttributedString(string:content, attributes: attributes)
var newSize:CGRect = attrString!.boundingRectWithSize(CGSizeMake(myLabel.frame.width,CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin , context: nil )

content: text input myLabel: input label newSize: final size

  • If you are hardcoding the font size, why do you need the height to change dynamically? Can't you just hardcode that too? – TurtleFan Sep 25 '15 at 04:04
  • @user3811577 font should be size 13. What i needed is to calculate the label height based on test length, which is based on input. – Jagajith Monappan Kalarickal Sep 25 '15 at 04:11
  • But why is the height of the label changing? – TurtleFan Sep 25 '15 at 05:02
  • @user3811577 Some case the content size is very large, in these cases i need to change the label height dynamically. – Jagajith Monappan Kalarickal Sep 25 '15 at 05:06
  • I understand why you want to change the width of the label. If you have more text, you need to extend the label horizontally. But labels don't support more than one line (you need a text view for that)... So anyways the height is pretty much going to be the same, you can just hard code it for the maximum height you'll need (like the top of an A and bottom of a j). – TurtleFan Sep 25 '15 at 05:09
  • @user3811577 Label width is alway constant, i want to change label height. By using boundingRectWithSize i could calculate the required height, but due to some small error i didn't get correct height. – Jagajith Monappan Kalarickal Sep 25 '15 at 05:16
  • Does this mean you have now resolved the issue? – TurtleFan Sep 25 '15 at 05:24
  • @TurtleFan No i didn't resolve the issue. I still need a perfect solution. – Jagajith Monappan Kalarickal Sep 25 '15 at 05:36
  • So can you please clarify why your label is increasing in size vertically and not horizontally..? Is this some custom implementation? I think you may want to try a text view instead of a label... – TurtleFan Sep 25 '15 at 06:10
  • @TurtleFan What i need to implement should be in label (first point). Second the width is set based on iphone screen so it can't be change. So the only way to display whole content is by changing the label height. – Jagajith Monappan Kalarickal Sep 25 '15 at 06:40

0 Answers0