2016-09-08 3 views
0

Nicht Höhe bekommt Text richtig, wenn es richtig neue Zeile mit („\ n“)NSString-Texthöhe wird nicht korrekt angezeigt?

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

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

Auch mit anderen Ansätzen wie Größe buti versuchte ich immer gleiche Werte meines Text als auch „\ n“ neue Zeile Zeichen mit .

Antwort

1

NSLineBreakByClipping verwenden Sie, wenn Sie den Text automatisch anpassen möchten. Wenn Sie versuchen, die Ansicht an den Text anzupassen, benötigen Sie NSLineBreakByWordWrapping.

Wenn Sie textWidth von der aktuellen Breite eines Steuerelements erhalten, achten Sie darauf, die Breite in viewWillAppear oder höher zu erhalten, nicht in viewDidLoad.

+0

NSLineBreakByWordWrapping Ich habe es aktualisiert – balusu

0

Sie müssen den rect-Wert runden. Siehe das Beispiel unten.

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

Ich bin mit diesem Problem in iPhone 6s Plus nur konfrontiert – balusu

0

Sie haben firstLineHeadIndent mit einem gewissen Wert setzen, wenn Sie NSMutableParagraphStyle verwenden möchten, weil es als paragraph betrachtet wird.

Ohne firstLineHeadIndent wird nicht funktionieren!

Also, legen Sie vernachlässigbaren Wert so etwas wie unten und Ihr Problem kann lösen !!

paragraphStyle.firstLineHeadIndent = 0.0001f; 

Update:

In einem meinem Projekt ich dies etwas wie leite unten,

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

Und es ist in jedem Gerät arbeiten. du kannst es versuchen!!

+0

Ich bin mit diesem Problem in iPhone 6s Plus nur – balusu

+0

überprüfen Update in Antwort !!!! – Lion

+0

Danke Ketan versuchte mit dem obigen Code, den Sie freigegeben haben, es funktioniert aber immer noch nicht, ohne AttributString zu verwenden – balusu

Verwandte Themen