The font size of text in a UITextView is dynamic, how can I determine the current height of a row (i.e. sizeWithFont.height + line spacing height)?
You might try:
textView.font.lineHeight
Content size of the UITextView gives the height of the text in textView.
With the frame property you can access properties like height or width.
myTextView.frame.size.heigth;
If you get silly values, use bounds instead of frame.
Related
I have a view and inside it I have a UILabel. I want the view to change its width according to the length of the text.
What I tried is to first perform SizeToFit() on my UILabel, hoping this will change its frame, and then put the labels frame into the view's frame.
This doesn't work, I guess because of auto layout restrictions.
This is the code of what I tried:
private func example(){
self.labelInsideView.sizeToFit()
viewThatContainsLabel.frame = labelInsideView.frame
self.layoutIfNeeded()
}
It seems pretty obvious that this won't work, because I am pretty sure size to fit does not change the frame. Is there any way to achieve this?
Make an IBOutlet of your UIView's width constraint and then simply change the width constraint constant to the width of UILabel
self.labelInsideView.sizeToFit()
self.widthConstraintOfView.constant = labelInsideView.frame.size.width
self.layoutIfNeeded()
Why are you even using this function. Your view should have constraints.
Leading = Top = Bottom = 8 and Trailing >= 20
Your label should have all the constraints equal to 5.
Now try and add any text in your Label and you'll get updated frame of the view without using this function. Just try this once.
I am having label in my Smartface project for description field.
Is there any way to set the height of the label to auto or setting height basing on the text length.
Thanks in advance.
There is a property for that.
Check the attached picture below :
If you make the label's autosize property true, then it's width property changes automatically in order to show the text.
If this is not enough for your text, you can also use multiline property. This makes your label's height property change.
If you don't want to change label's size, then you can use "Adjust font size to fit". If you set an appropriate value for min font size, then your text font gets smaller in order to fit into the label.
I have several textfields on a single row in the same band. The first of them is having a larger font than the rest. However, the size of this font might change for each record. How can I set the height of the other textfields to be dependant of the height of the first one?
Thanks for your help,
Andreas
On each field in the row set Stretch Type to be Relative to Tallest Object.
does anybody know how to vertically align these text elements. I would like to define an absolute left padding for the text which is not affected by each image dimensions. It's quite annoying like this:
thanks a lot!
You have two options.
1) UITableViewCell has a textLabel property which you can access and adjust the frame. See the Apple docs for more info on UITableViewCell
2) You can ensure that all of your icons have the same width.
You could do it by slecifying the frame on your UIImage, UILabel, or both. Set the float x to a safe distance from the left on the label or specify the width of your widest image as the width of the UIImage frame.
I can clarify more if needed when I'm not on my mobile.
I have an NSString and want to fit it into a rectangle. The rectangle has a specified size, lets say width=150 and height=30. When the String is short and has only one character, it can be as high as the rectangle. More specific: It can have a big font size. But if the string has too much characters and would exceed the bounds of the rectangle, it must become smaller. More specific: It's font size must become smaller, so that it won't exceed the bounds of the rectangle. Is there a way of doing that without messing around in core graphics?
For some reason, UILabel's adjustsFontSizeToFitWidth Property has no effect. The text keeps beeing small even if there is plently of space.
I've set that to
label.adjustsFontSizeToFitWidth = YES;
but nothing happens. I hope there is another way to do that...
There are several part to UILabel that make this possible, but first you have to know if you want to truncate the string or make the font size smaller to fit in the rectangle.
For both cases you'll want to set the UILabel's numberOfLines property to 0, allowing the label to wrap as much as necessary. Then you'll want to set the frame of the UILabel to match the rectangle you're looking to fit. From there you take one of two paths:
Truncation: Set the lineBreakMode property to UILineBreakModeClip, UILineBreakModeHeadTruncation, UILineBreakModeTailTruncation, or UILineBreakModeMiddleTruncation depending on the truncation behavior you're looking for.
Resizing: Set the lineBreakMode to either UILineBreakModeWordWrap or `UILineBreakModeCharacterWrap' depending on your preference. Then you'll need to enter a loop to figure out the right font size. Start with a reasonable font size (e.g., 12) and:
Set the font property of the UILabel with a UIFont that matches that size
Call - (void) sizeToFit for the UILabel.
Check the frame for the UILabel:
If the frame will fit within the bounds you need it to, you're done
If the frame is still to big, drop the size of the font and repeat the loop
For the latter option you'll want to make sure you're not squeezing the text into oblivion, so you'll want to put a minimum size cap on the font size.
You can get more information from the UILabel and UIFont documentation.
adjustsFontSizeToFitWidth adjusts the font size down, not up.
Set the font on your UILabel to an appropriately large size, and UILabel will shrink it when necessary to fit in its bounds.
Set label font size to desired normal size.
Set label minimum font size to
smallest possible font
(minimumFontSize property)
Set adjustsFontSizeToFitWidth to
YES.
If you need multiple lines, set lineCount to 0 as the other poster noted.