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.
Related
Hi i am beginner in ios and i am trying to insert UItextfield "text" data exactly at center of the y-axis as like below textfield image
And i would like to insert UIbutton "titleLabel text" exactly center of x-axis and center of y-axis as like below button image for this i tried lot but i did not get result please help me some one
my code:-
[self.TripIdTxt setValue:[UIColor blackColor] forKeyPath:#"_placeholderLabel.textColor"];
self.TripIdTxt.layer.sublayerTransform = CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f);
self.TripIdTxt.backgroundColor = [UIColor whiteColor];
self.TripIdTxt.layer.cornerRadius = 3.0f;
self.TripIdTxt.layer.masksToBounds = YES;
[self.TripIdTxt setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[self.TripIdTxt setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
self.TripIdTxt.textColor = [UIColor blackColor];
self.TripIdTxt.font = [UIFont fontWithName:bitter_Regular size:14];
set
for UIButton
yourbtnName.titleLabel.textAlignment = NSTextAlignmentCenter;
for y-axis
[yourbtnName setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
for X-axis
[yourbtnName setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
for UITextFields
yourtextfieldName.textAlignment = NSTextAlignmentCenter;
diectly you can access in storyboard like
for additional information see this link
Padding with TextField
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
TripIdTxt.leftView = paddingView;
self. TripIdTxt.leftViewMode = UITextFieldViewModeAlways;
or else use Transform
TripIdTxt.layer.sublayerTransform = CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f);
Set the text alignmaent center of your lable property .
mainlabel.textAlignment = NSTextAlignmentCenter;
u set its alignment using xib by
u can also set its vertical alignment using xib by
Programmatically U can set Content's vertical alignment for textfield as well as button as
button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
txtField.contentVerticalAlignment = UIControlContentHorizontalAlignmentRight;
Hope it helps.
Happy Coding.. :)
im trying to draw a text on a subview, the view appearing fine but the text not, this is what im doing, the view have black color, the text have white color, text and rect value given in other method:
NSString* sumText = [[NSString alloc]initWithFormat:#"%0.1f",sum];
CGRect textRect = CGRectMake(chartLocation.x+chartLength+10,
chartLocation.y,
20,
20);
[self drawTextRect:sumText inRect:textRect];
-(void)drawTextRect:(NSString *)sumText inRect:(CGRect)textRect {
UIFont* font=[UIFont fontWithName:#"Arial" size:(20/2)];
UIColor* textColor = [UIColor whiteColor];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle]mutableCopy];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary* stringAttr=#{NSFontAttributeName:font,NSForegroundColorAttributeName:textColor,NSParagraphStyleAttributeName:paragraphStyle};
UIView* textView =[[UIView alloc] initWithFrame:textRect];
textView.backgroundColor = [UIColor blackColor];
[sumText drawInRect:textRect withAttributes:stringAttr];
[self.view addSubview:textView];}
am i do anything wrong? thanks
The drawInRect:withAttributes: method draws the string in the current graphics context. You have not defined a context, so your drawing goes nowhere.
If you want to draw the string yourself, you should subclass UIView, add the required properties (sumText), and overwrite drawRect:. A graphics context is automatically created for you in drawRect: so you can just go ahead and draw.
But really, you probably just want to use a UILabel instead.
there is another way you can achieve what you want .
UILabel* lblText = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 200, 200)];
lblText.text = #"Hello World!";
lblText.backgroundColor = [UIColor blackColor];
lblText.textColor = [UIColor whiteColor];
[self.view addSubview:lblText];
And Output is:
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 ?
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.
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.