1定血、封裝一個(gè)nsobject 對(duì)象
@interface RkyMyControl : NSObject
+(UIButton*)createButtonWithimageName:(NSString*)imageName bgImageName:(NSString*)bgImageName title:(NSString*)title color:(UIColor *)color;
+(UILabel*)createLabelWithFont:(float)font Text:(NSString*)text Color:(UIColor *)color;
+(CGSize)sizeWithString:(NSString *)string font:(UIFont *)font andwidth:(CGFloat)width;
@end
2舞虱、實(shí)現(xiàn)這些方法
#import "RkyMyControl.h"
@implementation RkyMyControl
+(UIButton*)createButtonWithimageName:(NSString*)imageName bgImageName:(NSString*)bgImageName title:(NSString*)title color:(UIColor *)color {
UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
if (imageName) {
[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}
if (bgImageName) {
[button setBackgroundImage:[UIImage imageNamed:bgImageName] forState:UIControlStateNormal];
}
if (title) {
[button setTitle:title forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateDisabled];
[button setTitleColor:color forState:UIControlStateNormal];
}
return button;
}
/*
options:NSStringDrawingTruncatesLastVisibleLine| NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading//采用換行模式
*/
+(CGSize)sizeWithString:(NSString *)string font:(UIFont *)font andwidth:(CGFloat)width
{
CGRect rect = [string boundingRectWithSize:CGSizeMake(width, 800)//限制最大的寬度和高度
options: NSStringDrawingUsesLineFragmentOrigin//采用換行模式
attributes:@{NSFontAttributeName: font}//傳入的字體字典
context:nil];
return rect.size;
}
+(UILabel*)createLabelWithFont:(float)font Text:(NSString*)text Color:(UIColor *)color
{
//[[UILabel appearance]setTextColor:[UIColor redColor]];
UILabel*label=[[UILabel alloc]init];
//設(shè)置字體大小
label.font=[UIFont systemFontOfSize:font];
label.textColor=color;
//設(shè)置對(duì)齊方式
label.textAlignment=NSTextAlignmentLeft;
//設(shè)置行數(shù)
label.numberOfLines=0;
//設(shè)置折行方式? NSLineBreakByCharWrapping NSLineBreakByWordWrapping
label.lineBreakMode=NSLineBreakByWordWrapping;
//設(shè)置陰影的顏色
// label.shadowColor=[UIColor yellowColor];
//設(shè)置陰影的偏移
// label.shadowOffset=CGSizeMake(2, 2);
//設(shè)置文字
if (text) {
label.text=text;
}
return label;
}
3、使用方法說明
//計(jì)算_activityTitle.height的高度 ?重新排列控件的frame
-(void)layoutSubView:(NSString *)titleStr
{
CGFloat titleHeight=[RkyMyControl sizeWithString:titleStr font:titleFont andwidth:titleWidth].height;
_activityTitle.height=titleHeight;
_activitytime.top=CGRectGetMaxY(_activityTitle.frame)+10;
_activityaddress.top=CGRectGetMaxY(_activitytime.frame)+5;
_activitymember.top=CGRectGetMaxY(_activityaddress.frame)+5;
lineL.top=CGRectGetMaxY(_activitymember.frame)+10;
}
在uitableView :-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
返回cell 的高度
IWActive *activeM=_commactivityArrary[indexPath.row];
CGFloat titleHeight=[RkyMyControl sizeWithString:activeM.title font:[UIFont systemFontOfSize:14] andwidth:150].height;
CGFloat cellHeight=titleHeight+75;
return cellHeight;
UItableViewCell文字自適應(yīng)的高度就大功告成了当窗!是不是很簡(jiǎn)單啊寸宵!