BTLabel 1.2.3

BTLabel 1.2.3

测试测试
语言语言 Obj-CObjective C
许可证 MIT
发布上次发布2019 年 9 月

Денис Либит 维护。




BTLabel 1.2.3

  • 作者
  • Денис Либит

BTLabel

Version License Platform

具有垂直文本对齐、内边距、图片和高度计算的 UILabel 子类。

screenshot

特别设计用于与 UITableView 一起使用。

screenshot

使用方法

要运行示例项目,首先克隆库,然后从示例目录运行 pod install

示例

简单初始化

BTLabel *label = [[BTLabel alloc] initWithFrame:CGRectMake(10, 10, 300, 80) edgeInsets:UIEdgeInsetsMake(10, 20, 10, 10)];
label.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
label.verticalAlignment = BTVerticalAlignmentCenter;
label.textAlignment = NSTextAlignmentLeft;

在UITableViewCell中使用

// in interface
@property (nonatomic, strong) NSArray *texts;
@property (nonatomic, strong) UIFont *font;
@property (nonatomic, assign) UIEdgeInsets edgeInsets;

// in implementation
- (void)viewDidLoad
{
	[super viewDidLoad];
	
	self.texts = @[
		@"Text for row 0",
		@"Text for row 1",
		@"Text for row 2",
	];
	
	self.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
	self.edgeInsets = UIEdgeInsetsMake(10, 20, 10, 10);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return [BTLabel heightForWidth:tableView.bounds.size.width
							  text:self.texts[indexPath.row]
							  font:self.font
						edgeInsets:self.edgeInsets
					 numberOfLines:0
						 imageSize:CGSizeZero
					 imagePosition:UIRectEdgeTop
				   imageEdgeInsets:UIEdgeInsetsZero] + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString * const kCellID = @"kCellID";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
	
	if (!cell) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
		
		BTLabel *label = [[BTLabel alloc] initWithFrame:cell.bounds];
		label.tag = 100;
		label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
		label.font				= self.font;
		label.textAlignment		= NSTextAlignmentLeft;
		label.verticalAlignment	= BTVerticalAlignmentTop;
		label.edgeInsets		= self.edgeInsets;
		label.hasImage			= NO;
		
		[cell.contentView addSubview:label];
	}
	
	BTLabel *label = [cell viewWithTag:100];
	label.text = self.texts[indexPath.row];
	
	return cell;
}

安装

BTLabel可以通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中

pod "BTLabel"

作者

Денис Либит, [email protected]

授权协议

BTLabel位于MIT授权协议下。有关更多信息,请参阅LICENSE文件。