Note that there are some explanatory texts on larger screens.

plurals
  1. POLine spacing in multi-language layout with Core Text
    primarykey
    data
    text
    <p>The attributed string has only one attribute - 17 point Helvetica NeueUI font - covering the whole string. Line 1~3 are purely English, line 4~6 are mixtures of English and Chinese, line 7~8 are purely Chinese.</p> <p>It is then layouted with CTFramesetter and resultant frame is drawn with CTFrameDraw.</p> <pre><code>// UIView subclass - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor whiteColor]; CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica NeueUI"), 17.f, NULL); _text = [[NSAttributedString alloc] initWithString:string attributes: [NSDictionary dictionaryWithObject:(id)font forKey:(id)kCTFontAttributeName]]; CFRelease(font); _framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)_text); CGMutablePathRef path = CGPathCreateMutable(); CGPathAddRect(path, NULL, self.bounds); _frame = CTFramesetterCreateFrame(_framesetter, CFRangeMake(0, 0), path, NULL); CGPathRelease(path); } return self; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); // Flip the coordinate system. CGContextTranslateCTM(context, 0.f, self.bounds.size.height); CGContextScaleCTM(context, 1.f, -1.f); CGContextSetTextMatrix(context, CGAffineTransformIdentity); CTFrameDraw(_frame, context); CGContextRestoreGState(context); } </code></pre> <p>The problem is that the space between line 7 and 8 (purely Chinese lines) is much smaller than others.</p> <p>For contrast, I put a <code>UILabel</code> below it, with the same font and text. Ignoring the rendering difference of single characters, you can see the line spaces in <code>UILabel</code> are uniform including the last one between two Chinese lines.</p> <p><img src="https://img.skitch.com/20120226-tjxb4siwht2sa988j5r8h42wbt.png" alt="Screenshot"></p> <p><strong>Notice</strong>: this results above is got on a real device. If you run the same code on the simulator, you'll get very different result for the Core Text view — spaces between lines containing Chinese are much larger than others.</p> <ol> <li>Why is the space between Chinese lines smaller(larger)?</li> <li>How can I layout multi-language text with uniform line height using Core Text?</li> </ol> <p>PS: here is the <a href="http://d.pr/gZgi" rel="nofollow noreferrer">sample project</a> you can try yourself.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload