我在UITextView
中添加了文字,并了解了我使用青色为UITextView
着色的边界。
在google.com之后,我想删除一些额外的空间。
- (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;
}
我读过关于设置内容插入的内容,但这没有帮助。更改-80的值没有任何影响。
我会很感激一些指针可以帮助我删除uitextView底部的额外空间。
答案 0 :(得分:2)
使用
[_descriptionTextView setTextContainerInset:UIEdgeInsetsMake(0, 0, 0, 0)];
可以在UITextView
类引用
textContainerInset属性文本容器布局的插图 文本视图内容区域内的区域。
宣言
目标-C @property(非原子,分配)UIEdgeInsets textContainerInset
讨论
此属性提供文本边距 文本视图中列出的文本。默认情况下,此属性的值 是(8,0,8,0)。
希望这有帮助
答案 1 :(得分:0)
你应该可以使用textContainerInset获取某个地方(请参阅@ReinierMelian答案)。还有机会大大简化文本高度测量...
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);
答案 2 :(得分:0)
删除所有填充:
[textView setTextContainerInset:UIEdgeInsetsZero];
textView.textContainer.lineFragmentPadding = 0; // to remove left padding
如果要将多个新行删除为单个行,
在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;