本文转载至 http://blog.csdn.net/liulichao20/article/details/8957752
//UILabel自动换行,自适应高度
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont systemFontOfSize:14]];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setNumberOfLines:0];
[self.view addSubview:label];
NSString *str = @" fsdfadsfdsfsdfads fdsfads水电费 水电费水电费水电费水电费水电费水电费水电费水电费水电费 水电费水电费水电费 水电费时代fsdf ";
CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:14]constrainedToSize:CGSizeMake(320,500) lineBreakMode:UILineBreakModeWordWrap];
[label setText:str];
[label setFrame:CGRectMake(0.0f, 20.0f, 300, size.height)];
[label release];
//UITextView自动换行,自适应高度
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 150, 320,240)];
textView.backgroundColor = [UIColor clearColor];
textView.text = str;
textView.scrollEnabled = YES;
textView.font = [UIFont systemFontOfSize:14];
textView.userInteractionEnabled = NO;
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview:textView];
本文转载至 http://tangchuanyao.com/20120507760/
自动调整UITextView/UILabel的高度height
很多时候都需要依据用户输入的内容自动调整UILabel/UITextView的高度和宽度,特别是UINavigationController的标题,超过一行的时候默认就是「…」我们希望他能换行表示,这样就需要根据内容调整titleView的高度啦。直接贴sample代码,高度和宽度可以根据自己的需要调整。
UILabel Sample code
1
2
3
4
5
6
7
|
CGRect frame = CGRectMake(20, 0, 280,44);
CGSize labelsize = [titleLabel.text sizeWithFont:[UIFont boldSystemFontOfSize: 16.0f]
constrainedToSize:CGSizeMake(320, 44)
lineBreakMode:UILineBreakModeTailTruncation];
frame.size.width = labelsize.width;
frame.size.height = labelsize.height;
titleLabel.frame = frame;
|
UITextView Sample code
1
2
3
4
5
6
|
CGRect frame = noteTextView.frame;
CGSize size = [noteTextView.text sizeWithFont:noteTextView.font
constrainedToSize:CGSizeMake(280, 1000)
lineBreakMode:UILineBreakModeTailTruncation];
frame.size.height = size.height > 1 ? size.height + 20 : 64;
noteTextView.frame = frame;
|
UITextView是UIScrollView的子类,因此有contentSize属性,也可以按如下实现
1
2
3
|
CGRect frame = noteTextView.frame;
frame.size.height = noteTextView.contentSize.height;
noteTextView.frame = frame;
|
本文转载至 http://blog.sina.com.cn/s/blog_759d3e120101alji.html
苹果API
UILineBreakMode
Options for wrapping and truncating text. (Deprecated. Use NSLineBreakMode
instead.)
typedef enum {
UILineBreakModeWordWrap = 0,
UILineBreakModeCharacterWrap,
UILineBreakModeClip,
UILineBreakModeHeadTruncation,
UILineBreakModeTailTruncation,
UILineBreakModeMiddleTruncation,
} UILineBreakMode;
NSLineBreakMode
These constants specify what happens when a line is too long for its container.
enum {
NSLineBreakByWordWrapping = 0,
NSLineBreakByCharWrapping,
NSLineBreakByClipping,
NSLineBreakByTruncatingHead,
NSLineBreakByTruncatingTail,
NSLineBreakByTruncatingMiddle
};
typedef NSUInteger NSLineBreakMode
lineBreak模式在6.0之前一直用UILineBreakMode枚举类型,6.0使用NSLineBreakMode枚举类型。枚举值中各个值的意义,解释如下:
UILineBreakModeWordWrap = 0,
以单词为单位换行,以单位为单位截断。
UILineBreakModeCharacterWrap,
以字符为单位换行,以字符为单位截断。
UILineBreakModeClip,
以单词为单位换行。以字符为单位截断。
UILineBreakModeHeadTruncation,
以单词为单位换行。如果是单行,则开始部分有省略号。如果是多行,则中间有省略号,省略号后面有4个字符。
UILineBreakModeTailTruncation,
以单词为单位换行。无论是单行还是多行,都是末尾有省略号。
UILineBreakModeMiddleTruncation,
以单词为单位换行。无论是单行还是多行,都是中间有省略号,省略号后面只有2个字符。