I have 2 UILabels, one which is multi-line and other is of 2 lines. I always have an image besides these labels and its becoming difficult for me to align the image with the text. Eg:
If the 2 line label just has some small text on it, the text is displayed on the middle of the label and the image is aligned with the first line. If it has exactly 2 lines on text on it, then the image is aligned to the first line of text which is exactly what I want.
Is there a way I can get the position or co-ordinates (x,y) of the first line on UILabel so that I can adjust the image constraints accordingly then?
This will give you the co-ordinates and the dimensions:
label.frame.origin.x
label.frame.origin.y
label.frame.size.width
label.frame.size.height
Related
I need to fit a label and an image in a table view cell in such a way that the image should be placed immediately next to the label but within the cell visibility. ie., they should look like below,
When the text is smaller or within a line, then the image could be immediate next to the label. Eg -
Smaller text (image)
When the text goes beyond a line, then image could be at the edge of the cell because the label length had already been extended to the length. Eg -
Multiline text multi-line text multi-line
text multi-line text multi-line text multi (image)
multi-line text multi-line text
I am trying to achieve this using VFL and no storyboard or nib.
What I tried out :
1. "H:|-[label]-[image(==20)]-|" , "V:|-[label]-|"
This makes the image to always attached to the edge thus the case 1 fails which needs the image near the label. Eg -
case 1 :
Smaller text (image) (not ok)
case 2 :
Multiline text multi-line text multi-line
text multi-line text multi-line text multi (image) (ok)
multi-line text multi-line text
2. "H:|-[label]-[image(==20)]" , "V:|-[label]-|"
This makes the image near the text thus okay for case 1 whereas that fails for case 2 which does not wrap the text to multiple lines then and the text is extending beyond the cell width. Eg -
case 1 :
Smaller text (image) (ok)
case 2 :
Multiline text multi-line text multi-line text multi...
(not ok - text not wrapped to multiple lines, remaining text and image hidden)
How to resolve this? Setting priorities to some constraints would help ?
I don't have much experience with VFL but , I know Autolayout so I can give you Idea how to resolve
I have seen your both tries
1. "H:|-[label]-[image(==20)]-|" , "V:|-[label]-|"
2. "H:|-[label]-[image(==20)]" , "V:|-[label]-|"
In first try you have fixed image on trailing
on second try you have fixed image with label
What would be combine solution ?
Your clear requirement says that you need your image attached with label trailing also take care about tailing to superview
So clear solution for that problem is
you have to give your imageView Trailing may be this code [image(==20)]-| should be changed with Greater than or equal to relation with 0 Constant
it will tells autolayout engine to keep Min 0 Distance from trailing of superview
Sorry I couldn't give you exact code as I don't have much experience with VFL
Hope it will help to you
I have a UILabel that I'm using inside a container view. I'm using AutoLayout entirely programmatically--no xib or storyboard. My UILabel constraints (no height constraint set, large fixed width, top and left edges pinned, numberOfLines = 0, bottom of superview pinned to bottom of label) work perfectly for normal (around 13 or 14 point) sized text, but at large (60 point) size text I started noticing large gaps of extra space above and below my character; in this case, the capital letter "S" that's an NSMutableAttributedString. The only attributes for this attributed string are the font name and a kerning value of 1.0. Below picture shows a magnified screenshot of my label from Pixie (white square shown is from that program) with my UILabel's backgroundColor in red:
After doing lots of research (Ignore Ascender and Descender when centering UILabel vertically?, iOS - Get the "real" height of a letter, Vertically center text in UILabel depending on actual visible letter height), it seemed the reason I was encountering this issue was because of the ascender and descender values for this font aren't getting used in this case (this is a capital "S," so it would seem I just need its capHeight value). So, I decided to constrict my label height to this value, and did so by setting a height constraint equal to its capHeight value after the "S" attributed text is set. Doing this produced the following image:
As you can see, the text is now clipped in the label. As a sort of hack, I figured I'd add some padding to counteract this, so multiplying my capHeight value by 1.1 yielded the following image:
That's fine for some padding, but now there's 4 px of extra space at the top. Granted I'm zoomed in very far, but I'm not sure why I'm seeing this—I would think that since the height constraint is set to the height of this capHeight value (which has some extra added for padding now) that the "S" would be vertically centered. I've tried calling setNeedsLayout on the label, but that didn't do anything. These are the things I would love to know:
Why is the capHeight value either not returning the correct value and/or setting the label's height constraint to this value is causing the text to get clipped?
Why do I need to add padding to the capHeight value?
Does anyone have any suggestions on how I can get this text vertically centered after I add padding and/or why it's not happening automatically? I saw this answer (https://stackoverflow.com/a/17376146/482557) but wasn't sure if that would apply here.
Thanks!
I have 3 UILabels drawn in storyboard positioned on top of each other.
Static UI Label 1
Dynamic UI Label 2
Static UI Label 3
Labels 1 and 3 are static and never change. Label 2 is dynamic and is always one sentence long, but could be a short or long sentence that wraps. I want Label 2 to be perfectly vertically centered between label 1 and label 3 based on how much text is there. Any ideas how to do this? Greatly appreciated!
If you're using auto layout (which is on by default), then you can just stretch the middle label until it's top and bottom are the standard distance away from the other two labels (you will see a dotted blue line when you reach that distance). This assumes that your label has a clear background, or that you don't mind seeing a tall label if it doesn't. The text will be centered vertically in this tall label regardless of the number of lines. It will also stay centered on rotation.
Add the center values of Label1 and Label3 , Divide it by 2 and make it center of label2
CGPoint point = CGPointMake(Label2.Center.x,(Label1.center.y+Label3.center.y)/2);
Label2.center = point;
That's all....
I have the following cell design where the numeric label shrinks and the "Overall" label is directly underneath.
I have properly set the adjustFontSizeToFitWidth and minimumFontSize properties. The font is resizing correctly. However, anchoring the numeric label to the bottom is challenging. Particularly when the font shrinks the gap between the two labels widens and does not appear vertically centered.
I have tried sizeToFit, sizeThatFits, and using the font's pointSize. All unsuccessfully.
I am aware of sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:, but don't understand why I would need it in combination with adjustFontSizeToFitWidth.
Ah, so you want to position the UILabels in the middle of the container view (both horizontally and vertically)?
I have rephrased my answer so it will make more sense to future readers.
My code is assuming that you have the 3 IBOutlets set up:
UIView *containerView; //Your nice view containing the two textfields.
UILabel *points; //The label containing the number.
UILabel *overall; //The textfield containing the text 'overall'.
You could simply set the frame of the labels after assigning the text and calling the sizeToFit.
This first line positions the UILabel points, the only change being that the y coordinate is half of containerView subtract half of the height of itself.
points.frame = CGRectMake(points.frame.origin.x, (containerView.frame.size.height / 2) - (points.frame.size.height / 2), points.frame.size.width, points.frame.size.height);
To position the overall accordingly - say there is a distance of say 6 between the number and overall labels:
int space = 6;
overall.frame = CGRectMake(overall.frame.origin.x, points.frame.origin.y + points.frame.size.height + space, overall.frame.size.width, overall.frame.size.height);
Having read your comments, I think you are after this solution. If you want both UILabels to appear in the middle; subtract (overall.frame.size.height / 2) + (space / 2) from the y value of points like so (with the code of number 2 beneath it):
int space = 6;
points.frame = CGRectMake(points.frame.origin.x, ((containerView.frame.size.height / 2) - (points.frame.size.height / 2)) - ((overall.frame.size.height / 2) + (space / 2)), points.frame.size.width, points.frame.size.height);
overall.frame = CGRectMake(overall.frame.origin.x, points.frame.origin.y + points.frame.size.height + space, overall.frame.size.width, overall.frame.size.height);
The final point will produce an output like this image. As you can see the blue line is half of the whole image, and intersects the black rectangle (which is snuggly around the two labels) at its middle point. I hope this is what you were after.
Instead of using two labels, use CATextLayer instead. You will be easily able to make one part BOLD and the other normal. plus position and adjusting size for One layer will be easy relative to placing two labels. shadow setting, line break mode, fonts you will be able to adjust everything beautifully :)
I'm drawing a bingo card (UIView) with 27 squares: 9 columns x 3 lines. Cards can have 3 different sizes, so they must be resizable. And inside the card, every square with a number is an UIView with a custom class. Inside the view (in the .xib file) there is the UILabel, where the number is set with:
[lblNumber setText: [NSString stringWithFormat:((number < 10) ? #" %i" : #"%i"), number]];
When numbers have two digits (10->90) everything looks great inside the squares, but for numbers from 1 to 9, label displays with a bigger font size than the others because it only has 1 digit.
Ok, I tried to uncheck the "Adjust to fit" setting, but then font size remains small.
Autosizing settings of the label are set to both arrows, so the label has "space" to resize.
¿How can I have single digit numbers to resize the exact proportion than numbers with two digits?
As you can see in the line of code, I also tried to concatenate a space for numbers with one digit.
Thanks in advance.