我正在使用以下代码使用 UILabel 创建警报角标(Badge):
[label setText:@"67"];
UIFont * font = [UIFont systemFontOfSize:11];
[label setFont:font];
[label sizeToFit];
[label setTextAlignment:NSTextAlignmentCenter];
[label setBackgroundColor:[UIColor yellowColor]];
[label setTextColor:[UIColor whiteColor]];
label.layer.cornerRadius = label.frame.size.height/2;
一切正常,但文本的垂直对齐方式不正确。它更接近顶部边缘。如何在 UILabel 的垂直中心对齐文本?
我阅读了有关 UIFont 的上升和下降属性,我可以使用它们在 UILabel 的垂直中心对齐文本吗?
最佳答案
您可以使用属性字符串而不是普通的 NSString
作为标签的文本(因此使用 attributedText
属性而不是 text
),并使用 NSBaselineOffsetAttributeName
属性 described here at Apple .
关于ios - 我可以为 System UIFont 设置 ascender 或 descender 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26057358/