I am having one problem stuck. I have created UILabel and changed it frame i.e size height as per the content and it is very fine with all the iPhone devices. However my label is not centre align if we are getting just few word, like Adidas, Nike etc.
brandtitlelabel = [[UILabel alloc] init];
brandtitlelabel.backgroundColor = [UIColor clearColor];
brandtitlelabel.textColor = [UIColor commonTextColor];
brandtitlelabel.numberOfLines = 0;
brandtitlelabel.adjustsFontSizeToFitWidth = YES;
brandtitlelabel.minimumScaleFactor = 0.1;
brandtitlelabel.textAlignment = NSTextAlignmentCenter;
brandtitlelabel.lineBreakMode = NSLineBreakByWordWrapping;
brandtitlelabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:30];
NSString *text = #"Adidas";
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:#{NSFontAttributeName:brandtitlelabel.font}];
rect = [attributedText boundingRectWithSize:(CGSize){self.view.frame.size.width, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil];
rect.origin = CGPointMake(0, 66);
brandtitlelabel.frame = rect;
brandtitlelabel.text = text;
[self.view addSubview:brandtitlelabel];
Try with this.
brandtitlelabel.text = text;
brandtitlelabel.center = CGPointMake(self.view.center.x,brandtitlelabel.center.y);//replace an 'self.view' with your 'superver' on which you want to center.
[self.view addSubview:brandtitlelabel];
Add following line of code at the end after setting the frame.
brandtitlelabel.textAlignment = NSTextAlignmentCenter;
brandtitlelabel.lineBreakMode = NSLineBreakByWordWrapping;
Related
I want to make a game title but I can't seem to make the font larger. Here's what I have. I opted to not use CGRectMake initializer setting because I wanted to just set the x and y positions, not the size of the label (which I presumed would've been dictated by the size: portion of the UIFont method below). The 100.0 is no different from size 40.0.
UILabel *title = [[UILabel alloc] init];
CGRect frame = title.frame;
frame.origin.y = 30;
frame.origin.x = 100;
title.frame= frame;
title.text = #"TWINSTONES";
title.backgroundColor = [UIColor clearColor];
title.font = [UIFont fontWithName:#"TWINSTONES" size:100.0];
[title setTextColor:[UIColor colorWithWhite:0.9 alpha:1.0]];
title.numberOfLines = 0;
[title sizeToFit];
[self.view addSubview:title];
I am using following code to align 2 strings in UINavigationBar centre .
for ex:
<Back John Kelly Maria
23/F
I need these kind of middle aligned text in UINavigationbar but what jam now getting is all the text aligned in right side of UINavigationBar .
something like this
<Back John Kelly Maria
23/F
Please help..me .
this is my code
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,50)];
UIFont * customFont = [UIFont fontWithName:#"Helvetica Neue" size:19]; //custom font
NSString * text =title;
UILabel *patientNameLabel;
if([title length]>15){
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}else{
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}
patientNameLabel.text = #"John Kelly Maria";
patientNameLabel.font = customFont;
patientNameLabel.numberOfLines = 0;
patientNameLabel.textAlignment=NSTextAlignmentCenter;
patientNameLabel.minimumScaleFactor = 10.0f/12.0f;
patientNameLabel.clipsToBounds = YES;
patientNameLabel.backgroundColor = [UIColor clearColor];
patientNameLabel.textColor = [UIColor blackColor];
patientNameLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
NSString *subTitle =subTitleText;
UIFont * customFont1 = [UIFont fontWithName:#"Helvetica Neue" size:14]; //custom font
UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,320,20)];
subTitleLabel.textAlignment = NSTextAlignmentCenter;
subTitleLabel.text = #"23/F";
subTitleLabel.font = customFont1;
subTitleLabel.textAlignment=NSTextAlignmentCenter;
subTitleLabel.numberOfLines = 1;
subTitleLabel.adjustsFontSizeToFitWidth = YES;
subTitleLabel.minimumScaleFactor = 10.0f/12.0f;
subTitleLabel.clipsToBounds = YES;
subTitleLabel.backgroundColor = [UIColor clearColor];
subTitleLabel.textColor = [UIColor blackColor];
[titleView addSubview:patientNameLabel];
[titleView addSubview:subTitleLabel];
Add below two lines before addSubview lines..
patientNameLabel.center = CGPointMake(titleView.frame.size.width/2, 0);
subTitleLabel.center = CGPointMake(titleView.frame.size.width/2, 25);
Add Below Code & see
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,50)];
UIFont * customFont = [UIFont fontWithName:#"Helvetica Neue" size:19]; //custom font
NSString * text =#"Nilesh Patel";
UILabel *patientNameLabel;
if([text length]>15){
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}else{
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}
patientNameLabel.text = #"Nilesh Patel";
patientNameLabel.font = customFont;
patientNameLabel.numberOfLines = 0;
patientNameLabel.textAlignment=NSTextAlignmentCenter;
patientNameLabel.minimumScaleFactor = 10.0f/12.0f;
patientNameLabel.clipsToBounds = YES;
patientNameLabel.backgroundColor = [UIColor clearColor];
patientNameLabel.textColor = [UIColor blackColor];
patientNameLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
NSString *subTitle =#"iOS Developer";
UIFont * customFont1 = [UIFont fontWithName:#"Helvetica Neue" size:14]; //custom font
UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,320,20)];
subTitleLabel.textAlignment = NSTextAlignmentCenter;
subTitleLabel.text = #"28/M";
subTitleLabel.font = customFont1;
subTitleLabel.textAlignment=NSTextAlignmentCenter;
subTitleLabel.numberOfLines = 1;
subTitleLabel.adjustsFontSizeToFitWidth = YES;
subTitleLabel.minimumScaleFactor = 10.0f/12.0f;
subTitleLabel.clipsToBounds = YES;
subTitleLabel.backgroundColor = [UIColor clearColor];
subTitleLabel.textColor = [UIColor blackColor];
patientNameLabel.center = CGPointMake(titleView.frame.size.width/2, 10);
subTitleLabel.center = CGPointMake(titleView.frame.size.width/2, 30);
[titleView addSubview:patientNameLabel];
[titleView addSubview:subTitleLabel];
[self.navigationController.navigationBar addSubview:titleView];
Note : You have set static frame to titleView so if you see your app on different device you will not be able to see the text in exact centre. To avoid this set the titleView frame dynamically based on device/view width.
Try Frame setting like this
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,20,screenWidth,50)];
UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,screenWidth,20)];
UILabel *patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,screenWidth,30)];
I'm pretty sure this is actually a UIKit bug but want to get some input to see if I'm missing something silly here.
Here is the code I have:
// single line with modified line spacing and 2 colors - broken, line spacing is added to the bottom!
UILabel *brokenLabel = [[UILabel alloc] init];
brokenLabel.backgroundColor = [UIColor greenColor];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"Just some text"];
[attributedString addAttributes:#{NSForegroundColorAttributeName : [UIColor redColor]} range:[attributedString.string rangeOfString:#"text"]];
attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter);
brokenLabel.attributedText = attributedString;
[brokenLabel sizeToFit];
brokenLabel.frame = CGRectOffset(brokenLabel.frame, 50, 100);
[self.view addSubview:brokenLabel];
// end
// single line with modified line spacing and 1 color - correct
UILabel *workingLabel = [[UILabel alloc] init];
workingLabel.backgroundColor = [UIColor greenColor];
attributedString = [[NSMutableAttributedString alloc] initWithString:#"Just some text"];
attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter);
workingLabel.attributedText = attributedString;
[workingLabel sizeToFit];
workingLabel.frame = CGRectOffset(workingLabel.frame, 200, 100);
[self.view addSubview:workingLabel];
//end
// multiple lines with modified line spacing and 1 color - correct
UILabel *workingLabel2 = [[UILabel alloc] init];
workingLabel2.frame = CGRectMake(0, 0, 100, 0);
workingLabel2.numberOfLines = 0;
workingLabel2.backgroundColor = [UIColor greenColor];
attributedString = [[NSMutableAttributedString alloc] initWithString:#"Just some text"];
attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter);
workingLabel2.attributedText = attributedString;
[workingLabel2 sizeToFit];
workingLabel2.frame = CGRectOffset(workingLabel2.frame, 50, 300);
[self.view addSubview:workingLabel2];
//end
// multiple lines with modified line spacing and 2 color - correct
UILabel *workingLabel3 = [[UILabel alloc] init];
workingLabel3.frame = CGRectMake(0, 0, 100, 0);
workingLabel3.numberOfLines = 0;
workingLabel3.backgroundColor = [UIColor greenColor];
attributedString = [[NSMutableAttributedString alloc] initWithString:#"Just some text"];
[attributedString addAttributes:#{NSForegroundColorAttributeName : [UIColor redColor]} range:[attributedString.string rangeOfString:#"text"]];
attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter);
workingLabel3.attributedText = attributedString;
[workingLabel3 sizeToFit];
workingLabel3.frame = CGRectOffset(workingLabel3.frame, 200, 300);
[self.view addSubview:workingLabel3];
along with a simple convenience function to change the lineSpacing of an attributed string:
NSMutableAttributedString *attributedStringFromAttributedStringWithLineSpacing(NSAttributedString *string, CGFloat lineSpacing, NSTextAlignment textAlignment)
{
NSMutableAttributedString *mutable = string.mutableCopy;
NSMutableParagraphStyle *par = [NSMutableParagraphStyle new];
par.alignment = textAlignment;
par.lineSpacing = lineSpacing;
[mutable addAttributes:#{NSParagraphStyleAttributeName : par} range:NSMakeRange(0, mutable.length)];
return mutable;
}
However, this is what it looks like
As you can see, the height of the first label is way too big (or the height that it should be + my custom line spacing, to be precise). When simply adding another color to the first attributed string, it causes the sizeToFit size to increase by adding the lineSpacing below it. I also tried using the boundingRectWithSize: methods on the strings directly and the same issue is visible. So this is not specific to the label sizing code but is an issue with the strings themselves. I don't see any possible reason why this should be happening. Does anyone have any insight?
In your Attributes Dictionary add
[attrDic setObject:#0 forKey:NSBaselineOffsetAttributeName];
UILabel is created like this:
label = [[UILabel alloc] initWithFrame:CGRectMake(32, 10, 268, 44)];
label.textAlignment = NSTextAlignmentLeft;
label.backgroundColor = [UIColor yellowColor];
label.numberOfLines = 2;
label.lineBreakMode = NSLineBreakByTruncatingTail;
Then to update the label I do this:
label.text = someText;
CGRect frame = label.frame;
NSDictionary *attributes = #{NSFontAttributeName: label.font};
CGRect rect = [someText boundingRectWithSize:CGSizeMake(label.frame.size.width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
frame.size.height = rect.size.height;
label.frame = frame;
This works fine with the label only has one line of text, but I have a maximum of two lines. When the 2nd line gets truncated, extra bottom padding gets added to the label, which makes it impossible to position the label below it (the red label in the images):
Why does this happen? If it's a bug, is there a workaround?
Try something like this:
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:text attributes:attributes];
UILabel *anotherLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, myLabel.frame.size.width, MAXFLOAT)];
[anotherLabel setAttributedText:attrString];
[anotherLabel setNumberOfLines:2];
[anotherLabel setText:NSTextAlignmentLeft];
[anotherLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[anotherLabel sizeToFit];
[myLabel setAttributedText:attrString];
[myLabel setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, [anotherLabel frame].size.height)];
I've read many questions on the topic but I can't seem to find what is wrong with my code:
UILabel *nameLabel = [[UILabel alloc] init];
[nameLabel setText: _nameString];
nameLabel.textAlignment = UITextAlignmentLeft;
nameLabel.contentMode = UIViewContentModeTop;
nameLabel.lineBreakMode = UILineBreakModeWordWrap;
nameLabel.numberOfLines = 0;
nameLabel.font = [UIFont fontWithName:#"Verdana" size:14];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.textColor = [UIColor colorWithRed:0 green:0.282 blue:0.31 alpha:1];
nameLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
CGSize maximumLabelSize = CGSizeMake(200.0f, 60.0f);
CGSize expectedLabelSize = [_nameString sizeWithFont:nameLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:nameLabel.lineBreakMode];
nameLabel.frame = CGRectMake(10, 10, expectedLabelSize.width, expectedLabelSize.height);
And although sometimes it does work (on larger texts) on texts like "Airplanes being the future" the expectedLabelSize returns height 18.0f and it cuts the sentence on the "Airplanes being the"
What am I doing wrong here?
I had the same problem once, that was because my label's width was smaller than the maximum Label's width wat I used to calculate the "expectedLabelSize".
Since you are using an autoresizingMask your label might be too small.