如何在UITableViewCell的UITextView中一致地绘制NSAttributedString [英] How to consistently draw NSAttributedString in UITextView in UITableViewCell

查看:65
本文介绍了如何在UITableViewCell的UITextView中一致地绘制NSAttributedString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用NSAttributedStrings从UITableViewCell中的UITextViews获得一致的结果.

I am having trouble getting consistent results from UITextViews in a UITableViewCell using NSAttributedStrings.

内部-(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);

结果:

如您所见,前两个似乎以我期望/想要的方式绘制文本.在后两个中,UITextView sizeThatFits方法返回的尺寸要大得多,然后才需要绘制文本,并且文本在框架中居中,而不是紧靠框架顶部.这是一个问题,因为我希望能够基于uitextview框架高度来布局其他视图.

As you can see, the first two appear to draw the text in a way that I would expect/want. In the last two the UITextView sizeThatFits method returns a much larger size then is required to draw the text and the text becomes centered in the frame rather then tight to the top of the frame. This is an issue because I want to be able to layout other views based on the uitextview frame height.

滚动出框并返回后:

After Scrolling out of frame and back in:

现在,当重新使用单元格并重新设置属性字符串时,它变得更加陌生. uitextview以不一致的方式绘制文本.

Now it gets even stranger, when the cells are reused, and the attributed string is set again. the uitextview draws the text in an inconsistent way.

即使将contentInsets设置为

Even Setting the contentInsets to

headerText.contentInset = UIEdgeInsetsMake(-8, -8, -8, -8);

不提供任何一致的结果:

Does not provide any sort of consistent results:

在设置了contentinset的情况下滚动后:

And After Scrolling with contentinset set:

UITextView上是否还有其他属性可以让我获得所需的行为?

Are there other attributes on UITextView that would allow me to get the behavior that I need?

推荐答案

设置先前具有不同属性字符串的UITextView的属性字符串时,必须始终将所有UITextView的与字符串相关的属性设置为nil,例如:

When setting the attributed string of a UITextView that previously had a different attributed string, you must always set all of the UITextView's string-related properties to nil first, e.g.:

self.tv.text = nil;
self.tv.font = nil;
self.tv.textColor = nil;
self.tv.textAlignment = NSTextAlignmentLeft;
self.tv.attributedText = s2;

否则,正如您所发现的那样,先前归因字符串的旧功能仍然会徘徊并影响新归因字符串.

Otherwise, as you have discovered, old features of the previous attributed string still hang around and affect the new attributed string.

不过,总的来说,我不得不说我根本不明白为什么要使用UITextView.如果您不需要用户能够编辑这些属性字符串,请使用UILabel或直接绘制属性字符串以获取尽可能准确的呈现效果. NSAttributedString为您提供了测量尺寸并在该尺寸内绘制所需的全部功能.

In general, though, I have to say I don't see why you're using UITextView at all. If you don't need the user to be able to edit these attributed strings, use UILabel or else just draw the attributed string directly for the most accurate possible rendering. NSAttributedString gives you all the power you need to measure the size and draw within that size.

这篇关于如何在UITableViewCell的UITextView中一致地绘制NSAttributedString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆