iOS - 固定宽度和字号,获取Label的frame;固定高度和字号,获取Label的frame
2016-01-22 13:06
495 查看
固定宽度和字号,获取Label的frame;固定高度和字号,获取Label的frame
效果图:
代码如下:
- (void) loadSet
{
UILabel * textLabel = [[UILabel alloc]init];
NSString * str = @"天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行夫乱其所为。";
CGSize size = [self getSizeWithStr:str Width:200 Font:20];
textLabel.frame = CGRectMake(20, 90, 200, size.height);
textLabel.text = str;
textLabel.numberOfLines = 0;
textLabel.backgroundColor = [UIColor redColor];
[self.view addSubview:textLabel];
UILabel * textLabel1 = [[UILabel alloc]init];
NSString * str1 = @"temp";
CGSize size1 = [self getSizeWithStr:str1 Height:20 Font:20];
textLabel1.frame = CGRectMake(20, 300, size1.width, 20);
textLabel1.text = str1;
textLabel1.backgroundColor = [UIColor redColor];
[self.view addSubview:textLabel1];
}
#pragma mark - 固定宽度和字体大小,获取label的frame
- (CGSize) getSizeWithStr:(NSString *) str Width:(float)width Font:(float)fontSize
{
NSDictionary * attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
CGSize tempSize = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attribute
context:nil].size;
return tempSize;
}
#pragma mark - 固定高度和字体大小,获取label的frame
- (CGSize) getSizeWithStr:(NSString *) str Height:(float)height Font:(float)fontSize
{
NSDictionary * attribute = @{NSFontAttributeName :[UIFont systemFontOfSize:fontSize] };
CGSize tempSize=[str boundingRectWithSize:CGSizeMake(MAXFLOAT, height)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size;
return tempSize;
}
效果图:
代码如下:
- (void) loadSet
{
UILabel * textLabel = [[UILabel alloc]init];
NSString * str = @"天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行夫乱其所为。";
CGSize size = [self getSizeWithStr:str Width:200 Font:20];
textLabel.frame = CGRectMake(20, 90, 200, size.height);
textLabel.text = str;
textLabel.numberOfLines = 0;
textLabel.backgroundColor = [UIColor redColor];
[self.view addSubview:textLabel];
UILabel * textLabel1 = [[UILabel alloc]init];
NSString * str1 = @"temp";
CGSize size1 = [self getSizeWithStr:str1 Height:20 Font:20];
textLabel1.frame = CGRectMake(20, 300, size1.width, 20);
textLabel1.text = str1;
textLabel1.backgroundColor = [UIColor redColor];
[self.view addSubview:textLabel1];
}
#pragma mark - 固定宽度和字体大小,获取label的frame
- (CGSize) getSizeWithStr:(NSString *) str Width:(float)width Font:(float)fontSize
{
NSDictionary * attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
CGSize tempSize = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attribute
context:nil].size;
return tempSize;
}
#pragma mark - 固定高度和字体大小,获取label的frame
- (CGSize) getSizeWithStr:(NSString *) str Height:(float)height Font:(float)fontSize
{
NSDictionary * attribute = @{NSFontAttributeName :[UIFont systemFontOfSize:fontSize] };
CGSize tempSize=[str boundingRectWithSize:CGSizeMake(MAXFLOAT, height)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size;
return tempSize;
}
相关文章推荐
- IOS开发知识(三)
- IOS开发知识(二)
- IOS开发知识(一)
- iOS的集中collection布局
- iOS数字时间与时间的相互转换
- ios 键盘遮挡,监听键盘调整view的frame
- iOS 集成支付宝支付编译出现的问题及解决办法
- iOS 9之应用内搜索(CoreSpotlight)API
- iOS 学习资料
- iOS 学习资料
- iOS-蓝牙学习1
- iOS开发new与alloc/init的区别
- iOS开发new与alloc/init的区别
- [OCLint]iOS项目中使用OCLint指导手册
- IOS开发防止图片渲染的方法
- iOS 数组遍历删除元素的问题
- iOS程序猿之本地通知那些事
- iOS 关于presentViewController弹出页面反应迟钝的问题
- IOS图片压缩(修改图片大小)
- iOS开发常用的第三方资源