问题
Before I start, details_Label
is UITextView
. I have wrong naming because earlier it was label but I changed to UITextView for copy text and other functionalities.
Below is what I have
details_Label.text = [NSString stringWithFormat:@"%@", [[[detailsArray objectAtIndex:0] valueForKey:@"Details"] substringToIndex:200]];
NSMutableAttributedString *attributedString;
attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", details_Label.text] attributes:@{ NSFontAttributeName : [self adjustDefaultFont:85], NSLigatureAttributeName: @2}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [details_Label.text length])];
details_Label.editable = NO;
details_Label.selectable = YES;
details_Label.tintColor = pBlackColor;
[details_Label sizeToFit];
[details_Label layoutIfNeeded];
[details_Label setTextContainerInset:UIEdgeInsetsMake(0, 0, 0, 0)];
[details_Label.textContainer setSize:details_Label.frame.size];
details_Label.frame = CGRectMake(50*iPhoneFactorX, startX, 980*iPhoneFactorX, details_Label.frame.size.height);
This is how I am displaying the text.
The problem is half word is going in next line is as shown in below image...
At the start I thought it was because of long text, however when I just take first 200 characters, still problem is coming.
details_Label.text = [NSString stringWithFormat:@"%@", [[[detailsArray objectAtIndex:0] valueForKey:@"Details"] substringToIndex:200]];
Any idea what is going wrong?
Edit 1
When I print the NSAttributedString, below is what I get.
NSFont = " font-family: \"Arial-BoldMT\"; font-weight: bold; font-style: normal; font-size: 34.72pt"; NSParagraphStyle = "Alignment 1, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (\n), Lists (\n), BaseWritingDirection -1, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0"; }
回答1:
Finally I found what was going odd...
The string I was coming is as below.
word1 word2 word3 word4 word5
However I was seeing it as
word1 word2 word3 word4 word5
As in my case word1 word2 word3 word4 word5
is one word instead of 5 words, the text remaining text was going to next line which is default.
Hope all got the problem.
The fix was I manually re-typed all text and all is working fine.
This might be happening because client copied the HTML text and pasted in text area.
I found this strange thing happening after doing inspect element in browser for the cPanel
回答2:
I Suggest instead of writing it hardcoded why dont you use storyboard for alingment Step to reah for changing alignment 1). Select your text view 2). go to attribute Inspector and choose alingment accirding to your requirement you could also change the color and other thing instead of hardcoding it
来源:https://stackoverflow.com/questions/40233249/half-word-is-going-on-next-line-created-using-nsmutableattributedstring