I have an UIImageView that has a UILabel on top of it. This UILabel has a text that shows a number to the user of how many people like his content ( kinda like the Facebook likes or comments ),
Both the UIImageView and the UILabel are small in size so the UI needs to be just perfect. Right now if the UILabel shows a 2 digits number it still shows up aligned properly but when I get into the 3 digits it kinds skews off, how can I make sure that I can always align it to the UIImageView via code?
Similar to Scott's answer, but with the code that the poster requested
//assume that UIImageView (imageView) and UILabel (label) are already defined
label.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y - label.frame.size.height, imageView.frame.size.width, label.frame.size.height);
label.textAlignment = UITextAlignmentCenter;
Note: You'll also need to ensure that the struts and autoresize masks of the UILabel appropriately match that of the UIImageView to ensure this looks appropriately on screens of different sizes and different orientations.
A screenshot of what's going on would be nice, but you can always try to make the label the same width as the image, and then center the text of the label.
Try this:
UILabel *label = [[UILabel alloc] initWithFrame:imageView.frame];
label.textAlignment = NSTextAlignmentCenter;
This might work as UITextAlignmentCenter is deprecated in iOS 6 and above.
Related
I have a UILabel which is by default showing only 3 lines of text. I want to display all text content in some cases. I have tried everything I found, but nothing works. Here is my latest try:
CGRect labelSize = [contents.contentLanguage.Description boundingRectWithSize:CGSizeMake(self.view.frame.size.width * 0.97, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
cell.descLbl.frame = CGRectMake(labelSize.origin.x,
labelSize.origin.y,
ceil(labelSize.size.width),
ceil(labelSize.size.height));
[cell.descLbl setNumberOfLines:0];
[cell.descLbl setLineBreakMode:NSLineBreakByWordWrapping];
[cell.descLbl sizeToFit];
Maybe you should look at maybe using the UITextView instead? It's much better suited for multiline text.
https://developer.apple.com/documentation/uikit/uitextview
Ok, so I was able to resolve this issue by changing the height of the UILabel from the .xib file to >=0
Please use UiTextView in place of UILabel
textviewobj.text=#"";
[textviewobj sizeToFit];
You have two ways (choices) to do it:
Use UITextView without editing permission
Use UILabel with numberOfLines = 0
Use UITextView without editing permission
If you've fixed space (limited area to display your content i.e. fixed height) to diplay your content and you can't work with scrollable content then you should use UITextView without editing permission.
Use UILabel with numberOfLines = 0
If you've flexible space (no limited space/area to display a your content i.e. fixed height) to display your content, then you should use UILabel with number of line = 0.
Do not assign height value (constraint) if you want a flexible UILabel with height, that fits according to size of your content.
Or Assign minimum height value (constraint like >= 30) if you want to have/set a minimum area for content, whether there is no content to be display.
I am finding it surprisingly hard to resize a label containing newlines based on the quantity of lines and text. It displays fine in a large enough textview. However, I'd like the economy of sizing the label--or I'd be happy with resizing a textview--exactly.
This is the code I am using from an answer on SO but it is having no effect on the size of the label. Would appreciate any suggestions on how to make this work:
NSString *list = self.list.list;
// use font information from the UILabel to calculate the size
UITextView *view=[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 280, 10)];
//make random size view
view.text=list;
CGSize size=[view sizeThatFits:CGSizeMake(280, CGFLOAT_MAX)];
// create a frame that is filled with the UILabel frame data
CGRect newFrame = _listLabel.frame;
// resizing the frame to calculated size
newFrame.size.height = size.height;
// put calculated frame into UILabel frame
_listLabel.frame = newFrame;
Why are you setting the frame of your label with reference of a newly created UITextView, it will create a useless object in your memory, to set the label frame according to your text just use this 2 line of code
lbl.numberOfLines=0;
[lbl sizeToFit];
It will make the label as large as your text.
You really should use autolayout.
Just constrain the label where you need and let UIKit do it's job.
Here an example:
I set a top space and a leading margin constraints
Then I added a width constraint and then I added some more text
As you can see the label resized itself as it knows how much text it has inside and how much space it occupies.
I have a container UIView, and a UILabel. The container view size is set, I then add text to the UILabel, call sizeToFit on the label, and add the label as a subview of the container view.
I then use the following code to center the label inside the container view:
viewCountLabel.frame = CGRectMake(viewCountContainer.frame.size.width/2 - viewCountLabel.frame.size.width/2, viewCountContainer.frame.size.height/2 - viewCountLabel.frame.size.height/2, viewCountLabel.frame.size.width, viewCountLabel.frame.size.height);
This is pretty standard code to center something to it's superview/container, and it always works pretty well.
However, right now I have a small problem. This label's text is a simple number. It could be 1, 9, 15, 22, etc.
My problem is that when I center everything, some numbers look off center even though they technically are centered perfectly.
Here's an example pic with borders for both the container and the label so you can see that they are centered perfectly:
But here's how it looks without the borders:
The 15 does not look like it's centered even though it technically is. It looks like its a little too far to the right. If I subtract 2 or 3 from the x value, it will visually appear centered, but see that's the problem.
How can you setup logic to "visually center" various values for a label like this? I feel like setting a bunch of if statements based on the text value is overkill and only applying a bandaid to the problem.
I have also tried using NSTextAlignmentCenter and that does not make a difference. I'm not sure if I'm using NSTextAlignmentCenter incorrectly, or if there's a potential problem with sizeToFit.
You'll notice that that label's blue border has extra space on all sides, but most importantly for this problem it has extra space on the left and right sides. I feel like that could be causing this issue, but I'm not sure how to fix that or if that's even the problem.
Here is my code:
// Setup view count label container
UIView *viewCountContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 95, 95)];
viewCountContainer.frame = CGRectMake(self.view.frame.size.width/2 - viewCountContainer.frame.size.width/2, 87, viewCountContainer.frame.size.width, viewCountContainer.frame.size.height);
// Setup + add number label to container
UILabel *viewCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.pushModal.frame.size.width, 95)];
viewCountLabel.textAlignment = NSTextAlignmentCenter;
viewCountLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:72];
viewCountLabel.textColor = [UIColor colorWithRed:0.278 green:0.278 blue:0.278 alpha:1];
viewCountLabel.text = [NSString stringWithFormat:#"%d", viewCount];
[viewCountLabel sizeToFit];
// Center the label to it's super view (viewCountContainer)
viewCountLabel.frame = CGRectMake(viewCountContainer.frame.size.width/2 - viewCountLabel.frame.size.width/2, viewCountContainer.frame.size.height/2 - viewCountLabel.frame.size.height/2, viewCountLabel.frame.size.width, viewCountLabel.frame.size.height);
[viewCountContainer addSubview:viewCountLabel];
[self.view addSubview:viewCountContainer];
Unfortunately there is no "perfect" way to do this. I've added character spacing to the label, tried setting its width to the superview's width and centering, etc. but you'll never be able to do this dynamically and get it perfect this way.
The only way I could see this being perfect every time is if you made the UILabel first, and then programmatically drew a circle around it, and then centered the label.
That or the hard way of adding logic for every number and visually centering it that way.
Number can be centered by using monospacedDigitSystemFont(ofSize:weight:) in combination with textAlignment = .center
I'm using a UILabel in collection view cell which is centre aligned and has multiple lines. I'm using adjustFontSizeToFitWidth = YES and minimumFontScale = 0.5 to reduce the font size when text does not fit. This works fine in iOS 7.
But in iOS 6 the text alignment goes to the left. To align it to centre I used adjustLetterSpacingToFitWidth = NO which I found in other threads, this aligns the text to centre but the text doesn't fit and leaves a "..." trail in the end.
Thanks for the help!
You could use a UITextView which will allow you to wrap the text, unlike the UILabel. If you want all of your text on one line you could make it so that in iOS 7 the text is shrunk one amount and in iOS 6 it is shrunk more.
Now UILabel has [textLabel sizeToFit]; for label size changing also you can use following code:
textLabel.adjustsFontSizeToFitWidth = YES;
textLabel.numberOfLines = 0;
[textLabel sizeToFit];
I am trying to make a label which displays properly on both landscape and portrait mode.
The label looks fine when i am in portrait mode but when i switch to the landscape mode the label is not aligned correctly.Is there a way to adjust the UIlabel automatically when the screen is switched from one mode to another.
Edit:
This is the code that i have
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100 , 100, 100, 100)];
[label setText:#"xxx."];
label.adjustsFontSizeToFitWidth=YES;
[self.view addSubview:label];
[label release];
I have also created a NStimer method which discards the label after 5 seconds
What do you mean by automatically, and how do you want it to be aligned?
You could add code to -willRotateToInterfaceOrientation:duration: to give the UILabel a new frame- but it's hard to post the exact code unless you're specific about what you want the label to do.
I think, you mean shifting of your label.
You can use autoresizingMask. For example, for connecting label with left part of the screen:
yourLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
use autoresizingMask
lblBookmarkName.autoresizingMask = UIViewAutoresizingFlexibleWidth;