Custom multiline UILabel with black background behind text - ios

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;

Related

UITextView text give small gap between each word in middle of text

Team,
I have UITextView added content of text, with font family avinar roma, I noticed there is small gap in between each word which was not consistent. How to avoid the small gap in textview text for each word.
descriptionTextView = [[UITextView alloc] initWithFrame:CGRectMake(20, titlelabel.frame.origin.y + titlelabel.frame.size.height + 5, view.frame.size.width - 40, view.frame.size.height - 150)];
descriptionTextView.backgroundColor = [UIColor clearColor];
descriptionTextView.textColor = MH_PANTONE_444;
descriptionTextView.textAlignment = NSTextAlignmentCenter;
descriptionTextView.font = [UIFont fontWithName:MH_FONT_AVENIRROMAN size:MH_SCREEN_HEIGHT/48];
descriptionTextView.text = [descriptionArray objectAtIndex:index];
descriptionTextView.editable = NO;
descriptionTextView.scrollEnabled = NO;
descriptionTextView.selectable = YES;
descriptionTextView.dataDetectorTypes = UIDataDetectorTypeAll;
Please have image attached above
University Medical Center, is not same gap with remaining text.
Setting the hyphenationFactor should fix your problem. But you have to use NSAttributedString instead of plain text:
UIFont *font = [UIFont fontWithName: MH_FONT_AVENIRROMAN size: MH_SCREEN_HEIGHT/48];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.hyphenationFactor = 1.0;
NSDictionary *attributes = #{ NSFontAttributeName:font,
NSForegroundColorAttributeName: MH_PANTONE_444;
NSParagraphStyleAttributeName: paragraphStyle };
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString: [descriptionArray objectAtIndex:index]
attributes: attributes];
descriptionTextView.attributedText = attributedText;
https://developer.apple.com/reference/uikit/nsmutableparagraphstyle/1535553-hyphenationfactor
You may have to adjust the hyphenationFactor until you get the result you want. (0.0-1.0)
I solved the same issue by setting the leftView property of the UITextField to be an empty view with the size of the padding as desired:
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
textView.leftView = paddingView;
textView.leftViewMode = UITextFieldViewModeAlways;
I had also face an issue of text alignment with custom fonts in iOS application, and that was with vertical alignment. I guess this is because of the custom font you are using in your application. But luckily there is a solution.
Every font file have some configurations about display the letter. One of the property is minLeftSideBearing for left spacing and minRightSideBearing for right spacing. I guess by changing them you can solve this spacing issue.
Follow the following link to check how to change this properties.
http://www.andyyardley.com/2012/04/24/custom-ios-fonts-and-how-to-fix-the-vertical-position-problem/
And as mention in above blog you need to install font tool. That you can download from following link.
https://developer.apple.com/download/more/?=font
Hope this might help you.
Thanks, Jay.

How can we fix UItextfields text and UIbuttons title labels exactly at center position

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.. :)

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.

Clipping of UILabel when adjustsFontSizeToFitWidth

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.

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