How can I adjust the label Width according to the text? If text length is small I want the label width small...If text length is small I want the label width according to that text length. Is it possible?
Actually I have Two UIlabels. I need to place these two nearby. But if the first label's text is too small there will be a big gap. I want to remove this gap.
Try the following:
just use to if you using constrain in your view or xib or cell
if its not working then
sizeWithFont constrainedToSize:lineBreakMode:
is the original method to use. Here is an example of how to use it is below:Try these options,
You can also try with
[label sizeToFit];
Using this method, you can set frame of two labels as,Function
sizeWithFont:
is deprecated in iOS 7.0, so you have to usesizeWithAttributes:
for iOS 7.0+. Also to suport older versions, this code below can be used:Using function
ceil()
on result ofsizeWithAttributes:
is recommended by Apple documentation:"This method returns fractional sizes; to use a returned size to size views, you must raise its value to the nearest higher integer using the ceil function."
sizeWithAttributes