的属性串只有一个属性的多语言布局 - 17点Helvetica字体NeueUI - 覆盖整个字符串。 1〜3行纯英文,4〜6行是英文和中文混合,7〜8行纯中文。行距与核心文本
然后将其用CTFramesetter和所得帧layouted画有CTFrameDraw。
// 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);
}
问题是第7行和第8行之间的空格(纯中文行)比其他行小得多。
为了便于比较,我把UILabel
下方,用同样的字体和文字。忽略单个字符的渲染差异,可以看到UILabel
中的行空格是统一的,包括两个中文行之间的最后一行。
注意:这上面的结果是得到了一个真正的设备上。如果你在模拟器上运行相同的代码,核心文本视图会得到非常不同的结果 - 包含中文的行之间的空间比其他空间大得多。
- 为什么中国线之间的距离较小(较大)?
- 如何使用核心文本来布置具有统一线高度的多语言文本?
PS:这里是sample project你可以尝试自己。
如果您不介意,我很乐意在开发人员论坛上看到您的iOS 5答案(直到您可以在此发布)。 – 2011-09-26 02:03:40
等待OP交付。 – mxcl 2011-12-20 00:31:03