Adjust UILabel height to text

I’ve just put this in a playground and it works for me. Updated for Swift 4.0 import UIKit func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{ let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.greatestFiniteMagnitude)) label.numberOfLines = 0 label.lineBreakMode = NSLineBreakMode.byWordWrapping label.font = font label.text = text label.sizeToFit() return label.frame.height } let font = UIFont(name: “Helvetica”, size: 20.0) … Read more

Position: absolute and parent height?

If I understand what you’re trying to do correctly, then I don’t think this is possible with CSS while keeping the children absolutely positioned. Absolutely positioned elements are completely removed from the document flow, and thus their dimensions cannot alter the dimensions of their parents. If you really had to achieve this affect while keeping … Read more

Change UITableView height dynamically

There isn’t a system feature to change the height of the table based upon the contents of the tableview. Having said that, it is possible to programmatically change the height of the tableview based upon the contents, specifically based upon the contentSize of the tableview (which is easier than manually calculating the height yourself). A … Read more

When Can I First Measure a View?

I didn’t know about ViewTreeObserver.addOnPreDrawListener(), and I tried it in a test project. With your code it would look like this: public void onCreate() { setContentView(R.layout.main); final TextView tv = (TextView)findViewById(R.id.image_test); final LayerDrawable ld = (LayerDrawable)tv.getBackground(); final ViewTreeObserver obs = tv.getViewTreeObserver(); obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw () { Log.d(TAG, “onPreDraw tv height is … Read more

How to make a floated div 100% height of its parent?

For the parent: display: flex; You should add some prefixes http://css-tricks.com/using-flexbox/ Edit: Only drawback is IE as usual, IE9 does not support flex. http://caniuse.com/flexbox Edit 2: As @toddsby noted, align items is for parent, and its default value actually is stretch. If you want a different value for child, there is align-self property. Edit 3: … Read more

Setting width/height as percentage minus pixels

You can use calc: height: calc(100% – 18px); Note that some old browsers don’t support the CSS3 calc() function, so implementing the vendor-specific versions of the function may be required: /* Firefox */ height: -moz-calc(100% – 18px); /* WebKit */ height: -webkit-calc(100% – 18px); /* Opera */ height: -o-calc(100% – 18px); /* Standard */ height: … Read more