我无法使用NSAttributedStrings在UITableViewCell中从UITextViews获得一致的结果 .
**Inside - (UITableViewCell *)tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
headerText = [[UITextView alloc]initWithFrame:CGRectZero];
[headerText setUserInteractionEnabled:NO];
headerText.tag = HEADER_TEXT;
[cell.contentView addSubview:headerText];
} else {
headerText = (UITextView*)[cell.contentView viewWithTag:HEADER_TEXT];
}
//...setting up attributed strings
[headerText setAttributedText:headerString];
CGSize headerSize = [headerText sizeThatFits:CGSizeMake(246, CGFLOAT_MAX)];
headerText.frame = CGRectMake(45, 8, headerSize.width, headerSize.height);
Results:
正如您所看到的,前两个似乎以我期望/想要的方式绘制文本 . 在最后两个中,UITextView sizeThatFits方法返回一个更大的大小,然后需要绘制文本,文本在框架中居中而不是紧紧到框架的顶部 . 这是一个问题,因为我希望能够根据uitextview框架高度布局其他视图 .
After Scrolling out of frame and back in:
现在它变得更加奇怪,当重复使用单元格时,再次设置属性字符串 . uitextview以不一致的方式绘制文本 .
甚至将contentInsets设置为
headerText.contentInset = UIEdgeInsetsMake(-8, -8, -8, -8);
不提供任何一致的结果:
在使用contentinset设置滚动后:
UITextView上是否还有其他属性可以让我获得我需要的行为?
1 回答
设置以前具有不同属性字符串的UITextView的属性字符串时,必须始终将所有UITextView的字符串相关属性设置为nil,例如:
否则,正如您所发现的,先前属性字符串的旧功能仍然会挂起并影响新的属性字符串 .
但总的来说,我不得不说我不明白你为什么要使用UITextView . 如果您不需要用户能够编辑这些属性字符串,请使用UILabel或者直接绘制属性字符串以获得最准确的渲染 . NSAttributedString为您提供测量大小和在该大小内绘制所需的所有功能 .