1

在此处输入图像描述

我不确定为什么?NSAttributedString但是当我使用自定义字体时sizeToFit,它会剪切字符串中的特定字符。

这是代码示例:

NSString *appTitle = @"Hemang";
NSString *appQuote = @" My name last char is g";

NSString *mergedString = [NSString stringWithFormat:@"%@\n%@",appTitle,appQuote];

UILabel *label1 = [[UILabel alloc] init];
label1.frame = CGRectMake(0, 0, 200.f, 80.f);
NSMutableAttributedString *firstLabelAttributes = [[NSMutableAttributedString alloc] initWithString:mergedString];

NSMutableParagraphStyle *paragrapStyle = NSMutableParagraphStyle.new;
paragrapStyle.alignment = NSTextAlignmentLeft;
[firstLabelAttributes addAttribute:NSParagraphStyleAttributeName value:paragrapStyle range:NSMakeRange(0, [mergedString length])];

[firstLabelAttributes addAttribute:NSFontAttributeName
                             value:[UIFont fontWithName:@"NexaBold" size:50.f]
                             range:NSMakeRange(0, [appTitle length])];

[firstLabelAttributes addAttribute:NSFontAttributeName
                             value:[UIFont fontWithName:@"NexaLight" size:10.f]
                             range:NSMakeRange([appTitle length]+1, [appQuote length])];

[label1 setNumberOfLines:2];
[label1 setAttributedText:firstLabelAttributes];
[self.view addSubview:label1];
[label1 sizeToFit];
label1.center = self.view.center;

更新 :

在我的另一个正常测试中

label1.text = @"Hemang"
[label1 sizeToFit];

g 剪辑

更新 2:

临时解决办法是在sizeToFit通话后增加一些高度。

CGRect frame = label1.frame
frame.size.height += 5;
label1.frame = frame;

它解决了我的问题,但我真的不想要这种平静的补丁。

4

0 回答 0