AttributedString in UINavigationBar not showing both lines of text - ios

I am trying to set a two line label in my UINavigationBar. When I do the following, it only shows one line (the part before the line break).
NSString *title = #"First line of title is bigger\nSecond line of title is smaller";
NSDictionary *attribute1 = #{NSForegroundColorAttributeName: [UIColor colorWithRed:148/255.0f green:147/255.0f blue:147/255.0f alpha:1],
NSFontAttributeName: [UIFont boldSystemFontOfSize: 14.0f]};
NSDictionary *attribute2 = #{NSForegroundColorAttributeName: [UIColor colorWithRed:148/255.0f green:147/255.0f blue:147/255.0f alpha:1],
NSFontAttributeName: [UIFont boldSystemFontOfSize: 10.0f]};
const NSRange line1Range = {0, 29};
const NSRange line2Range = {31, 30};
NSMutableAttributedString *attributedText =[[NSMutableAttributedString alloc] initWithString:title];
[attributedText setAttributes: attribute1 range:line1Range];
[attributedText setAttributes: attribute2 range:line2Range];
UILabel *label = [[UILabel alloc] init];
label.attributedText=attributedText;
self.navigationItem.titleView = label;
[self.navigationItem.titleView sizeToFit];
For comparison, the following shows both line. The following is for comparison only. The top version is what I need.
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 2;
label.font = [UIFont boldSystemFontOfSize: 14.0f];
label.textColor = [UIColor colorWithRed:148/255.0f green:147/255.0f blue:147/255.0f alpha:1];
label.text = #"First line of title is bigger\nSecond line of title is smaller";
self.navigationItem.titleView = label;
[self.navigationItem.titleView sizeToFit];
Any help getting the top version to work correctly?

It appears that calling label.sizeToFit() before self.navigationItem.titleView = label displays the NSAttributedString title correctly.
(Swift, iOS8.1, XCode 6.1.1)

Easy problem, easy solution. Even if you are using attributedText instead of text, you still need to:
label.numberOfLines = 0;

Related

Navigation Title Chopped with external Font 'FuturaStd-Book'

Title chopped in Navigation with external Font 'FuturaStd-Book'.
Tried to decreased Font size with following code but still title chopped.
[[UINavigationBar appearance] setTitleTextAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"FuturaStd-Book" size:16]}];
Kindly help me on this...
Following code to Align the navigationItem title. (Only Works from iOS 11 - but the Navigation back and Title y position differs )
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.alignment = NSTextAlignmentCenter;
style.firstLineHeadIndent = 1.0f;
style.lineSpacing = 2; //Change spacing between lines
style.paragraphSpacing = 2; //Change space between paragraphs
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor blackColor],
NSBackgroundColorAttributeName:[UIColor clearColor],
NSFontAttributeName:[UIFont fontWithName:#"FuturaStd-Book" size:18],
NSParagraphStyleAttributeName: style }];
You've to use the titleView of the navigationItem. Try this in your VC:
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 400.0, 44.0)];
headerLabel.textAlignment = NSTextAlignmentCenter;
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.text = #"Register";
headerLabel.font = [UIFont fontWithName:#"FuturaStd-Book" size:16];
headerLabel.textColor = [UIColor blackColor];
self.navigationItem.titleView = headerLabel;
Output:

Add 2 lines of text on Navigation Bar in IOS using TitleView

I have to add two lines of text with different font sizes on Navigation Bar using Title View.
I have tried the following code:
I want EREDITOMETRO as bold with large font and LA SUCCESSIONE A PORTATA DI MANO with small font
UILabel * label =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 480, 44)]; label.backgroundColor =[UIColor clearColor];
label.numberOfLines =2;
label.font =[UIFont boldSystemFontOfSize:9.0f];
label.textColor =[UIColor whiteColor];
label.text = #"EREDITOMETRO \n LA SUCCESSIONE A PORTATA DI MANO";
self.navigationItem.titleView = label;
Try This -
NSString *stringName = #"Welcome Home" ;
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:stringName];
[attrString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize: 14.0f] range:NSMakeRange(0, 4)];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 480, 44)];
label.numberOfLines =0;
label.attributedText = attrString;
self.navigationItem.titleView = label;
- (void)setTitle:(NSString *)title
{
[super setTitle:title];
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView) {
titleView = [[UILabel alloc] initWithFrame:CGRectZero];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0];
titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleView.numberOfLines = 2;
titleView.textColor = [UIColor yellowColor];
self.navigationItem.titleView = titleView;
}
}
you need to make a custom label and pass it to navigation item like this
let label: UILabel = UILabel(frame: CGRectMake(0, 0, 300, 50))
label.backgroundColor = UIColor.clearColor()
label.numberOfLines = 2
label.font = UIFont.boldSystemFontOfSize(14.0)
label.textAlignment = .Center
label.textColor = UIColor.whiteColor()
label.text = "Your Text"
self.navigationItem.titleView = label

Setting UILabel with attributed string as titleView nav controller results in weird behaviour

I'm trying to set the titleView of a UINavigationController with multiple lines in a UILabel.
I need the two lines to be different font sizes but perfectly aligned so I figured I'd use a attributed string to do so. Here's my code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 480, 44)];
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 2;
label.font = [UIFont boldSystemFontOfSize: 14.0f];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
UIFont *fontSmall = [UIFont fontWithName:#"HelveticaNeue" size:11.0];
NSDictionary *attrsDictionarySmall =
[NSDictionary dictionaryWithObject:fontSmall
forKey:NSFontAttributeName];
NSAttributedString *attrSmallString =
[[NSAttributedString alloc] initWithString:#"small text"
attributes:attrsDictionarySmall];
UIFont *font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:14.0];
NSDictionary *attrsDictionary =
[NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
NSAttributedString *attrString =
[[NSAttributedString alloc] initWithString:#"$200"
attributes:attrsDictionary];
label.text = [NSString stringWithFormat:#"%#\n%#",attrString,attrBTCString];
self.navigationItem.titleView = label;
However, this results in the following:
How do I fix that?
NSAttributedString has a NSString property which can be used here.
label.text = [NSString stringWithFormat:#"%#\n%#",attrString.string,attrSmallString.string];

UILabel won't register line spacing property

For some reason this code (the bold text in particular) doesn't change the line spacing of the text at all:
UIFont* customFont = [UIFont fontWithName:#"BebasNeue" size:70];
NSString * text = #"Their \nIdeas";
**NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
paragrahStyle.lineSpacing = 30;
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [text length])];**
UILabel *lbl1 = [[UILabel alloc] init];
[lbl1 setFrame:CGRectMake(120, 70, viewWidth, 180)];
lbl1.backgroundColor = [UIColor clearColor];
lbl1.textColor = grayColor;
lbl1.numberOfLines = 2;
lbl1.attributedText = attributedString;
lbl1.userInteractionEnabled = NO;
lbl1.text = text;
[lbl1 setFont:customFont];
[view addSubview:lbl1];
[lbl1 setTransform:CGAffineTransformMakeRotation(0.35)];
What am I doing wrong?
The issue is with this line ,
lbl1.text = text;
You are assigning a non attributed string just after assigning the attributed string which contains all line spacing data. Remove above line then your code will work.
And if you are using a large value for line spacing, make sure your label's height is enough to display the second line.

attributedPlaceholder font change

I am trying to change the font of my placeholder but although the placeholder text appears, the font is the standard. Im using the following method to set up my UITextField:
UITextField *name = [[UITextField alloc] initWithFrame:CGRectMake(25, 0, frame.size.width - 50, 50)];
name.borderStyle = UITextBorderStyleNone;
name.background = [UIImage imageNamed:#"UITextField_orange.png"];
name.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Name" attributes:#{NSFontAttributeName:#"Champagne&Limousines"}];;
[self addSubview:name];
The value that goes with the NSFontAttributeName key should be a UIFont object:
name.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Name" attributes: #{
NSFontAttributeName: [UIFont fontWithName:#"Champagne&Limousines" size:12]
}];

Resources