Reputation: 2370
I added text in UITextView
and to understand the boundaries I colored my UITextView
with cyan color.
After google.com there is some extra space which I want to remove.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *desc = [self htmlAfterReplacingTagsAndAddingStyle];
_descriptionTextView.attributedText = [[NSAttributedString alloc] initWithData: [desc dataUsingEncoding:NSUTF8StringEncoding] options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
_descriptionTextView.linkTextAttributes = @{ NSForegroundColorAttributeName: [UIColor blueColor], NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle] };
_descriptionTextView.dataDetectorTypes = UIDataDetectorTypeLink;
_descriptionTextView.scrollEnabled = NO;
[self setTextViewHeight];
}
setTextViewHeight
- (void)setTextViewHeight {
UIFont *font = [UIFont fontWithName:@"OpenSans" size:14.0];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _descriptionContainer.frame.size.width, CGFLOAT_MAX)];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.font = font;
NSString *desc = [self htmlAfterReplacingTagsAndAddingStyle];
label.attributedText = [[NSAttributedString alloc] initWithData: [desc dataUsingEncoding:NSUTF8StringEncoding] options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
[label sizeToFit];
_descriptionTextView.backgroundColor = UIColor.cyanColor;
[_descriptionTextView setContentInset:UIEdgeInsetsMake(0, 0, -80, 0)];
_descriptionContainerHeight.constant = label.frame.size.height;
}
I read about setting content inset but that is not helpful. Changing the value of -80 doesn't have any impact.
I would appreciate some pointers which can help me remove the extra space present at the bottom of uitextView.
Upvotes: 0
Views: 2054
Reputation: 16160
To remove all padding:
[textView setTextContainerInset:UIEdgeInsetsZero];
textView.textContainer.lineFragmentPadding = 0; // to remove left padding
If you want to remove Multiple new lines into single one,
In textViewDidEndEditing:
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\n+" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *trimmedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@" "];
textView.text = trimmedString;
Upvotes: 1
Reputation: 62686
You should be able to get somewhere with textContainerInset (see @ReinierMelian answer). There's also opportunity to vastly simplify the text height measurement...
UIFont *font = [UIFont fontWithName:@"OpenSans" size:14.0];
CGFloat width = _descriptionTextView.bounds.size.width;
NSString *string = [self htmlAfterReplacingTagsAndAddingStyle];
CGRect rect = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:font } context:nil];
CGRect frame = _descriptionTextView.frame;
frame.size = CGSizeMake(width, rect.size.height);
_descriptionTextView.frame = frame;
// due to @ReinierMelian...
_descriptionTextView.extContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
Upvotes: 0
Reputation: 20804
Use
[_descriptionTextView setTextContainerInset:UIEdgeInsetsMake(0, 0, 0, 0)];
as can be readed in UITextView
Class reference
textContainerInset Property The inset of the text container's layout area within the text view's content area.
Declaration
OBJECTIVE-C @property(nonatomic, assign) UIEdgeInsets textContainerInset
Discussion
This property provides text margins for text laid out in the text view. By default the value of this property is (8, 0, 8, 0).
Hope this helps
Upvotes: 2