align UILabel to be centered on UIView - iphone

I am implementing a footer view, which is a UIView and it has a label inside it. I wanted it so that the UILabel stays in the center all the time, but it just won't. Here's my code:
- (void)setupFooterView
{
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView_.frameWidth, kFooterViewHeight)];
[footerView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth];
[footerView setBackgroundColor:[UIColor clearColor]];
UILabel *loadMoreLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[loadMoreLabel setBackgroundColor:[UIColor clearColor]];
[loadMoreLabel setFont:[UIFont fontWithName:kProximaNova size:16]];
[loadMoreLabel setTextColor:[UIColor whiteColor]];
[loadMoreLabel setLineBreakMode:UILineBreakModeTailTruncation];
CGSize desiredSize = [[PNRConstants kLoadingMore] sizeWithFont:loadMoreLabel.font constrainedToSize:CGSizeMake(footerView.frameWidth, footerView.frameHeight) lineBreakMode:loadMoreLabel.lineBreakMode];
[loadMoreLabel setFrameSize:desiredSize];
[loadMoreLabel setCenter:CGPointMake(footerView.frameWidth/2, footerView.frameHeight/2)];
self.loadingMoreLabel_ = loadMoreLabel;
[footerView addSubview:self.loadingMoreLabel_];
[loadMoreLabel release];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[spinner setBackgroundColor:[UIColor clearColor]];
[spinner setFrameX:self.loadingMoreLabel_.frameX - spinner.frameWidth - 10];
[spinner setFrameY:self.loadingMoreLabel_.frameY];
self.spinner_ = spinner;
[footerView addSubview:self.spinner_];
[spinner release];
[self.scrollView_ setFooterView_:footerView];
[footerView release];
}
What am I doing wrong here?

[loadMoreLabel setCenter:CGPointMake(footerView.frameWidth/2, footerView.frameHeight/2)];
The footerView.frameWidth/2 needs to be
footerView.frameWidth/2 - loadMoreLabel.frame.size.width/2
This is because the anchor of the label is in the top left corner instead of in the center. You subtract half the width of your label to account for this.

after setting label's frame: [yourLabel setCenter: yourFooter.center];

The right way: use nibs, set the resize mask and call it a day.
The painful way: programmatically create the label.
Then a couple of options:
- make the label as wide as the footer and set it's text align to center.
- set the label's center property to footer.frame.width/2 and footer.frame.height/2
Edit: oh, you're dong that already. Set the text align property of yournlabel to center.

Related

How to highlight only text in UILabel - IOS

I just want to highlight only text in UILabel, I have tried by giving backgroundColor for label, but it is highlighting the empty spaces also looks not good. So Is there any way to highlight text without resizing UILabel.
Please check the image, here labels are bigger than the text (center aligned)
Thanx.
Most of the other solutions don't consider text that spans multiple lines while still only highlighting the text, and they are all pretty hacky involving extra subviews.
An iOS 6 and later solution is to use attributed strings:
NSMutableAttributedString *s =
[[NSMutableAttributedString alloc] initWithString:yourString];
[s addAttribute:NSBackgroundColorAttributeName
value:[UIColor greenColor]
range:NSMakeRange(0, s.length)];
label.attributedText = s;
This will add a subview behind the text, with the correct size:
CGSize size= [[label text] sizeWithFont:[UIFont systemFontOfSize:18.0]];
NSLog(#"%.1f | %.1f", size.width, size.height);
NSLog(#"%.1f | %.1f", label.frame.size.width, label.frame.size.height);
UIView *highlightView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
[highlightView setBackgroundColor:[UIColor greenColor]];
[self.view insertSubview:highlightView belowSubview:label];
[highlightView setCenter:label.center];
And don't forget: [label setBackgroundColor:[UIColor clearColor]];
try this
MTLabel
MTLabel *label4 = [MTLabel labelWithFrame:CGRectMake(20, 270, 250, 60)
andText:#"This label is highlighted, has a custom line height, and adjusts font size to fit inside the label."];
[label4 setLineHeight:22];
[label4 setFont:[UIFont fontWithName:#"Helvetica" size:30]];
[label4 setAdjustSizeToFit:YES];
[label4 setMinimumFontSize:15];
[label4 setFontHighlightColor:[[UIColor orangeColor] colorWithAlphaComponent:0.5]];
[self.view addSubview:label4];
You can have a UIImageView, set its background color of your choice then place your UIlabel on it.
That will surely solve your problem.
Here is Workaround code :
UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 100, 40)];
[imgView setBackgroundColor:[UIColor brownColor]];
[self.view addSubview:imgView];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:UITextAlignmentCenter];
label.text = #"My Label";
[imgView addSubview:label];
[label release];
[imgView release];
You can also achieve the same using Interface Builder.

How do I vertically correct the navigationBar's titleView text position when using a custom font?

We're using custom fonts for the titleView in the navigation bar.
Somehow Apple always draws this font too high.
How do I correct for this strange offset you get when you are using custom fonts in a navbar?
I used setTitleVerticalPositionAdjustment:forBarMetrics:.
Compatibility: available starting from iOS 5.
Your can set a new view as titleView, then add a new label to it:
UIView * customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 40.0f)];
UILabel * customLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 200.0f, 20.0f)];
[customLabel setBackgroundColor:[UIColor clearColor]];
[customLabel setTextColor:[UIColor whiteColor]];
[customLabel setFont:[UIFont systemFontOfSize:12.0f]];
[customLabel setTextAlignment:UITextAlignmentCenter];
[customLabel setText:#"Your Text"];
[customTitleView addSubview:customLabel];
[customLabel release];
[self.navigationItem setTitleView:customTitleView];
[customTitleView release];

Is it possible to put a square bracket for the focus when the camera appears in the ZBar SDK?

I'm trying to place an image (a square bracket for the focus) in the camera view of the ZBAR SDK? Can anyone please help me what needs to be done? thanks
You can set overlay of camera screen to set the cameraOverlayView property. Design a view as per your requirement and assign it:
reader.cameraOverlayView = [self CommomOverlay];
-(UIView *)CommomOverlay{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
UIImageView *TopBar = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,58)];
[TopBar setImage:[UIImage imageNamed:#"topbar.png"]];
[view addSubview:TopBar];
UILabel *Toplabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 9, 300, 30)];
[Toplabel setFont:[UIFont fontWithName:#"Arial-BoldMT" size:11]];
[Toplabel setTextAlignment:UITextAlignmentCenter];
[Toplabel setBackgroundColor:[UIColor clearColor]];
[Toplabel setTextColor:[UIColor colorWithRed:76/255.0 green:76/255.0 blue:76/255.0 alpha:1.0]];
[Toplabel setNumberOfLines:1];
[Toplabel setText:#"Scan now "];
[TopBar addSubview:Toplabel];
UIImageView *FrameImg = [[UIImageView alloc] initWithFrame:CGRectMake(60,150,193,170)];
[FrameImg setImage:[UIImage imageNamed:#"frame.png"]];
[view addSubview:FrameImg];
return view;
}

UINavigationBar custom title position

I have a custom made UINavigationBar (different size, background etc) that has inside a custom title view. I used this code to achieve this:
UIView * mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
mainView.backgroundColor = [UIColor yellowColor];
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,kScreenWidth,80)];
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UINavigationItem *currentItem = [[UINavigationItem alloc] init];
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont fontWithName:#"Helvetica-Bold" size: 30.0];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:self.title];
[label sizeToFit];
[currentItem setTitleView:label];
[label release];
[navBar pushNavigationItem:currentItem animated:NO];
[currentItem release];
[mainView addSubview:navBar];
[navBar release];
self.view = mainView;
[mainView release];
However, my problem now is that, even if the custom title view is correctly added, it's not vertically centered with the NavBar. What I am missing?
Thanks!
You can use setTitleVerticalPositionAdjustment:forBarMetrics: in iOS 5 to change the title position, it works on normal title e.g.:
CGFloat verticalOffset = -4;
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
You created you UINavigationBar with a height of 80 points, when a standard UINavigationBar has 44 pts. It probably position it centred it on a 44 pts based centre...
You can try to make a UIView with a height of 80 pts and add the UILabel centred inside it. (not tested, just a guess)

How can I make text in a UILabel non-centered?

I have a UILabel:
descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 30, 130, 150)];
[descriptionLabel setFont:[Utils getSystemFontWithSize:14]];
[descriptionLabel setBackgroundColor:[UIColor clearColor]];
[descriptionLabel setTextColor:[UIColor whiteColor]];
descriptionLabel.numberOfLines = 0;
descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
[self addSubview:descriptionLabel];
If the text is only 1 line long it will appear in the middle of the label.
Is there anyway I can have the text display from the top left corner of the label instead of the middle center?
You can set
[descriptionLabel setTextAlignment:UITextAlignmentLeft];
or if you prefer dot syntax
descriptionLabel.textAlignment = UITextAlignmentLeft;
for left/right alignment set the textAlignment property of UILabel to either UITextAlignmentLeft or UITextAlignmentRight.
for more fine tuning you can override drawTextInRect: