我想创建一个比同一UILabel
中文本宽度略宽的UILabel
。我查看了很多问题并尝试了它们的答案,但仍然找不到正确的方法。我可以获取文本的宽度,但无法重新绘制框架。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *Cell = @"cell";
LikeActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];
cell.lbl.backgroundColor = [UIColor colorWithRed:7.0 green:4.0 blue:55.0 alpha:0.7];
NSString *lblString = [NSString stringWithFormat:@"%@", object[@"content"]];
cell.lbl.text = lblString;
float widthIs = [cell.lbl.text
boundingRectWithSize:cell.lbl.frame.size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{ NSFontAttributeName:cell.lbl.font }
context:nil]
.size.width;
float heightIs =
[cell.lbl.text
boundingRectWithSize:cell.lbl.frame.size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{ NSFontAttributeName:cell.lbl.font }
context:nil]
.size.height;
NSLog(@"CELL TEXT WIDTH %f", widthIs);
int x = widthIs + 10;
int y = heightIs;
// ATTEMPT 1
[cell.lbl setFrame:CGRectMake(0,0,x,cell.lbl.frame.origin.y)];
// ATTEMPT 2
cell.lbl.frame = CGRectMake(0, 0, x, cell.lbl.frame.origin.y);
return cell;
}
当我运行这些代码时,没有任何效果,标签没有发生改变,这让我困惑,因为它本应该有所改变。