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!!
NSLineBreakByWordWrapping Ich habe es aktualisiert – balusu