IOS 计算UILable字符串高度
2015-05-28 22:05
344 查看
IOS7 之前
截取了部分
@interface ZGViewCell ()
@property (nonatomic, strong) UILabel *nameLable;
@end
- (UILabel *)nameLable{
if (_nameLable == nil) {
_nameLable = [[UILabel alloc] init];
[self.contentView addSubview:_nameLable];
// _nameLable.backgroundColor = [UIColor redColor];
// 默认字体是17,会出现 ”···“
_nameLable.font = [UIFont fontWithName:@"Arial" size:14];
}
return _nameLable;
}
// 位置
- (void)settingFrame{
CGFloat padding = 10;
// 图标
CGFloat iconX = padding;
CGFloat iconY = padding;
CGFloat iconW = 30;
CGFloat iconH = 30;
self.iconImage.frame = CGRectMake(iconX, iconY, iconW, iconH);
// 姓名
UIFont *nameFont = [UIFont fontWithName:@"Arial" size:14.0f];
CGSize nameFrame = [self.statuses.name sizeWithFont:nameFont constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
CGFloat nameX = CGRectGetMaxX(self.iconImage.frame) + padding;
CGFloat nameY = (self.iconImage.frame.size.height - nameFrame.height) * 0.5 + padding;
self.nameLable.frame = CGRectMake(nameX, nameY, nameFrame.width, nameFrame.height);
}
IOS 7 苹果在UIStringDrawing.h中增加了很多方法
与上面对应的
/** boundingRectWithSize计算给定文本字符串所占的区域
返回值是一个x,y = 0的CGRect
如果要计算多行的准确高度,需要传入NSStringDrawingUsesLineFragmentOrigin选项
dict用于指定字体的相关属性的字典,UIKit框架中的第一个头文件
*/
// 位置
- (void)settingFrame{
NSDictionary *nameDict = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
CGRect nameFrame= [str boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nameDict context:nil];
nameFrame.origin.x = CGRectGetMaxX(self.iconImage.frame) + padding;
nameFrame.origin.y = (self.iconImage.frame.size.height - nameFrame.height) * 0.5 + padding;
self.nameLable.frame = nameFrame;
}
截取了部分
@interface ZGViewCell ()
@property (nonatomic, strong) UILabel *nameLable;
@end
- (UILabel *)nameLable{
if (_nameLable == nil) {
_nameLable = [[UILabel alloc] init];
[self.contentView addSubview:_nameLable];
// _nameLable.backgroundColor = [UIColor redColor];
// 默认字体是17,会出现 ”···“
_nameLable.font = [UIFont fontWithName:@"Arial" size:14];
}
return _nameLable;
}
// 位置
- (void)settingFrame{
CGFloat padding = 10;
// 图标
CGFloat iconX = padding;
CGFloat iconY = padding;
CGFloat iconW = 30;
CGFloat iconH = 30;
self.iconImage.frame = CGRectMake(iconX, iconY, iconW, iconH);
// 姓名
UIFont *nameFont = [UIFont fontWithName:@"Arial" size:14.0f];
CGSize nameFrame = [self.statuses.name sizeWithFont:nameFont constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
CGFloat nameX = CGRectGetMaxX(self.iconImage.frame) + padding;
CGFloat nameY = (self.iconImage.frame.size.height - nameFrame.height) * 0.5 + padding;
self.nameLable.frame = CGRectMake(nameX, nameY, nameFrame.width, nameFrame.height);
}
IOS 7 苹果在UIStringDrawing.h中增加了很多方法
与上面对应的
/** boundingRectWithSize计算给定文本字符串所占的区域
返回值是一个x,y = 0的CGRect
如果要计算多行的准确高度,需要传入NSStringDrawingUsesLineFragmentOrigin选项
dict用于指定字体的相关属性的字典,UIKit框架中的第一个头文件
*/
// 位置
- (void)settingFrame{
NSDictionary *nameDict = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
CGRect nameFrame= [str boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nameDict context:nil];
nameFrame.origin.x = CGRectGetMaxX(self.iconImage.frame) + padding;
nameFrame.origin.y = (self.iconImage.frame.size.height - nameFrame.height) * 0.5 + padding;
self.nameLable.frame = nameFrame;
}
相关文章推荐
- iOSUI学习之使用苹果系统自带的方法处理缓存问题
- 115.Distinct Subsequences
- 错误 CUICatalog: Invalid asset name supplied: (null), or invalid scale factor: 2.000000
- Next Permutation&&Permutation Sequence
- NEU 1132 Renew MST Quickly 增量最小生成树
- UVA10099 - The Tourist Guide(floyd + 最小值的最大化)
- 20150528 UI考试项目中,使用SHAI加密,生成请求URL
- [dp]poj1458 -Common Subsequence(LCS)
- 分类UIView+Extension:frame直接赋值方法
- 从客户端检测到有潜在危险的Request.Form值
- 为什么我们可以在非UI线程中更新UI
- XCODE UITextField 中的属性和用法
- Android 4.4 Graphic系统详解(4)HWUI概述
- IOS-UISlider的详细使用
- 用UIScroView实现图片的滚动
- 代码实现UIPickerView
- imagView的UIContentMode的显示方式
- break和continue
- iOS7 - UIActionSheet 按钮不响应点击事件的解决方法
- UITableViewCell使用自动布局计算高度