如何在 UITextView 中获取已删除的文本?
How to get deleted text in UITextView?
我想删除 UITextView
中的文本。
因为我要减少UITextView
,当'\n'被删除时
但是,shouldChangeTextInRange
委托方法只是 return 最后一个字符串。
所以,例如
UITextView.text = @"ABCD\nEFG";
并且用户删除文本是 @"D\nEFG"
。
那么,return编辑的文本不是'\n'。所以我不知道是否应该减少行数。帮帮我!
简单的解决方案
- (void)textViewDidChange:(UITextView *)textView
{
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
}
With this following code, you can change the height of your UITextView
depending of a fixed width (it's working on iOS 7 and previous version) :
- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
{
UITextView *textView = [[UITextView alloc] init];
[textView setAttributedText:text];
CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)];
return size.height;
}
使用此函数,您将采用 NSAttributedString 和固定宽度 return 所需的高度。
如果你想从你的特定字体的文本中计算帧,需要使用下面的代码:
- (CGSize)text:(NSString *)text sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])
{
CGRect frame = [text boundingRectWithSize:size
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName:font}
context:nil];
return frame.size;
}
else
{
return [text sizeWithFont:font constrainedToSize:size];
}
}
This worked for me for iOS6 and 7:
CGSize textViewSize = [self.myTextView sizeThatFits:CGSizeMake(self.myTextView.frame.size.width, FLT_MAX)];
self.myTextView.height = textViewSize.height;
您知道将在 shouldChangeTextInRange
内更改的文本范围。
在你从shouldChangeTextInRange
returnYES
之前,你可以扫描那个范围内的字符,看看是否有“\n
”或“[=14” =]" 字符,然后您可以通过在范围内找到的新行字符数来减少行号。
您可以使用下面的代码获取UItextView的高度 -
CGSize textViewSize = [text sizeWithFont:[UIFont fontWithName:@"font_name" size:font_size]
constrainedToSize:CGSizeMake(WIDHT_OF_VIEW, FLT_MAX)
lineBreakMode:UILineBreakModeTailTruncation];
将 font_name 和 font_size 替换为您在 UITextView 中使用的实际值。
并将 WIDHT_OF_VIEW 替换为您的 UITextView 的宽度。
您可以使用以下代码获取 UITextView 的高度-
textViewSize.height
我想删除 UITextView
中的文本。
因为我要减少UITextView
,当'\n'被删除时
但是,shouldChangeTextInRange
委托方法只是 return 最后一个字符串。
所以,例如
UITextView.text = @"ABCD\nEFG";
并且用户删除文本是 @"D\nEFG"
。
那么,return编辑的文本不是'\n'。所以我不知道是否应该减少行数。帮帮我!
简单的解决方案
- (void)textViewDidChange:(UITextView *)textView
{
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
}
With this following code, you can change the height of your UITextView depending of a fixed width (it's working on iOS 7 and previous version) :
- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
{
UITextView *textView = [[UITextView alloc] init];
[textView setAttributedText:text];
CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)];
return size.height;
}
使用此函数,您将采用 NSAttributedString 和固定宽度 return 所需的高度。 如果你想从你的特定字体的文本中计算帧,需要使用下面的代码:
- (CGSize)text:(NSString *)text sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])
{
CGRect frame = [text boundingRectWithSize:size
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName:font}
context:nil];
return frame.size;
}
else
{
return [text sizeWithFont:font constrainedToSize:size];
}
}
This worked for me for iOS6 and 7:
CGSize textViewSize = [self.myTextView sizeThatFits:CGSizeMake(self.myTextView.frame.size.width, FLT_MAX)];
self.myTextView.height = textViewSize.height;
您知道将在 shouldChangeTextInRange
内更改的文本范围。
在你从shouldChangeTextInRange
returnYES
之前,你可以扫描那个范围内的字符,看看是否有“\n
”或“[=14” =]" 字符,然后您可以通过在范围内找到的新行字符数来减少行号。
您可以使用下面的代码获取UItextView的高度 -
CGSize textViewSize = [text sizeWithFont:[UIFont fontWithName:@"font_name" size:font_size]
constrainedToSize:CGSizeMake(WIDHT_OF_VIEW, FLT_MAX)
lineBreakMode:UILineBreakModeTailTruncation];
将 font_name 和 font_size 替换为您在 UITextView 中使用的实际值。 并将 WIDHT_OF_VIEW 替换为您的 UITextView 的宽度。
您可以使用以下代码获取 UITextView 的高度-
textViewSize.height