Reputation: 547
I have a textview that is loading up some text from server in html format. But the font size of the text is not changing even if I set it in text view, and its probably because I am currently calculating the textview height based on the text contents from server.
Here are the codes
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;
}
I tried to setFont after setFrame in itemDescriptionTextView and it does change the font size, however the height of the textview is not large enough to display all the text.
So I was trying to change the font before the calculation of the height of the textview. But I have no luck so far. And would like to see if anyone can guide me thru. Thank you.
Upvotes: 0
Views: 1123
Reputation: 254
Please set the font on attributed string like this before creating the itemDescriptionTextView.
[attributedString addAttributes:@{NSFontAttributeName: [UIFont fontWithName:appFontRegular size:18]} range:NSMakeRange(0, attributedString.length)];
Upvotes: 1