How to top align the text in UITableView cell? - ios

I want to top align the text in tableView cell.I created a cell subclass and added the following in layout subview.However it doesn't work.What am I missing?
- (void)layoutSubviews {
[super layoutSubviews];
CGRect newFrame = self.textLabel.frame;
newFrame.origin.y = CGRectGetMinY (self.contentView.bounds);
[self.textLabel setFrame:newFrame];
}

Basically what you are doing is putting the UILabel at high place in the cell, but the labels height might still be big, and as a result the text is not at the top.
Vertical align in UILabel is a problem. By default a text in UILabel is center vertical aligned. If you want to see the text at the top you will have to change the label height to fit the size:
[self.textLabel sizeToFit];
// Will keep the same X and Y so label will look like it moved
// to the upper left corner of it's frame.

Related

How to break line of a UILabel dynamically with text?

I have a UILabel and I want to show some text in this label. I want to increase the label width at most 70% of the full screen of my device. If text length of that label doesn't fit this 70% of size then the label automatically goes to the next line as long as the text length. Every time the label length cross the 70% width of main screen then lines break as well. I have tried several ways but unable to solve yet. Please help me to solve this.
Thanks in advance;
Drag a label to your storyboard and add top and leading constraints to it.
Now select the label and control drag to the view holding the label (in your case view of ViewController) you will see the pop up and then select equal width
Now your Label's width is equal to your view's width :) That's not you want you want ur label width to be 70% of your view. So select the equal constraint of label, go to property inspector and change the multiplier to 0.7
Now your label width is 70% of your view!
But you don't want it to be 70% always. It can be at max 70% of screen, so
now change the relationship of constraint from being equal to less than or equal to.
select label and change number of lines to 0.
That's it :) have fun :)
Sample O/P:
When text is short - vs - long:
- - -
EDIT:
Not using a storyboard? Not a problem; write the same constraint programmatically and apply it to label simple enough. If you need help lemme know :)
EDIT:
As you have specified that you want to leave the gap at the beginning of each line in label you can achieve it by using Edge insets
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 0};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
You must have forgotten to increase the label's height.
The code below is for allowing the UILabel to have multiple lines:
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
Then you have to make sure the UILabel's frame has enough height to show the lines. You can achieve this by calculating the required height for the given text (NSString):
NSString *text = #"YourText";
CGFloat your70Width; // whatever your width is
CGSize constraintSize = CGSizeMake(your70Width, MAXFLOAT);
UIFont *yourLabelFont; // whatever your font is
CGRect requiredFrame = [text boundingRectWithSize:constraintSize options:NSStringDrawingUsesFontLeading attributes:#{NSFontAttributeName:yourLabelFont} context:nil];
// Keeps the old x,y coordinates and replaces only the width and height.
CGRect oldLabelFrame = label.frame;
label.frame = CGRectMake(oldLabelFrame.origin.x, oldLabelFrame.origin.y, requiredFrame.size.width, requiredFrame.size.height);
Now the label will be shown nicely in multiple lines.
To increase the height of the label according to the content if you are using storyboard. Give the label FOUR constraints (Top, Bottom, Leading, Trailing) then go to Attribute Inspector and make lines to 0 and in line break do it WORD WRAP.

how make the UIView height equal to its subview's additional height, so that it can easily scroll till the end

I have a situation where i have following view layout.
-UIViewcontroller
-SCrollView
-UIView(outer)
-buttons
-labels
-UIView(inner)
-labels
-buttons
-buttons
the inner UIView height can be any longer as content inside is dynamically added.
So the main issue is i can not scroll till the end of the content, instead i can only scroll till the height of inner UIView.
NOTE - I am using auto layout
Any help is much appreciated.
Thanks
You should change the frame of your inner view dynamically based on the content you are adding. According to your inner view hierarchy, you have label and button. You might be setting some dynamic text on label and also doing some manipulation with button . Use the following method to get the height of the text ,
- (CGSize)getHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{ NSFontAttributeName:font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height);
}
return size;
}
Here font is the font you set to button and label. Call this method by passing the text added on label and button. CGSize has a height property , add height value of label and button. Now set this sum height to innerView by calling innerView.frame.size.height = <height>. This increase the height of your innerView based on content inside that.
Put all your buttons in the bottom inside a container view and add the following constraints to the outerView (assuming that the new container you've added is called "buttonContainer"):
A vertical spacing constraint between the bottom of innerView and the top of the buttonContainer.
A vertical spacing constraint between the bottom of the buttonContainer and the bottom of the outerView.
Horizontal spacing constraints between between the buttonContainer and outerView to force it to full width.
When using AutoLayout, you have to have enough constraints to properly define the contentSize of the scrollView.
For more information about using AutoLayout with UIScrollView, check out the following links by Apple:
Working with Scroll Views
UIScrollView and Autolayout

Scroll the text in UITextview when texts are less and height of UITextview is large

I have one UITextView whose height is 568 (means fit in screen size) and if i write 50 lines in it then it will definitely scroll vertically.
But i want to scroll (or just bounce) when there is only one line of text.
When user scroll Vertically then the text will just bounce in UITextView.
in my application the UITextView is not Editable.
Any idea, code,link will be great help...
EDIT:
The UITextView's height will be that 568 only.
it will not change (means without changing the height of UITextView, set this thing).
[txtView setContentOffset:CGPointMake(0, 1000)];
It is not working too...
If you want to scroll TextView text that not Larger then it height . then you can't do this. you wont getting scroll bar if your textView's Text not larger then its frame height.
Setting [txtView setContentOffset:CGPointMake(0, 1000)]; wont work anymore if that data in not longer then textView's height.
UPDATE:-
Here it is i got one trick. If textview not editable and you just want to scroll it try to add your Textview in to one scrollviw. Like Bellow i got this working.
in .h class
#property(nonatomic,retain)IBOutlet UITextView *textvie;
#property(nonatomic,retain)IBOutlet UIScrollView *sceoll;
in .m class
- (void)viewDidLoad
{
[sceoll setContentSize:CGSizeMake(0, 500)];
[textvie setTextAlignment:NSTextAlignmentCenter];
textvie.editable = FALSE;
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
textvie.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
} else {
textvie.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
}
[super viewDidLoad];
}
in xib
and the result is:-
Set the frame of UITextView
CGRect frame = textView.frame;
frame.size = textView.contentSize;
textView.frame = frame;
See: UITextView change height instead of scroll

Width of multiline UILabel in UITableView keeps shrinking with autolayout

I have a UILabel functioning as a title in my UITableViewCell. It displays fine the first time it is used, but each time it is redisplayed the width shrinks. All my layout is done in IB, nothing in code at the moment.
[EDIT]
The left hand side of the label is pinned to the (barely visible) box that says "New Words". The right hand side is pinned to the superview. Height is set to 44px.
I am able to fix this by using the following code in layoutSubviews. I don't like it at all though since it involves setting the frame. I would like to understand the underlying problem.
[super layoutSubviews];
CGRect frame = self.titleLabel.frame;
CGSize size = self.titleLabel.intrinsicContentSize;
frame.size.width = size.width;
frame.size.height = 44;
self.titleLabel.frame = frame;
[super layoutSubviews];
Here are a few screenshots of the problem:
First time:
Second time:
Third+ time:

In iOS - How do I fit a UILabel to its text, without changing its position?

I'm calling sizeToFit on a UILabel which has right aligned text in it. It shrinks the height and width of the UILabeland fits the text to the top left of the UILabel.
Now...the position of the UILabel is incorrect. How can I make the UILabel stay in its original position (right aligned) or move it so it'll appear at its original position?
Once again - the problem is that the sizeToFit method is shrinking the width from the right side of the UILabel. It is treating the UILabel text as left aligned. But my text is right aligned. So, the right side border of the UILabel is where my text begins.
#implementation UILabel (Additions)
- (void)sizeToFitWithAlignmentRight {
CGRect beforeFrame = self.frame;
[self sizeToFit];
CGRect afterFrame = self.frame;
self.frame = CGRectMake(beforeFrame.origin.x + beforeFrame.size.width - afterFrame.size.width, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
}
#end
You check the widths before & after, then move the center of the view right by the difference / 2.
By the way, I think the "box" (frame) is shrunk in both directions, keeping the center intact.

Resources