在IOS中绘制字符串时,默认情况下文本以左上角为基线。当使用具有相同 y 坐标的不同字体大小的多个 字符串时,这可能会导致问题。
问题;
如何在第一行的左下角基线一个字符串。
我该如何实现?
Br,
蒂姆
最佳答案
我终于找到了解决问题的方法。
通过从 rect.origin.y 中减去 UIFont ascender 属性,我可以将文本设置为左下角的基线。
引用 ascender 属性的 UIFont 文档:
"The top y-coordinate, offset from the baseline, of the receiver’s longest ascender." https://developer.apple.com/library/ios/documentation/uikit/reference/UIFont_Class/Reference/Reference.html#//apple_ref/occ/instp/UIFont/ascender
- (void)drawTextBaseLined:(NSString *)text
rect:(CGRect)rect
font:(UIFont *)font
{
if ([text length] == 0) return false;
NSDictionary *attributes = @{NSFontAttributeName : font};
rect.origin.y -= font.ascender;
[text drawWithRect:rect
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
}
此方法适用于一行或多行文本以及不同的字体大小。 我希望我的解决方案也能帮助其他人。
Br,
蒂姆
关于ios - Objective-C - 左下角的文本基线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25767647/