一、label换行
警告:label的高度起码要有2行的高度,不然,无论怎么设置,都不会出现换行效果。如果要设置TableViewCell里的文字换行,设置cell高度的时候,请先在TableView的xib文件中设置cell的高度,不然cell的高度满足不了换行的高度,是没有效果的。
//换行,一般设置2个属性,lineBreakMode和numberOfLines ,
//lineBreakMode,设置换行模式,模式有很大种,如果是中文换行,就按单词换行,设置属性为NSLineBreakByWordWrapping
//numberOfLines,设置行数,值为0,代表多行。也可以指定行数,列如 numberOfLines = 2;
self.labelSummary.lineBreakMode = NSLineBreakByWordWrapping;
self.labelSummary.numberOfLines = 0;
//在autolayout环境下实现多行label需要将这个属性设定一下,当大于一行时进行自动分行。
self.labelSummary.preferredMaxLayoutWidth = 200;
二、NSNumber基本使用
1、当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。
2、有人说既然都有了NSInteger等这些基础类型了为什么还要有NSNumber?它们的功能当然是不同的。
NSInteger是基础类型,但是NSNumber是一个类。如果想要存储一个数值,直接用NSInteger是不行的,比如在一个Array里面这样用:
NSArray *array= [[NSArray alloc]init];
[array addObject:3];//会编译错误
这样是会引发编译错误的,因为NSArray里面放的需要是一个类,但‘3’不是。这个时候需要用到NSNumber:NSArray *array= [[NSArray alloc]init];
[array addObject:[NSNumber numberWithInt:3]];
Cocoa提供了NSNumber类来包装(即以对象形式实现)基本数据类型。例如以下创建方法:
+ (NSNumber*)numberWithChar: (char)value;
+ (NSNumber*)numberWithInt: (int)value;
+ (NSNumber*)numberWithFloat: (float)value;
+ (NSNumber*)numberWithBool: (BOOL) value;
还有很多初始化方法:
– initWithBool:
– initWithChar:
– initWithDouble:
– initWithFloat:
– initWithInt:
– initWithInteger:
– initWithLong:
– initWithLongLong:
– initWithShort:
– initWithUnsignedChar:
– initWithUnsignedInt:
– initWithUnsignedInteger:
– initWithUnsignedLong:
– initWithUnsignedLongLong:
– initWithUnsignedShort:
- (char)charValue;
- (int)intValue;
- (float)floatValue;
- (BOOL)boolValue;
- (NSString*)stringValue;
三、OC常用的类型转换
1、int转NSString,double转NSString
int useridInt = 1;
NSString *user_id = [NSString stringWithFormat:@"%d", useridInt];
double d = 2.0;
NSString *user_name = [NSString stringWithFormat:@"%lf", d];
四、NSString 常用方法
1、追加字符串,放在原字符串的末尾
NSMutableString *str = @"123456789";
NSLog(@"user_name:%@",[str stringByAppendingString:@"0"]);
2、截取字符串,按照所给出的位置,长度,任意地从字符串中截取子串
NSMutableString *str = @"123456789";
NSLog(@"user_name:%@",[str substringWithRange:NSMakeRange(0, 4)]);
//打印结果为:user_name:1234