[英]How to limit UILabel text using objective C?
I have created Tableview cell
with custom UILabel
. 我用自定义UILabel
创建了Tableview cell
。 For this UILabel
data, I am loading from NSMutableArray
. 对于这个UILabel
数据,我从NSMutableArray
加载。 NSMutableArray
data loading from JSON
. 从JSON
加载NSMutableArray
数据。 Now the problem is I need to limit the tableview cell UILabel
text. 现在问题是我需要限制tableview单元格UILabel
文本。 Here below one example 下面是一个例子
Now : 现在:
-------------------------
12.132454556
-------------------------
Needed : 需要的:
-------------------------
12.13
-------------------------
My Code : 我的代码:
pointbl.text = [NSString stringWithFormat:@"%@",[arraydata objectAtIndex:indexPath.row]];
Above tableview cell label data I need to show 5 digits
only including "."
以上tableview单元格标签数据我需要show 5 digits
仅包括"."
. 。 Some times NSMutableArray value will come "null"
, So based on that also I need to make some solution. 有时NSMutableArray值会变为"null"
,所以基于此我也需要做出一些解决方案。
I have tried by using below code but, If my value "null"
then its throwing exceptions. 我试过使用下面的代码,但是,如果我的值"null"
然后它抛出异常。
temp = [[arraydata objectAtIndex:indexPath.row]stringValue];
if ([temp length] > 5) {
NSRange range = [temp rangeOfComposedCharacterSequencesForRange:(NSRange){0, 5}];
temp = [temp substringWithRange:range];
}
The simplest solution would be to add temp != nil
to the if statement. 最简单的解决方案是将temp != nil
添加到if语句中。
However, you may implement this in a different way. 但是,您可以以不同的方式实现此功能。 What's the type of the objects that are saved in arraydata
? 在arraydata
中保存的对象的类型是什么? If's it a float value (and not a string, as can be concluded from your code samples), you can simply use NSNumberFormatter
for the display. 如果它是一个浮点值(而不是字符串,可以从您的代码示例中得出结论),您可以简单地使用NSNumberFormatter
进行显示。 Even simpler, you can use: [NSString stringWithFormat:@"0.2f", floatValue]
and it will give you at least most of what you're looking to achieve. 更简单的是,您可以使用: [NSString stringWithFormat:@"0.2f", floatValue]
,它至少会为您提供您想要实现的大部分内容。
// as you are loading values from json so, it can have "null" value ("null" in string format, different from nil if object is empty) //当你从json加载值时,它可以有“null”值(字符串格式为“null”,如果object为空则不同于nil)
float a = [yourValue floatValue];
label.text = [NSString stringWithFormat:@"%.2f",a];
Try to convert to float: 尝试转换为float:
if ([arraydata objectAtIndex:indexPath.row] != nil) {
pointbl.text = [NSString stringWithFormat:@"%.2f%%",[[arraydata objectAtIndex:indexPath.row]floatValue]];
}
else {
pointbl.text = @"0";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.