我有一个textview,它以html格式从服务器加载一些文本。 但即使我在文本视图中设置它,文本的字体大小也不会改变,这可能是因为我正在根据服务器的文本内容计算textview高度。
以下是代码
itemDescribtionView = [[[UIView alloc]init] autorelease];
[itemDescribtionView setBackgroundColor:[UIColor whiteColor]];
[mainPageScrollView addSubview:itemDescribtionView];
NSString * itemDescribtionStr = [NSString stringWithFormat:@"%@",[[delegate.detailPageArray objectAtIndex:delegate.detailArraySelectedIndex]objectForKey:@"item_description"]];
NSAttributedString *attributedString = [[NSAttributedString alloc]
initWithData: [itemDescribtionStr dataUsingEncoding:NSUnicodeStringEncoding]
options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
documentAttributes: nil
error: nil];
UITextView * itemDescriptionTextView = [[[UITextView alloc]init]autorelease];
[itemDescriptionTextView setDataDetectorTypes:UIDataDetectorTypeLink];
[itemDescriptionTextView setDelegate:self];
[itemDescriptionTextView setFont:[UIFont fontWithName:appFontRegular size:18]];
[itemDescriptionTextView setUserInteractionEnabled:YES];
[itemDescriptionTextView setScrollEnabled:NO];
[itemDescriptionTextView setEditable:NO];
[itemDescriptionTextView setAttributedText:attributedString];
[itemDescriptionTextView setFrame:CGRectMake(10,0,delegate.windowWidth-20,[self heightForAttributedString:attributedString maxWidth:delegate.windowWidth-20])];
itemDescriptionTextView.contentInset = UIEdgeInsetsMake(-7.0,0.0,0,0.0);
[itemDescribtionView addSubview:itemDescriptionTextView];
- (CGFloat)heightForAttributedString:(NSAttributedString *)text maxWidth:(CGFloat)maxWidth {
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
CGSize size = [text boundingRectWithSize:CGSizeMake(maxWidth, CGFLOAT_MAX) options:options context:nil].size;
NSLog(@"newtext %@",newtext);
NSLog(@"text %@",text);
CGFloat height = size.height + 1; // add 1 point as padding
NSLog(@"%f",height);
if(height<30)
{
height = 30;
}
return height;
}
我尝试在itemDescriptionTextView中的setFrame之后设置setFont并确实改变了字体大小,但是textview的高度不足以显示所有文本。
所以我试图在计算textview的高度之前更改字体。但到目前为止我没有运气。并且想看看是否有人可以指导我通过。谢谢。
答案 0 :(得分:1)
请在创建itemDescriptionTextView之前在此属性字符串上设置字体。
[attributedString addAttributes:@{NSFontAttributeName: [UIFont fontWithName:appFontRegular size:18]} range:NSMakeRange(0, attributedString.length)];