uitextview font not changing with fontWIthName - ios

I'm trying to change the font for a UITextView with textview.font = [UIFont fontWithName:etc];
But for some reason it isn't doing anything and it always displays the same font and size.
Here's the code:
UITextView *descriptionTextView = [[UITextView alloc] initWithFrame:rect];
descriptionTextView.editable = NO;
descriptionTextView.scrollEnabled = NO;
descriptionTextView.text = description;
titleTextView.font = [UIFont fontWithName:#"Veranda" size:17.0]; //<- this line isn't working
[cell.contentView addSubview:descriptionTextView];
Any idea what the probem might be?
Thanks a bunch!

May be the error in that you change font of other text field?
You operate with textview named 'descriptionTextView', but change font of 'titleTextView'
Or that default font is named "Verdana", not "Veranda"

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 to get the current font size of an UILabel

I have adjusted the UILabel font size according to the width in this way.
[btnPending.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:15]];
[btnPending setBackgroundColor:[UIColor clearColor]];
btnPending.titleLabel.adjustsFontSizeToFitWidth = YES;
btnPending.titleLabel.numberOfLines = 1;
btnPending.titleLabel.lineBreakMode = NSLineBreakByClipping;
[_vwTabBtnParent addSubview:btnPending];
And its working fine. But now I want to get the current font size of that UILabel. How can I do this in objective-c.
Please help me,
Thanks
Then to get the font name and size all you need is
NSString *fontName = self.label.font.fontName;
CGFloat fontSize = self.label.font.pointSize;
Try below method:
[label.text sizeWithFont:label.font
minFontSize:label.minimumFontSize
actualFontSize:&actualFontSize
forWidth:label.bounds.size.width
lineBreakMode:label.lineBreakMode];
Note: Deprecated in iOS 7 with no alternative.

how to change font size of label without changing font family in xcode

how to change font size of label without changing font family in xcode6 ?I am using following line of code:
lblMain.font = [UIFont systemFontOfSize:22];
Here's an example:
lblMain.font = [UIFont fontWithName:#"HelveticaNeue" size:22];
Or - you can even use the current font:
lblMain.font = [lblMain.font fontWithSize:22];
NSString *fontName = lblMain.font.fontName;
CGFloat fontSize = lblMain.font.pointSize;
[lblMain setFont:[UIFont fontWithName:fontName size:NEWSIZE]];
Use following code, if you just want to change font size keeping the same font family:
lblMain.font = [UIFont fontWithName:lblMain.font.fontName size:22.0f];
OR
You can also use this code:
lblMain.font = [lblMain.font fontWithSize:22.0f];
[lblMain setFont:[UIFont fontWithName:#"HelveticaNeue" size:17]];

Dynamically adjust font size in UINavigationBar

What I want to do is this :
I've a navigation bar using an image covering the whole bar and looking like this:
Now I want to put the title on the black part of the bar and so it doesn't impinge on the icon.
This title may be very short or very long. So I tried this:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
label.backgroundColor = [UIColor blackColor];
label.numberOfLines = 2;
label.minimumFontSize = 8.;
label.adjustsFontSizeToFitWidth = YES;
label.font = [UIFont boldSystemFontOfSize:16.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text = name;
self.navigationItem.titleView = label;
But this is not working because the label's width grows if needed upon the label and exceeds 200px.
What should I do then?
Thanks for your help.
PS : the camera icon is not a button.
Your code works fine in iOS 7.0
Since you are using UITextAlignmentCenter (instead of NSTextAlignmentCenter), and also using minimumFontSize, I am assuming you are running on an earlier version of iOS, since these were both deprecated in iOS 6.0
The main problem is that adjustsFontSizeToFitWidth only works in iOS6 and earlier if the numberOfLines is set to 1
As you can see in the apple developer documentation
https://developer.apple.com/library/ios/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/occ/instp/UILabel/adjustsFontSizeToFitWidth
If you necessarily want two lines and also want the font size to adjust... you could maybe just use the length property of your name string, which would tell you how many characters are in the string, and you could then set the font size according to the length of the string

Adjust font size in Navigation Title

Is there any way to make the font of the title of the navigation bar adjusted with the width?
Same behaviour as a [UILabel setAdjustsFontSizeToFitWidth:YES].
I'm trying setting the appearance of the NavigationBar, but no success so far.
Try doing something like:
self.navigationItem.titleView = titleLabel;
where titleLabel is a custom UILabel that you've initialized and customized.
Check out this Fontful code, for example.
There's no way to do this with the standard title item. You can still add your custom UILabel to the navigation bar and take care of the title changes.
You can not change the font of title.
Instead try out this..
self.title = #"My title is this.";
CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:17.0];
label.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = label;
label.text = self.title;
[label release];

Resources