lianjie:http://blog.csdn.net/shave_kevin/article/details/46841935
part1:
代码如下:在实际工作过程中 你需要实现宽度和高度自适应。下面有两个方法,一个实现宽度自适应一个实现高度自适应.
//ios6 及其以上同样适应。
//声明
@interface UILabel (AutoSizeMakeWidthORHeight)
- (void)setTitleWidth:(NSString *)title Font:(UIFont *)font BackgroundColor :(UIColor *)bacColor TitleColor :(UIColor *)titleColor;
- (void)setTitleHeight:(NSString *)title Font:(UIFont *)font BackgroundColor :(UIColor *)bacColor TitleColor :(UIColor *)titleColor;
//宏定义
#define SKCGSizeMake [UIScreen mainScreen].bounds.size.width,8000
//实现
@implementation UILabel (AutoSizeMakeWidth)
- (void)setTitleWidth:(NSString *)title Font:(UIFont *)font BackgroundColor :(UIColor *)bacColor TitleColor :(UIColor *)titleColor{
self.text = title;
self.font = font;
self.textColor = titleColor;
self.backgroundColor = bacColor;
CGSize labelsize;
float version = [selfgetSysBundle];
if (version <=6.0) {
labelsize = [self.textsizeWithFont:self.fontconstrainedToSize:CGSizeMake(SKCGSizeMake)lineBreakMode:UILineBreakModeWordWrap];
}
else {
CGRect rect = [self.textboundingRectWithSize:CGSizeMake(SKCGSizeMake)
options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesFontLeading |NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName: self.font}
context:nil];
labelsize = rect.size;
}
[selfsetFrame:CGRectMake(self.frame.origin.x,self.frame.origin.y, labelsize.width, self.frame.size.height)];
}
- (void)setTitleHeight:(NSString *)title Font:(UIFont *)font BackgroundColor :(UIColor *)bacColor TitleColor :(UIColor *)titleColor {
self.text = title;
self.font = font;
self.textColor = titleColor;
self.backgroundColor = bacColor;
self.numberOfLines =0;
CGSize labelsize;
float version = [selfgetSysBundle];
//支持ios 6.0
if (version <=6.0) {
labelsize = [self.textsizeWithFont:self.fontconstrainedToSize:CGSizeMake(SKCGSizeMake)lineBreakMode:UILineBreakModeWordWrap];
} else {
//版本 7.0 +可用
CGRect rect = [self.textboundingRectWithSize:CGSizeMake(SKCGSizeMake)
options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesFontLeading |NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName: self.font}
context:nil];
labelsize = rect.size;
}
[selfsetFrame:CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width, labelsize.height)];
//这句话必须写 sizeToFit
[selfsizeToFit];
}
part2:
实现TextView的高度自适应
前提:只有高度是改变的.其他的位置相对不变.
首先的是把textview 的scrollEnabled 属性设置成NO,这样就不可以滑动了。
然后设置一下textview 的代理
写下textview 的代理方法:
然后重新设置一下frame
- (void)textViewDidChange:(UITextView *)textView {
CGSize labelsize;
CGSize newSize = CGSizeMake(100, 80000);
labelsize = [_textView sizeThatFits:newSize];
_textView.frame = CGRectMake(_textView.frame.origin.x,_textView.frame.origin.y, _textView.frame.size.width, labelsize.height);
}
上面的是用代码实现的textview 没有加约束
下面有一个是用stroryboard 写的使用约束 的
首先我们想改变它的高 把它的高的约束拉出来
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heights;
//拉出来以后在代理方法里面更改高度然后约束改变 就好了
- (void)textViewDidChange:(UITextView *)textView{
CGSize labelsize;
CGSize newSize = CGSizeMake(_textView.frame.size.width, 80000);
labelsize = [_textView sizeThatFits:newSize];
//更改高度
_heights.constant = labelsize.height;
//更改约束
[self updateViewConstraints];
}