Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kanji in string appears to add extra leading to first line of attributed text #372

Closed
aeberbach opened this issue Apr 5, 2013 · 6 comments

Comments

@aeberbach
Copy link
Contributor

Thanks for this great library - it's almost perfect!

In the code below dtTitleLabel is a DTAttributedLabel that is set by some content from the web. Sometimes the content can contain characters other than the western alphabet, which must be supported. The issue is that the first line appears to have an abnormally large amount of leading above it when the line contains those characters. The difference can be observed by uncommenting the alternate value for titleLabelHTMLString.

This is a big deal because it upsets the layout of this label within a table view cell that contains several elements, and the whitespace really sticks out.

If you don't know what might be causing this do you have any pointers on where to look?

  UIFont *titleLabelFont = [UIFont systemFontOfSize:16];
  NSDictionary *builderOptions = @{ DTDefaultFontFamily : [titleLabelFont familyName],
                                    DTDefaultFontSize : [NSNumber numberWithFloat:[titleLabelFont pointSize]]
                                    };
  NSString *titleLabelHTMLString;

  titleLabelHTMLString = [NSString stringWithFormat:@"blender blender"];
  //titleLabelHTMLString = [NSString stringWithFormat:@"blender 湖蚔僻"];

  DTHTMLAttributedStringBuilder *stringBuilder =
  [[DTHTMLAttributedStringBuilder alloc] initWithHTML:[titleLabelHTMLString dataUsingEncoding:NSUTF8StringEncoding]
                                              options:builderOptions
                                   documentAttributes:nil];

  self.dtTitleLabel.attributedString = [stringBuilder generatedAttributedString];

  // set width and height to allow for layout
  CGRect dtTitleBounds = self.dtTitleLabel.bounds;
  dtTitleBounds.size.width = width;
  dtTitleBounds.size.height = FLT_MAX;
  self.dtTitleLabel.bounds = dtTitleBounds;

  // sizeToFit causes bounds to shrink to minimum to enclose content, wrapping lines the width set above
  [self.dtTitleLabel sizeToFit];

  // set origin to move the label to where it is supposed to be
  CGRect dtTitleFrame = self.dtTitleLabel.frame;
  dtTitleFrame.origin.x = currentX;
  dtTitleFrame.origin.y = currentY;
  self.dtTitleLabel.frame = dtTitleFrame;
@odrobnik
Copy link
Collaborator

odrobnik commented Apr 9, 2013

Try setting noLeadingOnFirstLine property of the layout frame to YES in DTAttributedLabel.

@aeberbach
Copy link
Contributor Author

self.dtTitleLabel.layoutFrame.noLeadingOnFirstLine = YES;

It doesn't have an effect either way.

@aeberbach
Copy link
Contributor Author

A better test case illustrating the difference in layout (iOS6) - this can be used in a basic single view Xcode project:


- (void)addTestLabel {

  NSString *testString = [NSString stringWithFormat:@"blender 湖蚔僻"];
  //NSString *testString = [NSString stringWithFormat:@"blender blender"];

  NSDictionary *builderOptions = @{ DTDefaultFontFamily : @"Helvetica",
                                    DTDefaultFontSize : [NSNumber numberWithFloat:14.0],
                                    DTDefaultTextColor : [UIColor yellowColor],
                                    DTUseiOS6Attributes : [NSNumber numberWithBool:YES]
                                    };

  DTHTMLAttributedStringBuilder *stringBuilder =
  [[DTHTMLAttributedStringBuilder alloc] initWithHTML:[testString dataUsingEncoding:NSUTF8StringEncoding]
                                              options:builderOptions
                                   documentAttributes:nil];


  UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 30)];
  testLabel.attributedText = [stringBuilder generatedAttributedString];
  testLabel.backgroundColor = [UIColor blackColor];
  [testLabel sizeToFit];
  [self.view addSubview:testLabel];

  DTAttributedLabel *dtTestLabel = [[DTAttributedLabel alloc] initWithFrame:CGRectMake(10, 50, 300, 30)];
  dtTestLabel.attributedString = [stringBuilder generatedAttributedString];
  dtTestLabel.backgroundColor = [UIColor blackColor];
  [dtTestLabel sizeToFit];
  [self.view addSubview:dtTestLabel];
}

@aeberbach
Copy link
Contributor Author

OK, found the problem. The noLeadingOnFirstLine mechanism is broken - because of the way layoutFrame is handled it is always false in DTCoreTextLayoutFrame's _buildLinesWithTypesetter method.

@odrobnik
Copy link
Collaborator

@aeberbach can you fix it?

@aeberbach
Copy link
Contributor Author

I think so. Will do a pull request when ready.

On 10/04/2013, at 9:01 PM, Oliver Drobnik notifications@github.com wrote:

@aeberbach can you fix it?


Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants