How do you vertically align UILabel text to the top now that sizeWithFront is deprecated? ios7 - cocoa-touch

It's been EXTREMELY frustrating trying to vertically align UILabel text to the top. I've search google for hours, and everything I've found has been deprecated.
What is the best way to do this? I've used numberOfLines=0 & sizeToFit... but this doesn't work upon initial loading of the UILabel on a custom TVCell.
Does anyone have a code snippet that works for align UILabel text to the top on a custom cell?

Calculate height of text and set UILabel frame again.
try this-
CGSize maxSize = CGSizeMake(label.frame.size.width, label.frame.size.height);
CGSize labelSize = [label.text boundingRectWithSize:maxSize
options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:label.font}
context:nil].size;
[label setFrame:CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, labelSize.height)];

Related

sizeWithFont:constrainedToSize doesn't work with custom font

I'm trying to resize a UITextView to the size the text within it.
The problem is that Im using a custom font and it the text doesnt fit within the UITextView.
NSString *textToFit = #"pretty long text";
UIFont *customFont = [UIFont fontWithName:#"Museo-100" size:15];
CGSize sizeText = [textToFit sizeWithFont:customFont constrainedToSize:CGSizeMake(textFrame.size.width, 1000)];
Where textFrame is the frame of the UITextView I want to adjust its height.
Im trying different fonts and also different files of the same font and still it never adjusts its height to the height that the text fills.
I've been searching and I dont find a solution. I've tried a workaround using a UILabel and the method textRectForBounds, but still no success, something on this lines.
UILabel *auxLabel = [[UILabel alloc]init];
auxLabel.numberOfLines = 0;
auxLabel.font = [UIFont fontWithName:#"Museo-100" size:15];
auxLabel.text = //THE TEXT I WANT TO FIT IN
CGRect textSize = CGRectMake(0.0, 0.0, textDescription.frame.size.width, FLT_MAX);
CGRect frame = [auxLabel textRectForBounds:textSize limitedToNumberOfLines:0];
I think
UIView : sizeToFit
Should solve your problem.
sizeToFit Resizes and moves the receiver view so it just encloses its
subviews.
Discussion: Call this method when you want to resize the current view so that it uses the most appropriate amount of space.
Specific UIKit views resize themselves according to their own internal
needs. In some cases, if a view does not have a superview, it may size
itself to the screen bounds. Thus, if you want a given view to size
itself to its parent view, you should add it to the parent view before
calling this method.
https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instm/UIView/sizeToFit

UIButton Title only displays half of it

I've a UIButton that has a custom FONT for the titleLabel attribute.
For some reason on iOS 6.0 it shows only half of the title. I tried increasing the height of the title. but that didn't work.
What am i missing?
Is this my only option?
btn.titleLabel.font = [UIFont fontWithName:#"Frutiger95-UltraBlack" size:17];
This is how it should look (minus the color change)
Single line labels have a low content compression resistance priority on the vertical axis. So when you increase the font size, they don't increase the height of their intrinsicContentSize. Setting the compressionResistancePriority to UILayoutPriorityDefaultHigh or UILayoutPriorityRequired should fix it.
[btn.titleLabel setContentCompressionResistancePriority:UILayoutPriorityRequired
forAxis:UILayoutConstraintAxisVertical];
I think updating the label is enough, but you may need to increase the priority on the button itself as well.
[btn setContentCompressionResistancePriority:UILayoutPriorityRequired
forAxis:UILayoutConstraintAxisVertical];
I think you need to set button title nil, then make your own custom UILabel
and add that label on your button like my example given below-
UILabel *lblloginbtntitle=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, btnLogin.frame.size.width, btnLogin.frame.size.height)];
lblloginbtntitle.text=NSLocalizedString(#"Login", nil);
lblloginbtntitle.textAlignment=NSTextAlignmentCenter;
lblloginbtntitle.textColor=[UIColor whiteColor];
lblloginbtntitle.font=[UIFont fontWithName:#"AvenirNextLTPro-Regular" size:20];
[btnLogin addSubview:lblloginbtntitle];
[btnLogin setTitle:#"" forState:UIControlStateNormal]

size of the NSString to fit on UItextview

i want to find the size of NSString to display on UITextview. It is working good for English. But if the string is other languages like spanish, hindi, japanese means it is working slow:
CGSize ContentSize=[MainContentString
sizeWithFont:TextView.font
constrainedToSize:CSize
lineBreakMode:UILineBreakModeWordWrap];
Is there a way to do it faster?
There is actually a very easy way to do resizing of the UITextView to its correct height of the content. It can be done using the UITextView contentSize.
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
Check out UIView's autoresizesSubviews and contentMode - play with them in the storyboard.
Try it both with and without your implementation - see if it works.

Dynamically Resizing a CGRect based on UITextView

Okay I'm not really sure how to explain this. But here goes. I have a UITextView with content that is dynamically populated. I have worked out how to resize automatically depending on the amount of text within it. It could be one line it could be 10.
Now I have a UIView that I have customised which has a rounded Rect code below.
The .h file
#interface roundedEdges : UIView
{
}
The .m file
- (void) drawRect:(CGRect)rect
{
CGRect frame = self.bounds;
UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:frame cornerRadius:8.0];
[[UIColor whiteColor] setFill];
[path fill];
}
What I would like to do is make the rounded UIView to be constrained to the height of the UITextView.
Currently the UITextView is sitting on top of the UIView. This way it provides a rounded box effect.
IS there a way to resize the above code (UIView) to be constrained depending on how much text is coming into the UITextView?
If so how?
Thanks in advance!
Jeremy
To dynamic change in height or width of textView or label depends upon the text size this code is working fine........
I have used this code for both UITextView and UILabel
CGFloat heightTXT = [[txtView text] sizeWithFont:txtView.font constrainedToSize:CGSizeMake(txtView.frame.size.width,INFINITY) lineBreakMode:UILineBreakModeWordWrap].height;
[txtView setFrame:CGRectMake(txtView.frame.origin.x,txtView.frame.origin.y,txtView.frame.size.width,heightTXT];

Different opacity for UILabel and the text of the UILabel

I have a UILabel whose opacity has been changed to blank by using [UIColor clearColor]
I now want to change the opacity of the text within the label to the full possible level.
How do I do it?
Use the following code to change the opacity of UILabel text
[headerLabel setAlpha:0.5];
Happy coding..:)