【发布时间】:2014-07-16 11:18:43
【问题描述】:
我有两个UILabels L1 和 L2。
- L1的最大宽度为150
- L1的最小宽度会根据文字长度调整
- 两个标签之间的距离为 10。
我的代码,
NSString *s = @"Stringwithoutspacetext";
L1.text = s;
L2.text = @"2";
CGSize textSize = { 150, 21 };
CGRect textRect = [[NSString stringWithFormat:@"%@",s]
boundingRectWithSize:textSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}
context:nil];
L1.frame = CGRectMake(L1.frame.origin.x, L1.frame.origin.y, textRect.size.width, L1.frame.size.height);
L2.frame = CGRectMake(L1.frame.origin.x+textRect.size.width+10, L2.frame.origin.y, L2.frame.size.width, L2.frame.size.height);
当字符串s 中没有空格时,它可以正常工作。
如果字符串有一个空格,即。 s = @"String withspacetext";
输出如下所示,
如果字符串有两个空格,即。 s = @"String with spacetext";
输出如下所示,
为什么空格文本会影响我的代码?我应该对我的代码进行哪些更改才能在上述条件下正常工作?
【问题讨论】:
-
嗯...
L1.text = s;和L1.text = @"2"?我没听懂。 -
@Neeku,
s是 NSString。 -
是的,我的意思是你为什么在
s之后分配@"2"?它不会取代它而不是合并吗? /我很困惑! -
@Neeku,已编辑,它是
L2.text。谢谢 -
啊。行。编辑帮助。那是
L2。 (:
标签: ios objective-c ios7 nsstring uilabel