Skip to content
This repository

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

aeberbach opened this Issue · 6 comments

2 participants

Adam Eberbach Oliver Drobnik
Adam Eberbach

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]

  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;
Oliver Drobnik

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

Adam Eberbach

self.dtTitleLabel.layoutFrame.noLeadingOnFirstLine = YES;

It doesn't have an effect either way.

Adam Eberbach

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]

  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];
Adam Eberbach

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.

Oliver Drobnik

@aeberbach can you fix it?

Adam Eberbach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.