计算文字的宽度(动态)
2015-11-13 10:09
363 查看
//交易订单数字 UILabel*TONumberLab = [[UILabel alloc] init]; TONumberLab = [self createTitleAndNumberLab:@"567" fontSize:TONumberFontSize]; CGSize TONumberLabSize = [self sizeWithText:TONumberLab.text font:TONumberLab.font fontSize:TONumberFontSize];
#pragma mark 计算文字尺寸 /** * 计算文字尺寸 * * @param text 需要计算尺寸的文字 * @param font 文字的字体 * @param maxSize 文字的最大尺寸 */ - (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font fontSize:(CGFloat)fontSize { CGSize maxSize = CGSizeMake(MAXFLOAT, fontSize); NSDictionary *attrs = @{NSFontAttributeName : font}; return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; }
#pragma mark 创建标题Lab /** * 创建标题/数字Lab * * @param title 标题/数字 * @param fontSize 标题/数字字号大小 * */ -(UILabel*)createTitleAndNumberLab:(NSString*)title fontSize:(CGFloat)fontSize{ //本月收款(元) UILabel*tmpLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; tmpLab.font = [UIFont systemFontOfSize:fontSize]; [tmpLab setTextColor:[UIColor whiteColor]]; [tmpLab setText:title]; return tmpLab; }
不封装计算宽度 //最新订单 UILabel *latestOrderLab = [[UILabel alloc]init]; [latestOrderLab setText:@"最新订单"]; [latestOrderLab setFont:[UIFont systemFontOfSize:LatestOrderAndRefreshLabFontSize]]; [latestOrderLab setTextColor:UIColorFromRGB(0x333333)]; NSDictionary *attrs = @{NSFontAttributeName : latestOrderLab.font}; CGSize maxSize = CGSizeMake(MAXFLOAT, LatestOrderAndRefreshLabFontSize); CGSize latestOrderLabSize = [latestOrderLab.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; CGFloat latestOrderLabX =SpaceLabHeight; CGFloat latestOrderLabY =SpaceLabHeight; CGFloat latestOrderLabW =latestOrderLabSize.width; CGFloat latestOrderLabH =latestOrderLabSize.height; latestOrderLab.frame = CGRectMake(latestOrderLabX, latestOrderLabY, latestOrderLabW, latestOrderLabH); [latestOrderAndRefreshView addSubview:latestOrderLab];
相关文章推荐
- Linux下查看硬件配置信息
- ScrollView中嵌套ListView时显示不全
- Linux_Centos中搭建nexus私服
- redis集群+主从部署
- Google Java编程风格指南
- text-indent失效的原因
- 如何提升团队生产力 Q&A
- iOS 9音频应用播放音频之ios9音频基本功能
- 类似京东淘宝的五星评级,需要的看一下,找找思路
- CocoaPods安装和使用教程
- 获取listview渲染后的高度
- Unsupported compiler 'GCC 4.2' selected for architecture 'i386'
- iOS之应用程序国际化
- 如何学习数字信号处理?——数字信号处理学习方法及学习资料( 原创,若转载请标明出处 )
- 窄巷道托盘货架(VNA)介绍
- 关于CocoaPods详解
- 常见CSS注意问题
- svn:is not under version control and is not part of the commit, yet its child '
- iOS Animations
- Android 将程序从后台转到前台