本篇整理了一批在 iOS 開發中經常重複使用的 UI 程式碼片段,涵蓋常見控件的建立與布局計算,方便隨時查閱與複製。
{
UILabel* lb = [[UILabel alloc] initWithFrame:CGRectZero];
lb.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_lbEmptyInfo = lb;
lb.frame = CGRectMake(100, 100, 100, 14);
lb.textColor = COLORRGB(0x1e1e1e);
lb.textAlignment = NSTextAlignmentLeft;
lb.backgroundColor = [UIColor clearColor];
lb.font = [UIFont systemFontOfSize:12];
lb.text = @"還沒有發布任何約伴信息哦!";
[self.view addSubview:lb];
}
{
UIButton* bt = [UIButton buttonWithType:UIButtonTypeCustom];
bt.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[bt setBackgroundImage:[[UIImage imageNamed:@"yb_bt_bg_1_nor@2x"]
resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)] forState:UIControlStateNormal];
_btEmptyCreate = bt;
bt.frame = CGRectMake(100, 100, 100, 30);
[bt addTarget:self action:@selector(forwardCreatePostVC:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor clearColor];
bt.titleLabel.font = [UIFont systemFontOfSize:12];
[bt setTitle:@"立即發布" forState:UIControlStateNormal];
[bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.view addSubview:bt];
}
{
UIView* v = [UIView new];
v.frame = CGRectMake(100, 100, 100, 30);
v.autoresizingMask = UIViewAutoresizingFlexibleWidth;
v.backgroundColor = COLORRGB(0xd1d4d4);
[self.view addSubview:v];
}
{
UIImageView* iv = [UIImageView new];
iv.frame = CGRectMake(0, 0, 35, 35);
iv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
iv.backgroundColor = [UIColor clearColor];
iv.image = [UIImage imageNamed:@"search-calendar-icon"];
[self.view addSubview:iv];
}
{
UITableView* tb = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tbItems = tb;
tb.tableFooterView = [UIView new];
tb.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
tb.separatorColor = COLORRGB(0xd6d6d6);
tb.separatorStyle = UITableViewCellSeparatorStyleNone;
tb.delegate = self;
tb.dataSource = self;
tb.backgroundColor = MAIN_SECTION_BG_COLOR;
[self.view addSubview:tb];
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
{
UITextView* tv = [[UITextView alloc] initWithFrame:CGRectZero];
tv.frame = CGRectMake(10, 10, 100, 100);
tv.tag = TAG_CELL_TV_NOTE;
tv.textColor = COLORRGB(0x606565);
tv.textAlignment = NSTextAlignmentRight;
tv.backgroundColor = [UIColor clearColor];
tv.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:tv];
}
{
UISwitch* sw = [[UISwitch alloc] initWithFrame:CGRectZero];
sw.frame = CGRectMake(cell.frame.size.width-33-10, (45-20)/2, 33, 20);
sw.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
[sw addTarget:self action:@selector(handlerSwitchEvent:) forControlEvents:UIControlEventValueChanged];
sw.tag = TAG_CELL_SW_SWITCH;
sw.onTintColor = MAIN_COLOR_2;
[cell.contentView addSubview:sw];
}
{
UITextField* tf = [[UITextField alloc] initWithFrame:CGRectZero];
tf.frame = CGRectMake(80, 16, cell.frame.size.width-80-10, 15);
tf.tag = TAG_CELL_TF_VALUE;
[tf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
tf.borderStyle = UITextBorderStyleNone;
tf.textColor = COLORRGB(0x787c7c);
tf.textAlignment = NSTextAlignmentRight;
tf.backgroundColor = [UIColor clearColor];
tf.font = [UIFont systemFontOfSize:13];
[cell.contentView addSubview:tf];
}
忽略行數,取得 UILabel 的最大尺寸:
{
UILabel* lb = (id)[_vTableHeader viewWithTag:TAG_HEADER_LB_REMARK];
CGSize maximumLabelSize = CGSizeMake(lb.frame.size.width, 1000);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:lb.font, NSFontAttributeName, nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:lb.text attributes:attributesDictionary];
CGRect newFrame = [string boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CHANGE_FRAME_HEIGTH(lb, newFrame.size.height);
CHANGE_FRAME_Y(lb, CGRectGetMaxY([_vTableHeader viewWithTag:TAG_HEADER_V_LOCATION_FLOW].frame)+8);
}
當行數大於 1 時,透過 sizeThatFits 取得尺寸:
{
UILabel* lb = (id)_lbSignature;
CGSize maximumLabelSize = CGSizeMake(lb.frame.size.width, 1000);
CGSize newSize = [lb sizeThatFits:maximumLabelSize];
CHANGE_FRAME_HEIGHT(lb, newSize.height);
}
這些程式碼片段涵蓋了 iOS 開發中最高頻的控件建立與文字尺寸計算場景,整理成庫能顯著提升日常開發效率。