how to programmatically align a label to an x, y coordinate - ios

below are some other properties of the label. obviously nstextalignmentleft is not what i'm going for. having trouble understanding where to enter coordinates.
self.lblTimer = [[UILabel alloc] init];
self.lblTimer.translatesAutoresizingMaskIntoConstraints = NO;
self.lblTimer.textAlignment = NSTextAlignmentLeft;
self.lblTimer.font = [UIFont systemFontOfSize:10];
self.lblTimer.textColor = [UIColor redColor];`self.lblTimer = [[UILabel alloc] init];

I think you're misunderstanding what textAlignment does. This simply controls the alignment within the label, not on the screen. If you want to position the label on screen you must change its frame property:
self.lblTimer.frame = CGRectMake(x,y,width,height);

As you've set self.lblTimer.translatesAutoresizingMaskIntoConstraints to NO I am guessing you want to use Auto Layout and if so, I highly recommend taking a look at Masonry.
An alternative is to set the frame property on the label with requires a CGRect where you give it the x and y coordinates as well as the width and height.

Related

How to set UILabel height dynamically in Objective c?

I am new in iOS and I am facing problem regarding to create UILabel dynamically. I need to change its hight and the Y position according to data from webservice.
My code is like this
NSMutableArray *remarkarray =[[NSMutableArray alloc] init];
remarkarray=[responsedict valueForKey:#"Key"];
NSString *RemarkString=[NSString stringWithFormat:#"%#",[remarkarray objectAtIndex:0]];
remarklbl.text=RemarkString;
remarklbl.numberOfLines=0;
[remarklbl sizeToFit];
I have create UILabel like this
But its Y Position is fix. I need to create a UILabel in which I can change the height and the Y position according to text in it. Like this
Thanks in Advance!
You can do it very easily by declaring two labels in Storyboard.
Here are steps.
Drag two UILabels in your viewController and select the first one and set constraints like below image.
Select the second label and add following constraints.
Select both labels and set numberOfLines to zero.
Now u can set text dynamically from code.
All you have to do is:
float x=10.0,y=10.0,height=40;
float verticalSpace=20.0;
for (int i = 0; i < 10; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(x, y,50,height)];
label.text = #"dummy text";// It could be from array as well if you want to add different text for each label. Eg: label.text = [arr objectAtIndex:i];
[self.view addSubview:label];
y = y +height+ verticalSpace;
}
You could search around with key word "auto layout dynamic label height"

iOS API - sizeThatFits: does not work

UPDATE:
Sorry I didn't describe it enough and incorrectly before.
I did some code like this:
UIView *view = [[UIView alloc] initWithFrame:someFrame];
CGSize size = [view sizeThatFits:CGSizeMake(100, 100)];
But when I observe size, it got nothing: {0, 0}.
Do I use it wrong?
Even I passed in an initial frame to my view, the size still returned nothing.
I searched some posts and someone said UILabel may also gets affected.
Your view does't contain any content and it does't have any intristic content size, so it will have zero size.
Try these and you will see that it works:
UILabel *label [UILabel new];
label.text = #"test test test test";
CGSize size = [label sizeThatFits:CGSizeMake(20, CGFLOAT_MAX)];
As a #Konstantin said:
Your view does't contain any content and it does't have any inartistic
content size
So try to use this code
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
CGSize size = view.size;
Check the description of sizeThatFits, it states,
Asks the view to calculate and return the size that best fits the
specified size. A new size that fits the receiver’s subviews.
So, your view should have some subview or content otherwise it will return 0,0.

iOs 8 seems to be ignoring my UILabel's X and Y coordinates

I have a somewhat old Objective C function that adds a UILabel to my UIView. It keeps track of the last label's Y in the variable _nextFieldY so it can put the next label below it. When I'm adding the label, I init it with its Width, Height, X and Y, and add it to my view. In iOs 7, this works, and correctly puts everything in its place. However, with any devices running the same app in iOs 8+, it appears the labels are all put at (0,0); the labels are all on top of each other in the top left corner. Was there a change between these versions that would cause this? I usually avoid programmatically adding things to my XIBs, so I don't know if there's certain work I have to do to get them to behave.
CGFloat labelWidth = 100;
CGFloat labelHeight = 40;
CGFloat labelY = _nextFieldY-REG_FORM_PADDING_Y;
UIFont *labelFont = [UIFont boldSystemFontOfSize:12.0f];
UILabel *currLabel = [[UILabel alloc] initWithFrame:CGRectMake(REG_FORM_OFFSET_X, labelY, labelWidth, labelHeight)];
currLabel.text = label;
[currLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[currLabel setFont:labelFont];
[currLabel setTextAlignment:NSTextAlignmentLeft];
[_formView addSubview:currLabel];
_nextFieldY += currLabel.frame.size.height + REG_FORM_PADDING_Y;
Since you're setting the frame directly to position your view, you should be leaving translatesAutoresizingMaskIntoConstraints set to YES. Setting it to NO tells the system that it can ignore the frame you set.

Unable to calculate height of text area in iOS

I am using the ios library UIPlaceHolderTextView in my application: https://github.com/JohnnyZeng/UIPlaceHolderTextView which is linked to the following question:
Placeholder in UITextView
The library itself is working fine, however, I need to calculate the height of the area after the user has entered text, and unfortunately, I am always getting a value of 0.00000 despite the fact that the text area contains text. Why is this? Here is my relevant code:
//self.textView is a reference to a UIPlaceHolderTextView object, and text is its attribute
self.textView.text = #"Text that the user has entered, and can be of any length";
float height = MIN(175, [self.textView.attributedText boundingRectWithSize:CGSizeMake(self.textView.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:NULL].size.height);
NSLog(#"textview height is: %f", self.textView.frame.origin.y);
NSLog(#"textview height is: %f", self.textView.frame.size.height);
The output in both lines are 0.00000. Can anyone see why this is happening? My guess is that the problem lies in the expression self.textView.attributedText, but not sure if I'm correct, and if so, what I should do instead.
you are assigning
`self.textView.text = #"Text that the user has entered, and can be of any length";`
and calculating heightfor self.textView.attributedText which is nil i guess please do appropriate changes. It should work.
To make is work you should assign value to self.textView.attributedText
Use a local textview of same width as real textview and set the same text with sizeToFit property.it will be returned in correct frame.
UITextView *tv = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
//whatever alignment of real text view you have set
tv.textAlignment = NSTextAlignmentJustified;
tv.text= #"Text that the user has entered, and can be of any length";
[tv sizeToFit];
float height = tv.frame.size.height;

Text Alignment issue when I set the custom font

When I set the custom font for the segmented control then it changes the vertical text alignment. I am using below code to set the font .
// I dont think these lines are creating any issue but just wanted to paste all the code
self.segmentType.layer.borderColor = navigationTintColor.CGColor;
self.segmentType.layer.cornerRadius = 0.0;
self.segmentType.layer.borderWidth = 1.5;
// These are the lines that are changing the text alignment
UIFont *font = [UIFont fontWithName:ftHelveticaNeueLTPro_Th size:13.5];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
forKey:UITextAttributeFont];
[self.segmentType setTitleTextAttributes:attributes
forState:UIControlStateNormal];
Here is the screenshot of whats is happening . If you observer, the text is not vertically centre aligned .
Please help me . Thank you in advance !!
The below code suggested by #Emmanuel works perfectly fine. You can change the vertical offset to align the text vertically at the center .
[self.segmentType setContentPositionAdjustment:UIOffsetMake(0, 2) forSegmentType:UISegmentedControlSegmentAny barMetrics:UIBarMetricsDefault];
Can you please try it using custom UILabel on custom view on it. Change & modify frame value of either titleLabel or customSegmentView as per convenience on actual view. And add this whole view as subview on your segmented control.
UIView *customSegmentView = [[UIView alloc] init];
UILabel *segmentTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 7.0f,180.0f,22.6f)];
segmentTitleLabel.text = #"your-text";
segmentTitleLabel.backgroundColor = [UIColor clearColor];
segmentTitleLabel.textAlignment = UITextAlignmentCenter;
segmentTitleLabel.font = [UIFont fontWithName:#"ftHelveticaNeueLTPro_Th" size:13.5f];
customSegmentView.frame = CGRectMake(60, 20, 180, 35);
[customSegmentView addSubview:segmentTitleLabel];
[self.segmentType setTitleView:customSegmentView];
Hope that will work for your issue. Please check and let me know if we have to go with another solution.
In InterfaceBuilder on XCode 6 there is a Content Offset control for the segments, which affects the baseline of the text. I had this problem because my Content Offset was 2 in the Y dimension instead of 0.

Resources