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;
Related
Ok i have already gone through similar questions, but none of them helped.
I want to add a UILabel inside a UIScrollView so that the Label can be scrolled if the contents are large. Here is my code:
ViewController.h:
#interface ViewController : UIViewController
{
UILabel *myLabel;
UIScrollView *myScroll;
}
ViewController.h:
myLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+30, self.view.frame.size.width, self.view.frame.size.height)];
myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+30, self.view.frame.size.width, self.view.frame.size.height)];
myLabel.backgroundColor = [UIColor yellowColor];
myLabel.text = #"Large random text";
[myLabel setNumberOfLines:0];
myLabel.lineBreakMode = NSLineBreakByWordWrapping;
[myLabel sizeToFit];
myScroll.contentSize = CGSizeMake(myLabel.frame.size.width,
myLabel.frame.size.height);
[myScroll addSubview:myLabel];
[self.view addSubview:myScroll];
I searched a lot on the internet but could not find a answer, can someone let me know what the issue is ?
Thank You !
I'm not sure how to do that, but I would like to suggest an alternative that may suit your needs. Use a UITextView, but set " [textView userInteractionEnabled:NO] " and it will act as a label, since it cannot be edited. It might end up looking like how you want.
Based on your comments, you need to size the label as needed so it is big enough for the given text. Then add the label to scroll view. Then set the scroll view's contentSize so it fits the whole label. This will ensure the scroll view allows you to scroll to see the whole label.
In addition to this, make sure the label's frame is relative to the scroll view and not to self.view. In other words, setting the label's frame's origin to 0,0 will put the label in the top left corner of the scroll view regardless of the position of the scroll view relative to its parent.
Try by replacing your contentSize like below code,
myScroll.contentSize = CGSizeMake(self.view.frame.size.width,self.view.frame.size.height + 100);
Hope it will work for u.
1.You have to use View(ContentView) to place the labels.
2.Add the ContentView into ScrollVIew.
3.Assign the ContentView Size whatever You Want
Go Through This Link,You Will get a Clear idea.
https://www.youtube.com/watch?v=UnQsFlMGDsI
Remove scrollView = [[UIScrollView alloc] init]; this is not necessary.Please Upvote if it helps.
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];
In my awakeFromNib function, I have:
[_descriptionLabel setLineBreakMode:UILineBreakModeWordWrap];
[_descriptionLabel setNumberOfLines:10];
[_descriptionLabel sizeToFit];
and yet my label looks like this:
I know I'm setting these calls on the right label, because without these lines, the text appears vertically centered rather than aligned at the top. How can I make make my UILabel multiline?
I also tried setNumberOfLines:0.
SOLUTION I had set the width incorrectly in the xib file.
Try to set [_descriptionLabel setNumberOfLines:0]; which set the number of lines to auto.
It might be that sizeToFit is changing your label width, try to set it to explicit width, or remove it.
Just make sure that the initial width is set correctly. Then it should expand/shrink the height. But still the label won't be aligned to the top, it will be centered in its previous size after a call of sizeToFit (in case the new height is smaller than before, otherwise the origin will stay the same).
If you're not using AutoLayout, you need to measure text in code:
CGRect frame = _descriptionLabel.frame;
CGSize size = [_descriptionLabel.text sizeWithFont:_descriptionLabel.font constrainedToSize:CGSizeMake(frame.size.width, FLOAT_MAX) lineBreakMode:_descriptionLabel.lineBreakMode];
frame.size.height = MIN(MAX(size.height, frame.size.height), MAX_ALLOWED_HEIGHT);
_descriptionLabel.frame = frame;
Are you sure your awakeFromNib is in the right place. If it's in the view controller, it won't work. If it's in the implementation of a custom label, it should get called and that code should work:
#interface TestLabel : UILabel
#end
#implementation TestLabel
- (void)awakeFromNib
{
[self setLineBreakMode:UILineBreakModeWordWrap];
[self setNumberOfLines:10];
[self sizeToFit];
NSLog(#"Label: %#", NSStringFromCGRect(self.frame));
}
#end
Also, it's easier to set numberOfLines to 0, which means "any number of lines". And you shouldn't need to set UILineBreakModeWordWrap, since that's the default.
If you want to keep the code in the view controller, you can move it to viewDidLoad, which is the best place to do this kind of setup.
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.