我正在尝试创建一个简单的自定义UIView,它包含用单个字体绘制的字符串,但第一个字符稍大。NSString sizeWithFont:返回不一致的结果?已知的错误?
我认为这将很容易实现与两个UILabel:放在彼此旁边。
我使用NSString sizeWithFont来衡量我的字符串能够正确地布局它。
但我注意到,返回的矩形中的字体基线根据我设置的字体大小而变化,具有+/- 1像素。 这里是我的代码:
NSString* ctxt = [text substringToIndex:1];
NSString* ttxt = [text substringFromIndex:1];
CGSize sz = [ctxt sizeWithFont: cfont ];
clbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, sz.width, sz.height)];
clbl.text = ctxt;
clbl.font = cfont;
clbl.backgroundColor = [UIColor clearColor];
[contentView addSubview:clbl];
CGSize sz2 = [ttxt sizeWithFont: tfont];
tlbl = [[UILabel alloc] initWithFrame:CGRectMake(sz.width, (sz.height - sz2.height), sz2.width, sz2.height)];
tlbl.text = ttxt;
tlbl.font = tfont;
tlbl.backgroundColor = [UIColor clearColor];
[contentView addSubview:tlbl];
如果我使用12.0和14.0的尺寸,它工作正常。 但是如果我改用13.0和15.0,那么第一个字符就是1个像素太高。
这是一个已知的问题?
任何建议如何解决它?
用CSS和HTML页面创建一个UIWebView似乎有点矫枉过正。和更多的工作来处理动态字符串。这是我所期望的吗?
我也试图将它作为drawRect实现:使用[NSString drawInRect:]来确保问题不是在UILabel内。 那里有同样的问题。 – 2010-05-31 18:03:18