我使用以下代码来获取乌尔都语的大小,但它计算的是错误的宽度,请帮我找到乌尔都语字符串的正确宽度。
CGSize size = [str sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"Jameel-Noori-Nastaleeq" size:18.0f]}];
答案 0 :(得分:0)
尝试以下方法。他们使用CoreText
。
获取特定文本的高度,约束到特定宽度:
- (CGFloat) heightWithAttributedString:(NSAttributedString*)renderedAttributedText maxWidth:(CGFloat) width {
if (!renderedAttributedText) {
return 0.0;
}
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFTypeRef)(renderedAttributedText));
CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(width, CGFLOAT_MAX), NULL);
CFRelease(framesetter);
return suggestedSize.height;
}
获取特定文本的宽度,约束到特定高度:
- (CGFloat) widthWithAttributedString:(NSAttributedString *)renderedAttributedText maxHeight:(CGFloat)height {
if (!renderedAttributedText) {
return 0.0;
}
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFTypeRef)(renderedAttributedText));
CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(CGFLOAT_MAX, height), NULL);
CFRelease(framesetter);
return suggestedSize.width;
}