【问题标题】:Is there a way to get the measurements of \t in an NSString?有没有办法在 NSString 中获取 \t 的测量值?
【发布时间】:2013-01-23 02:43:44
【问题描述】:

如果这是重复的,请告诉我,我会立即将其删除——我一直在寻找答案,因为“\t”返回的搜索不带正斜杠。

我需要找到包含以 \t\t 开头的 NSString 的宽度。使用 sizeWithFont 不会这样做,因为它只是忽略了空格。有没有办法测量标签占用了多少空间?

【问题讨论】:

  • 您尝试过使用 NSAttributedString 吗? stackoverflow.com/questions/3429671/…
  • 没有。我以前没有使用过 NSAttributedString,但对文档的初步浏览并没有让我立即清楚如何使用它来完成此操作。你对我可以用 NSAttributedString 做什么有什么具体的想法吗?

标签: ios objective-c nsstring


【解决方案1】:

不确定您的代码是什么,但这似乎对我有用。

NSString *delMessage = @"\t";
CGSize theSize = [delMessage sizeWithFont:[UIFont fontWithName:@"American Typewriter" size:30]];
NSLog(@"length %f",theSize.width);

日志长度为 8.000000

NSString *delMessage = @"\t\t";
CGSize theSize = [delMessage sizeWithFont:[UIFont fontWithName:@"American Typewriter" size:30]];
NSLog(@"length %f",theSize.width);

日志长度为 16.000000

【讨论】:

  • 嗯,这很容易。哎呀。我认为我的问题是我一直在测量UITextView 的sizeWithFont,并且当它没有考虑\t 时,假设它也适用于NSString。只是为了向您展示 - 永远不要做出最基本的假设!
猜你喜欢
  • 1970-01-01
  • 2011-07-13
  • 1970-01-01
  • 2020-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多