实现UILabel高度和宽度的自适应和UITextView的高度自适应
来源:互联网 发布:监控要不要网络 编辑:程序博客网 时间:2024/05/03 23:34
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 = [_textViewsizeThatFits:newSize];
_textView.frame =CGRectMake(_textView.frame.origin.x,_textView.frame.origin.y,_textView.frame.size.width, labelsize.height);
}
上面的是用代码实现的textview 没有加约束
下面有一个是用stroryboard 写的使用约束 的
首先我们想改变它的高 把它的高的约束拉出来
@property (weak,nonatomic) IBOutletNSLayoutConstraint *heights;
//拉出来以后在代理方法里面更改高度然后约束改变 就好了
- (void)textViewDidChange:(UITextView *)textView{
CGSize labelsize;
CGSize newSize =CGSizeMake(_textView.frame.size.width,80000);
labelsize = [_textViewsizeThatFits:newSize];
//更改高度
_heights.constant = labelsize.height;
//更改约束
[selfupdateViewConstraints];
}
- 实现UILabel高度和宽度的自适应和UITextView的高度自适应
- 实现UILabel高度和宽度的自适应和UITextView的高度自适应
- UILabel的高度和宽度自适应
- UILabel的高度和宽度自适应
- UILabel和UITextView自适应高度
- UILabel和UITextView自适应高度
- UILabel和UITextView自适应高度
- UILabel自适应宽度和高度
- UILabel自适应里面的文字,自动调整宽度和高度
- UILabel自适应里面的文字,自动调整宽度和高度
- UILabel自适应里面的文字,自动调整宽度和高度
- UILabel 文字自适应调整高度和宽度
- UILabel, UITextView 高度自适应
- UILabel, UITextView 高度自适应
- UIlabel自适应高度和UITableViewCell自适应高度
- UILabel的自适应 高度
- UILabel的高度自适应
- UILabel的自适应高度
- android 系统文件
- Java 中的 static 使用之静态方法
- poj2750--Potted Flower(线段树)
- Springmvc+Hibernate+JPA Maven环境搭
- hbase协处理器endpoint应用:hbase数据加盐(Salting)后的数据查询方法
- 实现UILabel高度和宽度的自适应和UITextView的高度自适应
- linux软件编译安装:make与configure
- try catch finally执行顺序
- Java内存模型
- 一个http请求的详细过程
- php中curl扩展学习
- 正式开始android学习
- 《爱因斯坦文集(第一卷)》【美】爱因斯坦 著
- Maven