iOS :viewForHeaderInSection can't get align with cell? - ios

I have this code in my viewForHeaderInSection: of tableViewController
UILabel *l = [[[UILabel alloc] initWithFrame:CGRectMake(20, 0, 320, 40)] autorelease];
l.backgroundColor = [UIColor clearColor];
//l.textAlignment = UITextAlignmentLeft; //i tried it didn't help!
l.text= NSLocalizedString(#"My Label:", #"");
the label always appears around 35 point off the cell's left side. If i use the UITextAlignmentCenter it works and it goes in center but I want it align with left size of cell. I tried the CGRectZero instead of CGRectMake(20, 0, 320, 40), changed x but it didn't work!
Any help greatly appreciated.

At first make view with frame (0, 0, 320, 40). Then put label on it.

Related

Input Range Width Of UITextField iOS

Is it possible to set a UITextField's input range (not the number of characters)? You can check out my screenshot below.
I already tried implementing this: Ellipsis at the end of UITextView
It didn't work, or I'm just doing it wrong.
What I want is to limit the size of the user's input AND make it ellipsis. The text at the left side of UITextfields are merely UILabels set as subviews.
So again... I need help in setting the range of the text input, and if the input length or width overlaps my RED MARK in my screenshot, the rest of the input will be converted to POINTS OF ELLIPSIS. And then I'm pretty sure I can now proceed to my project (example: clicking the arrow button will make a small popup that will show the full text).
I forgot to add my codes in that UITextField:
self.billingAddressTextField = [[UITextField alloc] init];
_billingAddressTextField.frame = CGRectMake(0, 150, scrollView.frame.size.width, 50);
_billingAddressTextField.delegate = self;
_billingAddressTextField.borderStyle = UITextBorderStyleNone;
_billingAddressTextField.background = [UIImage imageNamed:#"textfieldLogIn.png"];
[_billingAddressTextField setTextColor:[UIColor colorWithRed:0.686 green:0.686 blue:0.686 alpha:1.0]];
_billingAddressTextField.backgroundColor = [UIColor clearColor];
_billingAddressTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
[_billingAddressTextField setFont:[_billingAddressTextField.font fontWithSize:16.0f]];
[_billingAddressTextField setTextAlignment:NSTextAlignmentRight];
[scrollView addSubview:_billingAddressTextField];
Ok solved, I just added a new Padding (UIView) at the left side of the UITextField (billingAddressTextField). So there are now two paddings (both side) in my UITextField.
UIView *paddingViewForArrowButton = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35, 25)];
_billingAddressTextField.rightView = paddingViewForArrowButton;
_billingAddressTextField.rightViewMode = UITextFieldViewModeAlways;
UIView *paddingforBillingLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
_billingAddressTextField.leftView = paddingforBillingLeft;
_billingAddressTextField.leftViewMode = UITextFieldViewModeAlways;

How can I get the text to center in my scrollable UILabel?

The scrollable label works fine, but for some reason I cant get the text in the UILabel to center in the UIScrollView, it always stays left aligned. Here is an image of what I am talking about, and my code. (P.S. the UILabel is a subview of the UIScrollView, and the UIScrollView is a subview of the UITableViewCell.
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(cell.frame.size.width/21.5, 25, cell.frame.size.width - 40 , 20)];
scrollView.showsHorizontalScrollIndicator = NO;
[cell addSubview:scrollView];
scrollLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, 20)];
scrollLabel.text = secondaryLabel.text;
scrollLabel.font = secondaryLabel.font;
scrollLabel.backgroundColor = [UIColor clearColor];
scrollLabel.textColor = [UIColor whiteColor];
scrollLabel.numberOfLines = 1;
scrollLabel.textAlignment = NSTextAlignmentCenter;
[scrollLabel sizeToFit];
[scrollView addSubview:scrollLabel];
scrollView.contentSize = CGSizeMake(scrollLabel.frame.size.width, 20);
I think the code you written for UILabel is pretty much right and the center TextAlignment is there but the problem is you need to pass the right X & Y for UILabel Frame's. For now they are getting set to 0-0 and are adding on the Scrollview's starting frame. This might can solve your problem.

Adding a UILabel as a subview on top of a UITextView

I am trying to make a fake placeholder for a UITextView with a UILabel. Let's say there's a UITextView inside ViewController and this is what I do:
CGFloat frameWidth = self.postInputField.frame.size.width;
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
textViewPlaceholder.text = POST_PLACEHOLDER;
textViewPlaceholder.textColor = [UIColor lightGrayColor];
[self.view addSubview:textViewPlaceholder];
[super viewWillAppear:animated];
}
And there's an unnecessary margin on top of the UILabel as shown below:
I could set the y value to be negative something but I want to make sure why this is happening. One thing that I do when the view controller is loaded is self.automaticallyAdjustsScrollViewInsets = NO; to get rid of the margin inside the UITextView, but it shouldn't matter.
The exact x y values that work are (4, -16) for the UILabel.
I think the problem is the height of UILabel:
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
The height is too large. Try to reduce it. And using background color to see more.

Cant center UILabel in titleView of navigationItem iOS8

Im having trouble setting a programmatically allocated UILabel to be centered in my NavigationItem's titleView. Im even setting the NSAlignment to be NSAlignmentCenter. Here's an image of what it looks like:
As you can see.. I have no right UIBarButtonItem in use, but this shouldn't be keeping the text from being centered? Here's the code, in the viewDidLoad:
//Set the title of navBar----------
UILabel *navTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)]; //0 0 320 10
navTitleLabel.backgroundColor = [UIColor clearColor];
navTitleLabel.textAlignment = NSTextAlignmentCenter;
navTitleLabel.textColor=[UIColor whiteColor];
navTitleLabel.text = #"IMG";
[navTitleLabel setFont: [UIFont fontWithName:#"BullettoKilla" size:16]];
self.navigationItem.titleView = navTitleLabel;
What am I doing wrong?
As pointed out by 0yeoj you have set the item width to 320. Try something smaller.
Try this
UILabel *navTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 40)];

How to set a label at the top center

I want to set a label in the top center of the view, I've already tried this
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.myView.center.x, 25, 92, 46)];
But its not actaully in the center as I want, here is how it looks like:
And I want the middle of the label, in the x center, and not the beginning of the label in the center.
The easiest way is to make label's frame width equal to view's frame width and set the alignment of label to Center.
Hope this Helps !!!
Try like this ,
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
lbl.textAlignment=UITextAlignmentCenter;
It does exactly what you programmed. Unfortunately there is always some basic math involved in UI programming.
One possible solution:
Initialize it with some frame of any position but with the right size.
Then set the lable views' center property accordingly.
[label setCenter:CGPointMake(self.myView.center.x, 25)];
Or simply set the frame in a way, that it takes all the space from myView's left to right and just set the text alignment to NSTextAlignmentCenter.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, self.myView.frame.size.width, 46)];
label.textAlignment = NSTextAlignmentCenter;
Or calculate the top left corner accordingly and set the frame upon initialization. Use the center position and substract half of the width. Or use the full with of myView, substract the with and then devide by 2.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.myView.center.x-92/2, 25, 92, 46)];
or
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((self.myView.frame.width-92)/2, 25, 92, 46)];
Try to set the proper x-position as view.center.x - (labelwidth/2). Also set the textAlignment to center.
Using CGPointMake set center of label like this.
lblAppTitle.center= CGPointMake(self.view.bounds.size.width / 2, 25);
[lblAppTitle setTextAlignment:NSTextAlignmentCenter];
Try this
int x=(self.myView.frame.size.width-92)/2;
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(x, 25, 92, 46)];
lbl.textAlignment=UITextAlignmentCenter;
Try this:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 92, 46)];
label.center = CGPointMake(label.bounds.size.width / 2, 25);
or this:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.myView.center.x - 46, 25, 92, 46)];
Set it's center rather than setting rect
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, 92, 46)];
label.center = self.myView.center;
//Also if your frame is bigger than text then you can set this also
label.textAlignment = UITextAlignmentCenter;

Resources