I want to configure the truncation mode for the text that appears in UINavigationItem.
I've come across a couple of different solutions of configuring the font size but not sure how to make use of the UILineBreakModeHeadTruncation that exists on NSString.
How I've been able to configure the font size:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
[label setFont:[UIFont boldSystemFontOfSize:16.0]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:self.title];
[self.navigationController.navigationBar.topItem setTitleView:label];
[label release];
(Based on the recommendation from here)
If I call this in loadView or viewDidLoad, it seems to work but outside of that method, after the view has been loaded, it doesn't seem to work. Not sure why though.
What have you tried, and what's not working? You should be able to do:
label.lineBreakMode = UILineBreakModeHeadTruncation;
Or to wrap to multiple lines:
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
Related
I just want to highlight only text in UILabel, I have tried by giving backgroundColor for label, but it is highlighting the empty spaces also looks not good. So Is there any way to highlight text without resizing UILabel.
Please check the image, here labels are bigger than the text (center aligned)
Thanx.
Most of the other solutions don't consider text that spans multiple lines while still only highlighting the text, and they are all pretty hacky involving extra subviews.
An iOS 6 and later solution is to use attributed strings:
NSMutableAttributedString *s =
[[NSMutableAttributedString alloc] initWithString:yourString];
[s addAttribute:NSBackgroundColorAttributeName
value:[UIColor greenColor]
range:NSMakeRange(0, s.length)];
label.attributedText = s;
This will add a subview behind the text, with the correct size:
CGSize size= [[label text] sizeWithFont:[UIFont systemFontOfSize:18.0]];
NSLog(#"%.1f | %.1f", size.width, size.height);
NSLog(#"%.1f | %.1f", label.frame.size.width, label.frame.size.height);
UIView *highlightView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
[highlightView setBackgroundColor:[UIColor greenColor]];
[self.view insertSubview:highlightView belowSubview:label];
[highlightView setCenter:label.center];
And don't forget: [label setBackgroundColor:[UIColor clearColor]];
try this
MTLabel
MTLabel *label4 = [MTLabel labelWithFrame:CGRectMake(20, 270, 250, 60)
andText:#"This label is highlighted, has a custom line height, and adjusts font size to fit inside the label."];
[label4 setLineHeight:22];
[label4 setFont:[UIFont fontWithName:#"Helvetica" size:30]];
[label4 setAdjustSizeToFit:YES];
[label4 setMinimumFontSize:15];
[label4 setFontHighlightColor:[[UIColor orangeColor] colorWithAlphaComponent:0.5]];
[self.view addSubview:label4];
You can have a UIImageView, set its background color of your choice then place your UIlabel on it.
That will surely solve your problem.
Here is Workaround code :
UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 100, 40)];
[imgView setBackgroundColor:[UIColor brownColor]];
[self.view addSubview:imgView];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:UITextAlignmentCenter];
label.text = #"My Label";
[imgView addSubview:label];
[label release];
[imgView release];
You can also achieve the same using Interface Builder.
I'm running into an issue rendering the following text example
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(200.0f, 200.0f, 0.0f, 0.0f)];
[label setFont:[UIFont fontWithName:#"MyriadPro-Bold" size:30.0f]];
[label setText:#"MÈssÅgËs"];
[label sizeToFit];
I then tried using the attributed string in iOS 6.0 >
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(200.0f, 200.0f, 0.0f, 0.0f)];
[label setFont:[UIFont fontWithName:#"MyriadPro-Bold" size:30.0f]];
[label setAttributedText: [[NSAttributedString alloc] initWithString:#"MÈssÅgËs"]];
[label sizeToFit];
While the attributed string sized better, the text is clipped in both cases. Does anyone know how to solve this issue?
In my app when I'm setting the title more than 13 characters it is hidden by bar button.So how do i overcome from this problem.Please help me.Thanks in advance.
Use a smaller font or a shorter title, or use a multiline title by customizing the navBar's titleView with a custom UILabel. For example,
// CUSTOMIZE NAVIGATION BAR TITLE LABEL
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont boldSystemFontOfSize:20.0]];
[label setAdjustsFontSizeToFitWidth:YES];
[label setTextAlignment:UITextAlignmentCenter];
[[self navigationItem] setTitleView:label];
[label release];
You can customize it however you like.
I just wanted to know if it is possible to set the NavigationBarButton's title's text color, and if so how?
I did the following. In -(id)init add:
// CUSTOMIZE NAVIGATION BAR TITLE COLOR
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont boldSystemFontOfSize:20.0]];
[label setAdjustsFontSizeToFitWidth:YES];
[label setTextAlignment:UITextAlignmentCenter];
[[self navigationItem] setTitleView:label];
[label release];
Then in -(void)viewDidLoad add:
[[[self navigationItem] titleView] setText:#"TITLE"];
[[[self navigationItem] titleView] setTextColor:titleColor];
I put them like this so that I could change the title and titleColor based on how one got to this screen. If you don't need to customize, then put it all in init or viewDidLoad. Doing it like this I get a warning that "UIView may not respond to '-setText'" even though it all works fine.
You can set an image for the UIBarButtonItem and thus fully customize the title text.
I have a UILabel:
descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 30, 130, 150)];
[descriptionLabel setFont:[Utils getSystemFontWithSize:14]];
[descriptionLabel setBackgroundColor:[UIColor clearColor]];
[descriptionLabel setTextColor:[UIColor whiteColor]];
descriptionLabel.numberOfLines = 0;
descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
[self addSubview:descriptionLabel];
If the text is only 1 line long it will appear in the middle of the label.
Is there anyway I can have the text display from the top left corner of the label instead of the middle center?
You can set
[descriptionLabel setTextAlignment:UITextAlignmentLeft];
or if you prefer dot syntax
descriptionLabel.textAlignment = UITextAlignmentLeft;
for left/right alignment set the textAlignment property of UILabel to either UITextAlignmentLeft or UITextAlignmentRight.
for more fine tuning you can override drawTextInRect: