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?
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 have try to add a two line title bar in UINavigationController
I want to adjust font size automatically set according to string length.My String max size goes to 60. I have try to implemented through following code
UILabel *bigLabel = [[UILabel alloc] init];
bigLabel.text = #"1234567890 1234567890 1234567890 1234567890 1234567890 123456";
bigLabel.backgroundColor = [UIColor clearColor];
bigLabel.textColor = [UIColor whiteColor];
bigLabel.font = [UIFont boldSystemFontOfSize:20];
bigLabel.adjustsFontSizeToFitWidth = YES;
bigLabel.clipsToBounds = NO;
bigLabel.numberOfLines = 2;
bigLabel.textAlignment = ([self.title length] < 10 ? NSTextAlignmentCenter : NSTextAlignmentLeft);
[bigLabel sizeToFit];
self.navigationItem.titleView = bigLabel;
It didn't work for me can you help me please. I have to made this for iPhone and iPad screen
Just set setNumberOfLines: 0 of UILabel. See the example below.
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[label setBackgroundColor:[UIColor clearColor]];
[label setNumberOfLines:0];
[label setTextColor:[UIColor whiteColor]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setText:#"1234567890 1234567890 1234567890 1234567890 1234567890 123456"];
self.navigationItem.titleView = label;
Set Left and Right UIBarButtonItems - you can add them.
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithTitle:#"Left" style:UIBarButtonItemStylePlain target:self action:#selector(leftPress:)];
self.navigationItem.leftBarButtonItem = leftBarButton;
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithTitle:#"Right" style:UIBarButtonItemStylePlain target:self action:#selector(rightPress:)];
self.navigationItem.rightBarButtonItem = rightBarButton;
Also decrease the FontSize of Label. It is 12 in this case.
And it will look like this:
For the extended question:
Just make some changes in previous code -
[label setNumberOfLines:2]; //Set it 2 instead of 0
NSString *titleStr = #"3456456676456464554541223434484384233456744444444456785643367";
//Check your string length then set font according to it.
//I have set font size according to your requirement
//which is 0-60.
if([titleStr length]>40 && [titleStr length]<=52){
[label setFont:[UIFont fontWithName:#"Arial" size:13.0]];
}
else if([titleStr length]>52){
[label setFont:[UIFont fontWithName:#"Arial" size:11.5]];
}
[label setText:titleStr];
Note: You can't use adjustsFontSizeToFitWidth: property of UILabel, because it doesn't work for setNumberOfLines:0 in your case you will have to handle it with if condition.
This is method for set fontSize Of UILabel according to its width.
[label setNumberOfLines:1];
label.adjustsFontSizeToFitWidth = YES;
label.minimumFontSize = 1.0;
thanks to #TheTiger for such a solution.
I have edited his answer according my requirement. so here is my answer
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setTextAlignment:NSTextAlignmentCenter];
NSString *titleStr1 = #"1234567890 1234567890 1234567890 1234567890 123 4568";
NSString *titleStr = #"I am a computer engineer. I wa";
NSLog(#"%d", [titleStr length]);
if([titleStr length]>40 && [titleStr length]<=50){
[label setNumberOfLines:2];
[label setFont:[UIFont boldSystemFontOfSize:13]];
}
else if([titleStr length]>30 && [titleStr length]<=40){
[label setNumberOfLines:2];
[label setFont:[UIFont boldSystemFontOfSize:16]];
}
else if([titleStr length]>50){
[label setNumberOfLines:2];
[label setFont:[UIFont boldSystemFontOfSize:10]];
}
else{
[label setFont:[UIFont boldSystemFontOfSize:20]];
[label setAdjustsFontSizeToFitWidth:YES];
}
[label setText:titleStr];
self.navigationItem.titleView = label;
I want to draw a long string of text underneath the Title on a UINaviagtionController.
I tried adding a UILabel to by doing :
[appDelegate.NavControll.view addSubview:testLabel];
And the Label appears but doesn't animate/move with the navbar and just seems independent of it.
Is there a way to achieve this or should I leave it alone and come up with another idea for displaying this string somewhere else.
Many Thanks,
Code
Looks a little bit ugly, but it works
You can easily customize appearance by yourself:
self.navigationItem.title = #"Needed title"; // for back button on pushed view
UIView *customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = #"Title";
titleLabel.font = [UIFont fontWithName:#"Helvetica" size:18];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.center = CGPointMake(30, 10);
[titleLabel sizeToFit];
[customTitleView addSubview:titleLabel];
[titleLabel release];
UILabel *detailsLabel = [[UILabel alloc] init];
detailsLabel.text = #"details";
detailsLabel.font = [UIFont fontWithName:#"Helvetica" size:9];
detailsLabel.backgroundColor = [UIColor clearColor];
[detailsLabel sizeToFit];
detailsLabel.center = CGPointMake(50, 35);
[customTitleView addSubview:detailsLabel];
[detailsLabel release];
self.navigationItem.titleView = customTitleView;
[customTitleView release];
you can add a label in your title bar and then on that label you can show your title and your long string both.
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[titleLabel setBackgroundColor:[UIColor clearColor]];
// here's where you can customize the font size
[titleLabel setFont:[UIFont boldSystemFontOfSize:16.0]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setText:#"hello\nhii"];
[titleLabel setNumberOfLines:2];
[titleLabel setLineBreakMode:UILineBreakModeTailTruncation];
[titleLabel sizeToFit];
[titleLabel setCenter:[self.navigationItem.titleView center]];
[self.navigationItem setTitleView:titleLabel];
[titleLabel release];
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;
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: