gpt4 book ai didi

iphone - 从适合一定宽度的字符串中提取字符串

转载 作者:搜寻专家 更新时间:2023-10-30 20:20:52 26 4
gpt4 key购买 nike

如果我有字符串“Life is Good”。现在我需要从宽度为 40 的字符串中提取一些单词。如何从 Objective-C 中实现这一点?

最佳答案

您可以使用 sizeWithFont:循环中的方法,如下所示:

NSString *longestFitting = nil;
NSString *orig = "Life is Good";
UIFont *font = ...;
for (int i = orig.length-1 ; i > 1 ; i--) {
NSString *tmp = [orig substringToIndex:i];
if ([tmp sizeWithFont:font].width <= 40) {
longestFitting = tmp;
break;
}
}

关于iphone - 从适合一定宽度的字符串中提取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11540348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com