balusu
balusu

Reputation: 377

NSString text height not getting correct value?

Not getting text height correctly if having new line correctly ("\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];

Even tried with other approaches like size butI am getting same values my text is having "\n" new line character as well.

Upvotes: 0

Views: 111

Answers (3)

Ketan Parmar
Ketan Parmar

Reputation: 27438

You have to set firstLineHeadIndent with some value if you want to use NSMutableParagraphStyle because it is considered as paragraph.

Without firstLineHeadIndent, NSMutableParagraphStyle will not work!

So, set negligible value something like below and your issue may solve!!

 paragraphStyle.firstLineHeadIndent = 0.0001f;

Update :

In one of my project i am managing this something like below,

    // 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;

And it is working in every device. you can try it!!

Upvotes: 0

Bruno Coelho
Bruno Coelho

Reputation: 946

You need to round the rect value. See the example bellow.

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);

Upvotes: 0

Marcus Adams
Marcus Adams

Reputation: 53840

NSLineBreakByClipping is what you use when you want to autoshrink the text to fit. If you're trying to adjust the view to fit the text, then you'll want NSLineBreakByWordWrapping.

Also, if you're getting textWidth from the current width of a control, be sure to get the width in viewWillAppear or later, not in viewDidLoad.

Upvotes: 1

Related Questions