NSString文本高度没有得到正确的值?

时间:2016-09-08 12:25:25

标签: ios objective-c ipad

如果正确使用新行(“\ n”)

,则无法正确获取文本高度
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

CGRect rect = [text boundingRectWithSize:CGSizeMake(textWidth, MAXFLOAT)
                                 options:NSStringDrawingUsesLineFragmentOrigin
                              attributes:@{NSFontAttributeName:textFont,
                                           NSParagraphStyleAttributeName:
                                               paragraphStyle}
                                 context:nil];

即使尝试了其他方法,例如尺寸,但我得到相同的值,我的文字也有“\ n”新行字符。

3 个答案:

答案 0 :(得分:1)

NSLineBreakByClipping是您想要自动收缩文本以适应的内容。如果您尝试调整视图以适合文字,那么您需要NSLineBreakByWordWrapping

此外,如果您从控件的当前宽度获得textWidth,请确保宽度位于viewWillAppear或更晚,而不是viewDidLoad

答案 1 :(得分:0)

您需要舍入rect值。见下面的例子。

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

CGRect rect = [text boundingRectWithSize:CGSizeMake(textWidth, MAXFLOAT)
                                 options:NSStringDrawingUsesLineFragmentOrigin
                              attributes:@{NSFontAttributeName:textFont,
                                           NSParagraphStyleAttributeName:
                                               paragraphStyle}
                                 context:nil];



rect = CGRectIntegral(rect);

答案 2 :(得分:0)

如果您要使用firstLineHeadIndent,则必须将NSMutableParagraphStyle设置为某个值,因为它被视为paragraph

如果没有firstLineHeadIndentNSMutableParagraphStyle将无效!

因此,设置可忽略的值,如下所示,您的问题可能会解决!!

 paragraphStyle.firstLineHeadIndent = 0.0001f;

更新:

在我的一个项目中,我管理的内容如下,

    // I am using this commented code with my custom class

//    CustomLabel *staticTextLabel = [[CustomLabel alloc]init];
//    NSString *title;
//    
//    staticTextLabel.topInset = 0.0;
//    staticTextLabel.leftInset = 10.0;
//    staticTextLabel.rightInset = 10.0;
//    staticTextLabel.bottomInset = 0.0;


// you can use below code
UILabel *staticTextLabel = [[UILabel alloc]init];
NSString *title = @"your string here";


NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment = NSTextAlignmentJustified;      //justified text
paragraphStyles.firstLineHeadIndent = 0.0001f;                //must have a value to make it work


UIFont *myFont = staticTextLabel.font;

NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyles, NSFontAttributeName : myFont};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString: title attributes: attributes];


// set width according to your case
CGSize maximuLabelSize = CGSizeMake(self.view.frame.size.width - 40 -20, FLT_MAX); //20 is padding space (left right)


CGRect textRect = [attributedString boundingRectWithSize:maximuLabelSize
                                                 options: NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading
                                                 context:nil];


CGFloat height2 = textRect.size.height ;
NSLog(@"height 2 is %f",height2);


// manage width according to your case and height also if any padding is there
CGRect newFrame = CGRectMake(20, 10, self.view.frame.size.width - 40, height2 +20); // 20 is for top bottom padding
staticTextLabel.frame = newFrame;
staticTextLabel.layer.borderWidth = 1.0;
staticTextLabel.layer.borderColor = [[UIColor lightGrayColor]CGColor];
staticTextLabel.attributedText = attributedString;
staticTextLabel.numberOfLines = 0;

它适用于所有设备。你可以尝试!!