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.
Related
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?
I am implementing a footer view, which is a UIView and it has a label inside it. I wanted it so that the UILabel stays in the center all the time, but it just won't. Here's my code:
- (void)setupFooterView
{
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView_.frameWidth, kFooterViewHeight)];
[footerView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth];
[footerView setBackgroundColor:[UIColor clearColor]];
UILabel *loadMoreLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[loadMoreLabel setBackgroundColor:[UIColor clearColor]];
[loadMoreLabel setFont:[UIFont fontWithName:kProximaNova size:16]];
[loadMoreLabel setTextColor:[UIColor whiteColor]];
[loadMoreLabel setLineBreakMode:UILineBreakModeTailTruncation];
CGSize desiredSize = [[PNRConstants kLoadingMore] sizeWithFont:loadMoreLabel.font constrainedToSize:CGSizeMake(footerView.frameWidth, footerView.frameHeight) lineBreakMode:loadMoreLabel.lineBreakMode];
[loadMoreLabel setFrameSize:desiredSize];
[loadMoreLabel setCenter:CGPointMake(footerView.frameWidth/2, footerView.frameHeight/2)];
self.loadingMoreLabel_ = loadMoreLabel;
[footerView addSubview:self.loadingMoreLabel_];
[loadMoreLabel release];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[spinner setBackgroundColor:[UIColor clearColor]];
[spinner setFrameX:self.loadingMoreLabel_.frameX - spinner.frameWidth - 10];
[spinner setFrameY:self.loadingMoreLabel_.frameY];
self.spinner_ = spinner;
[footerView addSubview:self.spinner_];
[spinner release];
[self.scrollView_ setFooterView_:footerView];
[footerView release];
}
What am I doing wrong here?
[loadMoreLabel setCenter:CGPointMake(footerView.frameWidth/2, footerView.frameHeight/2)];
The footerView.frameWidth/2 needs to be
footerView.frameWidth/2 - loadMoreLabel.frame.size.width/2
This is because the anchor of the label is in the top left corner instead of in the center. You subtract half the width of your label to account for this.
after setting label's frame: [yourLabel setCenter: yourFooter.center];
The right way: use nibs, set the resize mask and call it a day.
The painful way: programmatically create the label.
Then a couple of options:
- make the label as wide as the footer and set it's text align to center.
- set the label's center property to footer.frame.width/2 and footer.frame.height/2
Edit: oh, you're dong that already. Set the text align property of yournlabel to center.
We're using custom fonts for the titleView in the navigation bar.
Somehow Apple always draws this font too high.
How do I correct for this strange offset you get when you are using custom fonts in a navbar?
I used setTitleVerticalPositionAdjustment:forBarMetrics:.
Compatibility: available starting from iOS 5.
Your can set a new view as titleView, then add a new label to it:
UIView * customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 40.0f)];
UILabel * customLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 200.0f, 20.0f)];
[customLabel setBackgroundColor:[UIColor clearColor]];
[customLabel setTextColor:[UIColor whiteColor]];
[customLabel setFont:[UIFont systemFontOfSize:12.0f]];
[customLabel setTextAlignment:UITextAlignmentCenter];
[customLabel setText:#"Your Text"];
[customTitleView addSubview:customLabel];
[customLabel release];
[self.navigationItem setTitleView:customTitleView];
[customTitleView 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: