UIButton Title only displays half of it - objective-c

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]

Related

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

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)];

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

How to find the position of the last text line in a multiline UILabel or otherwise have UILabel have 0 padding

I have a UILabel that has both -numberOfLines set to 3 and text-size auto shrink and I need to align another UIView to this UILabel's last line of text. That is, I might need to align to the y position of line 0, 1 or 2, depending on the text inside the label (and the distance between these lines of text may vary depending on whether the text is long enough that it triggered font resizing).
But:
UILabel doesn't expose a contentSize
the label's bounds extend past the last line of text (there seems to be a content inset), so aligning to the bounds won't work.
subclassing UILabel and doing something like this:
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0., 0., -30., 0.};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
just happens to work for the case where I have 3 lines and the font size was auto shrunk, but I still can'r figure out a generic way of subtracting insets for the general case, regardless of text size. And I don't seem to be able to use -boundingRectWithSize:options:context: either: it either returns a single line equivalent rect or, If I play around with the options, a a rect the same size of the original label (that is, including the extra insets I'm trying to get rid of). Mind you, the idea behind removing any insets is that if I have no way of knowing where the last line of text is, at least I can remove any insets in the label so that the last line of text aligns with the label's bounds.origin.y + bounds.size.height.
Any thoughts?
I don't know if the problem was that originally I was using boundingRectWithSize on non-attributed text or what but now this seems to work:
NSString *text = <get text from CoreData>;
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:#{NSFontAttributeName: self.titleLabel.font}];
CGRect rect = [attributedText boundingRectWithSize:self.titleLabel.frame.size
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
if (!rect.size.height || rect.size.height > self.titleLabel.frame.size.height) {
attributedText = [[NSAttributedString alloc] initWithString:text attributes:#{NSFontAttributeName: [UIFont boldSystemFontOfSize:self.titleLabel.font.pointSize * self.titleLabel.minimumScaleFactor]}];
rect = [attributedText boundingRectWithSize:self.titleLabel.frame.size
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
}
self.titleLabel.frame = rect;
self.titleLabel.attributedText = attributedText;
While this doesn't really find the position of the bottom of the last line of text in the UILabel (the label still adds some padding at the bottom... not sure if to account for descenders), it adjusts the label's bounds close enough to the bottom that I can at least align based on bounds.origin.y + bounds.size.height and it looks good enough.

Changing the size of UIStepper

I can't seem to change the size of UIStepper:
In IB, the Width and Height boxes are grayed out.
I used initWithFrame:
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(300, 638, 120, 80)];
But it does not change the size. Several posts on SO seemed to implied it is changeable. Any suggestion?
UIStepper* s = [UIStepper alloc] init];
s.transform = CGAffineTransformMakeScale(0.75, 0.75);
You can properly update the UIStepper size without transformation.
Use the following method to set the background image and the stepper will draw itself using the background size:
- (void)setBackgroundImage:(UIImage*)image forState:(UIControlState)state
Example
[self.stepper1 setIncrementImage:[UIImage imageNamed:#"plusIcon1.png"] forState:UIControlStateNormal];
[self.stepper1 setDecrementImage:[UIImage imageNamed:#"minusIcon1.png"] forState:UIControlStateNormal];
[self.stepper1 setBackgroundImage:[UIImage imageNamed:#"stepperBkg1.png"] forState:UIControlStateNormal];
[self.stepper1 setBackgroundImage:[UIImage imageNamed:#"stepperBkgHighlighted1.png"] forState:UIControlStateHighlighted];
[self.stepper1 setBackgroundImage:[UIImage imageNamed:#"stepperBkgDisabled1.png"] forState:UIControlStateDisabled];
This yields the following result on the left, compared to an unmodified stepper on the right:
stepperBkg1#2x.png:
stepperBkgHighlighted1#2x.png:
I tried the transform on my stepper - it did change the appearance and did scale it, however, the images of the + and - were stretched (so you have to scale in proportion to the original stepper.
Also, be careful because the area of touch that actually increments and decrements, does change - so on the stretched image, the button would not decrement along the entire view - so this is probably not a good solution....
from the doc:
The bounding rectangle for a stepper matches that of a UISwitch object.
Doesn't sound, like it is possible upfront.
Also in this blog post:
// Frame defines location, size values are ignored
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(120, 20, 0, 0)];
But you can try to transform it's layer.
You can provably scale it:
stepper.transform = CGAffineTransformMakeScale(1.75, 1.0);
I've made a small custom UIStepper class for it. No images needed, no transformation needed. Images generated automatically.
https://github.com/alelipona/VZCustomSizeStepper
Yes, you can change size of stepper.
first, right click on storyboard --> select (open as)--> Select (Source Code)
then find stepper in the code--> find width=??? and change.
then click on storyboard again and select open as interface builder.

UILabel font size?

I can't seem to modify the font size of a UILabel with the following code:
itemTitle.font = [UIFont systemFontOfSize:25];
As i increase the number 25 to something greater, it seems to only add a top margin to the label, which consequently pushes the text down so much, so that the text gets cut off at the bottom or completely overflows.
i have another UILabel elsewhere with systemFontOfSize 25, and it's much smaller than the itemTitle text. What's going on? Isn't 25 supposed to be an absolute value?
i am so confused on how to programmatically change font size of uilabels.
I have modified the UILabel by following code:
label.font=[label.font fontWithSize:25];
Try this and see whether is it working in your case or not???
Check that your labels aren't set to automatically resize. In IB, it's called "Autoshrink" and is right beside the font setting. Programmatically, it's called adjustsFontSizeToFitWidth.
[label setFont:[UIFont systemFontOfSize:9]];
this works for me.
For Swift 3.1, Swift 4 and Swift 5, if you only want change the font size for a label :
let myLabel : UILabel = ...
myLabel.font = myLabel.font.withSize(25)
**You can set font size by these properties **
timedisplayLabel= [[UILabel alloc]initWithFrame:CGRectMake(70, 194, 180, 60)];
[timedisplayLabel setTextAlignment:NSTextAlignmentLeft];
[timedisplayLabel setBackgroundColor:[UIColor clearColor]];
[timedisplayLabel setAdjustsFontSizeToFitWidth:YES];
[timedisplayLabel setTextColor:[UIColor blackColor]];
[timedisplayLabel setUserInteractionEnabled:NO];
[timedisplayLabel setFont:[UIFont fontWithName:#"digital-7" size:60]];
timedisplayLabel.layer.shadowColor =[[UIColor whiteColor ]CGColor ];
timedisplayLabel.layer.shadowOffset=(CGSizeMake(0, 0));
timedisplayLabel.layer.shadowOpacity=1;
timedisplayLabel.layer.shadowRadius=3.0;
timedisplayLabel.layer.masksToBounds=NO;
timedisplayLabel.shadowColor=[UIColor darkGrayColor];
timedisplayLabel.shadowOffset=CGSizeMake(0, 2);
This worked for me in
Swift 3
label.font = label.font.fontWithSize(40.0)
Swift 4
label.font = label.font.withSize(40.0)
very simple, yet effective method to adjust the size of label text progmatically :-
label.font=[UIFont fontWithName:#"Chalkduster" size:36];
:-)
This worked for me:
sequencerPlayLabel.font = [UIFont fontWithName:kTypeFont size:kTypeFontSize];
-rich
Answers above helped greatly.
Here is the Swift version.
#IBOutlet weak var priceLabel: UILabel!
*.... lines of code later*
self.priceLabel.font = self.priceLabel.font.fontWithSize(22)
In C# These ways you can Solve the problem, In UIkit these methods are available.
Label.Font = Label.Font.WithSize(5.0f);
Or
Label.Font = UIFont.FromName("Copperplate", 10.0f);
Or
Label.Font = UIFont.WithSize(5.0f);
Try to change your label frame size height and width so your text not cut.
[label setframe:CGRect(x,y,widht,height)];