If i set bottomLabel.text = Chinese character,i can't see the aboveLabel when i run it and if i debug view hierarchy i can see it.so what's the problem?
UILabel *bottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 200, 40)];
bottomLabel.backgroundColor = [UIColor redColor];
bottomLabel.text = #"中文";
[self.view addSubview:bottomLabel];
UILabel *aboveLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
aboveLabel.backgroundColor = [UIColor greenColor];
aboveLabel.text = #"aboveLabel";
[bottomLabel addSubview:aboveLabel];
why dont you use NSMutableAttributedString for this with one LABEL.
NSString *text1 = #"Hello";
NSString *date1 = #" 12.05 Pm\n";
NSString *text2 = #"World";
NSString *date2 = #" 11.00 AM";
self.lbl.numberOfLines = 0;
NSString * str = [text1 stringByAppendingString:date1];
NSString * str2 = [text2 stringByAppendingString:date2];
UIFont *text1Font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:10];
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString: str attributes:#{ NSFontAttributeName : text1Font }];
NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle1 setAlignment: NSTextAlignmentLeft];
[paragraphStyle1 setLineSpacing:1];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value: paragraphStyle1 range:NSMakeRange(0, [attributedString1 length])];
UIFont *text2Font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:10];
NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString: str2 attributes:#{NSFontAttributeName : text2Font }];
NSMutableParagraphStyle *paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle2 setLineSpacing:1];
[paragraphStyle2 setAlignment: NSTextAlignmentLeft];
[attributedString2 addAttribute:NSParagraphStyleAttributeName value: paragraphStyle2 range:NSMakeRange(0, [attributedString2 length])];
[attributedString1 appendAttributedString:attributedString2];
[self.lbl setAttributedText:attributedString1];
because above label is overlap on bottom label like above image so you not display that if you see that label above label x position change then it display.
if x value set 50 then you display bottm label.
You are adding aboveLabel as subview of bottomLabel hence it doesnot get displayed when you assign chinese character to it. You can see it view hierarchy as it is assigned a frame and added as subview. If you want to add one UILabel above UILabel you can add both labels as subview of common parent view.
i.e.
[self.view addSubview:bottomLabel];
[self.view addSubview:aboveLabel];
You are trying to adding aboveLabel inside of bottomLabel.Instead add both into one view or self.view.
UILabel *bottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 200, 40)];
bottomLabel.backgroundColor = [UIColor redColor];
bottomLabel.text = #"中文";
[self.view addSubview:bottomLabel];
UILabel *aboveLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(bottomLabel.frame)+10, 60, 30)];
aboveLabel.backgroundColor = [UIColor greenColor];
aboveLabel.text = #"aboveLabel";
[self.view addSubview:aboveLabel];
Try this code
Objective - C
UILabel *bottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 30)];
bottomLabel.backgroundColor = [UIColor redColor];
bottomLabel.text = #"中文";
[self.view addSubview:bottomLabel];
UILabel *aboveLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 0, 60, 30)];
aboveLabel.backgroundColor = [UIColor greenColor];
aboveLabel.text = #"abc";
[bottomLabel addSubview:aboveLabel];
Related
I'm trying to show an attributedString within a UILabel and I added "firstLineHeadIndent" property to its attributes.
With 16 characters of "国", it should break into two lines. But UILabel just show me 14 characters in only one line as shown in picture "not correct with 16 characters".
And it can show correctly when using 17 characters.Can anyone tell me why??
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 300, 80)];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
[self.view addSubview: label];
NSMutableParagraphStyle *paraStyle = [NSMutableParagraphStyle new];
paraStyle.firstLineHeadIndent = 40;
paraStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attrs = #{
NSParagraphStyleAttributeName:paraStyle,
NSFontAttributeName:[UIFont systemFontOfSize:17 weight:UIFontWeightBold],
NSKernAttributeName:#.9f
};
label.attributedText = [[NSAttributedString alloc] initWithString:#"国国国国国国国国国国国国国国国国" attributes:attrs];}
Not correct with 16 characters:
Show correctly with 17 characters:
Updated Answer
change this line : to
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 300, 80)];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByCharWrapping;
label.adjustsFontSizeToFitWidth=YES;
label.backgroundColor=[UIColor redColor];
label.textAlignment=NSTextAlignmentLeft;
[self.view addSubview: label];
NSMutableParagraphStyle *paraStyle = [NSMutableParagraphStyle new];
paraStyle.firstLineHeadIndent = 20;
paraStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attrs = #{
NSParagraphStyleAttributeName:paraStyle,
NSFontAttributeName:[UIFont systemFontOfSize:16 weight:UIFontWeightBold],
NSKernAttributeName:#.9f
};
label.attributedText = [[NSAttributedString alloc] initWithString:#"国国国国国国国国国国国国国国国国" attributes:attrs];
solution 2 :
remove paragraph : thing
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 300, 80)];
label.numberOfLines = 0;
label.text=#"国国国国国国国国国国国国国国国国国国";
label.lineBreakMode = NSLineBreakByWordWrapping;
label.adjustsFontSizeToFitWidth=YES;
label.backgroundColor=[UIColor redColor];
label.textAlignment=NSTextAlignmentCenter;
[self.view addSubview: label];
I also encountered this problem, which I think is a bug of UILabel. So I try to change it to UITextView, and it worked.
I think you can use NSTextAttachment with a transparent image to add extra space.
Hello in Xcode I am trying to add two labels to my image programmatically but when I add the second one it will overwrite my first one. Here is my code:
//first label
UILabel* caption = [[UILabel alloc] initWithFrame:
CGRectMake(-25, 0, 320, 50)
];
caption.backgroundColor = [UIColor whiteColor];
caption.textColor = [UIColor blackColor];
caption.textAlignment = UITextAlignmentLeft;
caption.font = [UIFont systemFontOfSize:16];
caption.text = [NSString stringWithFormat:#" #%#",[data objectForKey:#"username"]];
[self addSubview: caption];
//second label
UILabel* bottomBox = [[UILabel alloc] initWithFrame:
CGRectMake(-25, -200, 320, 50)
];
caption.backgroundColor = [UIColor whiteColor];
caption.textColor = [UIColor blackColor];
caption.textAlignment = UITextAlignmentCenter;
caption.font = [UIFont systemFontOfSize:16];
caption.text = [NSString stringWithFormat:#" Testing"];
[self addSubview: bottomBox];
I've tried changing the "0 to -200 to modify the Y coordinates so that the second label will shift down but it just overwrites the first one for some reason. Any help would be greatly appreciated thanks.
All the code for your 2nd label is referencing the 1st variable caption instead of bottomBox. You want:
//second label
UILabel* bottomBox = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 320, 50)];
bottomBox.backgroundColor = [UIColor whiteColor];
bottomBox.textColor = [UIColor blackColor];
bottomBox.textAlignment = UITextAlignmentCenter;
bottomBox.font = [UIFont systemFontOfSize:16];
bottomBox.text = #"Testing";
[self addSubview: bottomBox];
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)];
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.