Clipping of UILabel when adjustsFontSizeToFitWidth - ios

When I add text to a label with the adjustsFontSizeToFitWidth set to YES the text is no longer centred vertically and eventually clips the text at the bottom of the label frame. For a large amount of text it will eventually disappear off the bottom of the label.
This is what happens if you add less text:
This is clipped as I would expect it (i.e. the font size did not reduce, the text was vertically centred in the label and clipped on the top and bottom.
Here is the code to reproduce:
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor blueColor];
testLabel = [[UILabel alloc] init];
testLabel.font = [UIFont boldSystemFontOfSize:172];
testLabel.textColor = [UIColor blackColor];
testLabel.adjustsFontSizeToFitWidth = YES;
testLabel.numberOfLines = 1;
testLabel.frame = CGRectMake(50, 50, 300, 100);
testLabel.text = #"123";
[self.view addSubview:testLabel];
}
Should this happen? And how do I get my label to centre vertically irrespective of the number of characters in my label.

Add
testLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
to your code to vertical-center the text on font scale.

Would also like add that adjustsFontSizeToFitWidth doesn't work too well with attributed text, so add your attributes to the label instead of the attributed text if you can. This worked for me.

Related

UIScrollView not working like most scroll views

I am trying to make my scroll view get to the bottom of my text without having to 'force scroll'. So when I scroll down, the scroll bar at the right stops before I reach the bottom portion of the remaining text. I have to then scroll again with force (so that the scroll bar at the right shrinks, revealing the remaining text). Hopefully someone has encountered this. I don't want to force scroll. I want it all displayed in a clean way.
UIScrollView *theScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 568.0f)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(17.0f, 150.0f, 286.0f, 568.0f)];
label.text = #" a very large portion of text ";
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:#"Chalkduster" size:16.0];
label.lineBreakMode = NSLineBreakByWordWrapping;
label.numberOfLines = 0;
[label sizeToFit];
[theScroll setShowsVerticalScrollIndicator:NO];
theScroll.contentSize = CGSizeMake(theScroll.contentSize.width, label.frame.size.height);
[theScroll addSubview:label];
[self.view addSubview:theScroll];
This is all in the viewDidLoad method.
Two things don't quite seem right.
You set the label's origin.y to 150. Did you mean to have the label start so far from the top of the scroll view's content area?
You set the scroll view's contentSize.height to be the same as the label's height. This conflicts with #1. The contentSize.height should be tall enough for all of the content.
Assuming #1 is correct, #2 should be set as:
theScroll.contentSize = CGSizeMake(theScroll.contentSize.width, CGRectGetMaxY(label.frame));
Remember, the bottom of the label will be its origin + its height, not just its height.

Custom multiline UILabel with black background behind text

I need to create custom multiline UILabel with black background behind text, with some pading of 2 pixel from left, right, top and bottom. Padding must be same for all side, let say if padding between [Songs.. and black border = 2 then I need 2pixel padding between "Perfect" and right border, same after "mp3"
I tried this:
[string addAttribute:NSBackgroundColorAttributeName
value:[UIColor blackColor]
range:NSMakeRange(0, string.length)];
but its not giving me extra padding.
Could you guys suggest me any library or some hint will also be helpful
I would use a UITextView rather than a UILabel. Do something like the following:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, y, self.view.frame.size.width - 20, 40)];
textView.textAlignment = NSTextAlignmentJustified;
textView.scrollEnabled = NO;
textView.backgroundColor = [UIColor blackColor];
textView.userInteractionEnabled = NO;

UITextField sizeToFit leaves right margin

I have a UITextField that I'm trying to center in a UIView. However, when I call sizeToFit it leaves a small right margin in the textField which causes the text to be slightly off center when the textField background is clear. I've colored it here so you can see what I'm talking about:
messageField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
messageField.delegate = self;
messageField.font = [UIFont systemFontOfSize:15];
messageField.textColor = [UIColor darkGrayColor];
messageField.textAlignment = NSTextAlignmentLeft;
messageField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"not fitting right" attributes:#{NSForegroundColorAttributeName: [UIColor darkGrayColor]}];
[messageField layoutSubviews];
[messageField sizeToFit];
messageField.backgroundColor = [UIColor lightGrayColor];
And the textfield looks like this (I would post a picture but need more reputation points, this is basically the same though):
not fitting right
Any help would be greatly appreciated!
I should not that if I create a UILabel of the same font size and text, call sizeToFit on the label and then assign the UITextField frame to be the same as the UILabel, it works like a charm.

Vertical Shift UILabel Text Without Affecting Background

I've been trying to figure out how to vertically align my UILabel text to the top or bottom. The problem is, I have an image behind it which also gets shifted with the text.
Is there a way to keep the background color/image in place, but only shift the text up or down?
Here's the current code:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (!self) return nil;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,
0,
frame.size.width,
frame.size.height)];
label.textAlignment = NSTextAlignmentLeft;
label.font = [UIFont fontWithName:#"AvenirNext-Bold" size:12];
label.backgroundColor = [UIColor clearColor];
[self addSubview:label];
self.numberLabel = label;
self.layer.cornerRadius = 3;
return self;
}
I've been following this tutorial, but it shifts everything, including my label background.
Vertically align text to top within a UILabel
find the height of the text.
Use another view to show image.
Set frame of label relative image view with height of the text and whatever position you want to set for label.
ON the label if you're not using auto layout use:
[YOUR_LABEL sizeToFit];
This will resize your label to fit the text.
What is the background image ? A separate UIImageView ? How are you setting this image ?

Positioning UILabel inside a UITextView

Trying to position a UILabel above some text, but the elements are "stepping" on each other. I positioned them with x,y,h,w such that they should not do this but alas, they are. You can see in the screenshot that the label text "Abalone" and the text are commingled. What changes do I need to make here? I've already tried changing the x,y,w,h values to no great effect...
txtView = [[UITextView alloc] initWithFrame:CGRectMake(10,20,280,300)];
[txtView setFont:[UIFont fontWithName:#"ArialMT" size:14]];
txtView.text = word.definition;
txtView.editable = NO;
txtView.scrollEnabled = YES;
[txtView scrollRangeToVisible:NSMakeRange([txtView.text length], 0)];
txtView.minimumZoomScale = 1;
txtView.maximumZoomScale = 10;
UILabel *wordTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 20)];
wordTitle.text = word.term;
[txtView addSubview:wordTitle];
[detailsViewController.view addSubview:txtView];
I think the problem is you are adding the label view as a subview to UITextview. You have to reserve some space for the label view in the UITextview. You can either add some padding to the textView, which can be confusing, because padding in UITextView is not constant and varies based on the font size. Or you can add your UILabel directly to the detailsViewController.view and start your UITextView below the label.
Hope it helps.

Resources