How to rotate viewForHeaderInSection - iphone

UITableView has custom header UIView, which contains UILabel. Since table has also section index at right side, I want to center the label in remaining space.
Problem is how to "center" label in same way, when device is rotated to landscape! I've tried both Interface Builder and code-only and something is always wrong.
Restrictions:
Label is as narrow as possible
Label must not be resized
Label must be always centered inside table width minus section index
One easy way to fix this would be making section header view detached from tableView right side. Tried to do it, failed. Here's some code from viewForHeaderInSection:
CGRect frame = CGRectMake(0, 0, tableView.bounds.size.width - 32, 50.0f);
// MAGIC 32 for SectionIndexTitles
UIView *view = [[[UIView alloc] initWithFrame:frame] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.font = [UIFont fontWithName:#"Courier" size:20];
label.text = #"Section title long";
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor redColor];
CGSize labelSize = [label.text sizeWithFont:label.font
constrainedToSize:frame.size
lineBreakMode:UILineBreakModeClip];
label.frame = CGRectMake(0, 0, labelSize.width, labelSize.height);
label.center = CGPointMake(frame.size.width/2.0f, kHeaderHeight/2.0f);
label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
Any ideas welcome! Can't even recall how many different ways I have failed :)

Hey, set up as following in the IB:
Disable all autoresizing functionalities (no flexible width, height, nor any left, bottom, top, right).
Should do the job :)
To support the magic 32 pixels, try to position your Label as you need it.
That means:
Position it in the center of your UIView and move it 16 pixels to the left, now don't change the frame programmatically, else the "wonderful" auto-positiong will be manipulated what we don't really want to :)

Related

Subview appears underneath superviews layer.border?

I have a UIView in which I define it's border in the following manner:
self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.borderWidth = 3;
I attach a subview to this UIView, and when I move the subview over the border, it goes underneath it. Is this the intended behavior? Is there anyway to make the subview go on top of it?
According to the Apple specification: It is composited above the receiver’s contents and sublayers.
So, the border will always be above of all your subviews, even if you bring your subview to the front and so on.
So I make a background view to fake the border.
E.g.:
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.clipsToBounds = NO;
UIView *bView = [[UIView alloc] initWithFrame:CGRectInset(backgroundView.bounds, 3, 3)];
bView.backgroundColor = [UIColor redColor];
UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(-50, -50, 100, 100)];
cView.backgroundColor = [UIColor yellowColor];
[bView addSubview:cView];
[backgroundView addSubview:bView];
[self.window addSubview:backgroundView];
and the effect:
Depending on your view structure, it might be easier to add the subview to the parent of your main view. It can then overlap the main view and will overlay the border as you requested.
Did you try setting the superview's 'clipsToBounds' property to YES? This is set to NO by default for performance reasons, but setting it to yes might give you the effect you are looking for.
Insert layer at specific position that suits you:
self.layer.insertSublayer(sublayer, at: 0)

Customize UILabel's Behavior

I am new to iphone.i found UILabel instance methodes very hard for me to implement.how can i use it.how can i customize the appearance of your text of a UIlabel further by subclassing UILabel.Plz i need little help to initiate.Foe example i have a label in my viewController how can i custmize it's text and hoe to subclass
.thanks in advance.
You can use so many properties of a UILabel like:
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 40)];
lbl.font = [UIFont fontWithName:#"Helvetica" size:12.0]; // For setting font style with size
lbl.textColor = [UIColor whiteColor]; //For setting text color
lbl.backgroundColor = [UIColor clearColor]; // For setting background color
lbl.textAlignment = UITextAlignmentCenter; // For setting the horizontal text alignment
lbl.numberOfLines = 2; // For setting allowed number of lines in a label
lbl.lineBreakMode = UILineBreakModeWordWrap; // For setting line break mode
lbl.text = #"TitleText"; // For setting the text inside the label
Let me know if any thing other than this you want to know!!
The two methods
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
return CGRectInset(bounds, MARGIN, MARGIN);
}
- (void)drawTextInRect:(CGRect)rect
{
[super drawTextInRect: CGRectInset(self.bounds, MARGIN, MARGIN)];
}
We are using CGRectInset to create a rectangle that is either larger or smaller than an existing rectangle (bounds).
For Smaller rectangle, use positive values as MARGIN
For Larger rectangle, use positive values as MARGIN
All the best!!!

How to fix UILabel alignment in UINavigationBar

I put a custom UILabel in my UINavigation bar like this:
UILabel *navTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
navTitle.backgroundColor = [UIColor clearColor];
navTitle.text = #"TEST";
navTitle.font = [UIFont fontWithName:#"GothamNarrowBook-Regular" size:28];
navTitle.textColor = [UIColor whiteColor];
navTitle.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = navTitle;
But when it shows up on the emulator, its aligned too high:
I have had no luck with adjusting the frame. Any ideas?
You can put label inside another view, and insert this another one as titleView. That will allow some kind of flexibility.
You can also try to adjust position of the frame:
UILabel *navTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, >>>10<<<, 200, 44)];
But remember, that NavigationBar has different height in landscape mode, so you have to use autoresizingMask accordingly.
Can you set the frame of self.nagivationItem.titleView and push it down?
CGRect initialFrame = self.navigationItem.titleView;
self.navigationItem.titleView = CGRectMake(initialFrame.origin.x, initialFrame.origin.y+10, initialFrame.size.width, initialFrame.size.height);
Another thing to check, make sure your UINavigationBar is actually 44px tall, if your UILabel is the same height then I believe default behavior is to center vertically which would land it in the correct position.
I take it that increasing the height of the UILAbel doesn't work.
Or the Y position.
And I take it you tried adjusting the frame after it's created, e.g. other than in the initWithFrame.
Maybe try using an image instead? Well it's an idea ;)

can't add subview to UIScrollView

I am trying to create a horizontal scroll view which has a UILabel as elements, by doing the following in viewDidLoad:
for(int index=0; index < [self.category count]; index++)
{
UILabel *label = [[UILabel alloc] init];
label.text = [self.category objectAtIndex:index];
label.textColor = [UIColor blackColor];
CGSize maximumLabelSize = CGSizeMake(100,9999);
CGSize expectedLabelSize = [[self.category objectAtIndex:index] sizeWithFont:label.font
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
label.frame = CGRectMake(5+xOffset, 0, expectedLabelSize.width, 40);
self.scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);
[self.scrollView addSubview:label];
xOffset += 170;
}
However, I can't see anything when I run the app in the simulator, what am I missing here? Pretty sure that the UIScrollView is connected via the IBOutlet and I know that the text exists as I tried printing that out via NSLog
UPDATE:
Also how do I check which UILabel is clicked? I wanted to know this as well..
I think you did'nt initialize xOffset=0 before running for loop. so that it is taking a garbage value and then executing xOffset += 170; instruction.
so please initialize xOffset=0;
If you're positioning the labels with a frame you should also set autoresizingMask to make sure if you position based on a portrait layout but are testing the app in landscape, your labels don't get auto-positioned outside the new bounds. Also, make sure to autorelease those labels or else you'll be leaking memory.
Your expectedLabelSize calculation seems to adjust the height, leaving the width at 100. When you set the frame of your UILabel you are just using this width.

iPhone, how do I add a label so that text will be exactly center of the screen, vert and hotiz?

I've got a view which is shown in landscape mode and I want to add a label which is int he center of the view both horizontal and vertically.
CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *lab = [[[UILabel alloc] initWithFrame:frame] autorelease];
lab.font = [UIFont boldSystemFontOfSize:16.0];
lab.textAlignment = UITextAlignmentCenter;
lab.text = #"No data!";
[self.view addSubview:lab];
However, its not the right width and its at the top of the view ?
The texts are vertically center aligned by default. If you make the frame height equal to super view (which covers whole window, I assume) height then it will be vertically center aligned in that view.
For horizontal alignment you need to set textAlignment property which you are already setting correctly. But you need to fix the frame width which should be 480.
CGRect frame = CGRectMake(0, 0, 480, 320);