我有一个不可滚动的UITextView,它的layoutManager maximumNumberOfLines设置为9,这很好用,但是,我似乎在NSLayoutManager中找不到一个方法来限制文本不能超出UITextView的框架。
举个例子,在这个截图中,光标在第9行(第一行在截图的顶部,所以忽略这一点)。如果用户继续键入新字符、空格或按return键,光标将继续离开屏幕,并且UITextView的字符串将继续变长。
我不想限制UITextView的字符数,因为外文字符的大小不同。
几个星期以来,我一直在努力解决这个问题;如果有任何帮助,我将非常感激。
CustomTextView.h
#import <UIKit/UIKit.h>
@interface CustomTextView : UITextView <NSLayoutManagerDelegate>
@end
CustomTextView.m
#import "CustomTextView.h"
@implementation CustomTextView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = [UIColor clearColor];
self.font = [UIFont systemFontOfSize:21.0];
self.dataDetectorTypes = UIDataDetectorTypeAll;
self.layoutManager.delegate = self;
self.tintColor = [UIColor companyBlue];
[self setLinkTextAttributes:@{NSForegroundColorAttributeName:[UIColor companyBlue]}];
self.scrollEnabled = NO;
self.textContainerInset = UIEdgeInsetsMake(8.5, 0, 0, 0);
self.textContainer.maximumNumberOfLines = 9;
}
return self;
}
- (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect
{
return 4.9;
}
@end
更新,仍未解析
发布于 2014-02-26 09:53:44
您可以检查边界矩形的大小,如果它太大,请调用撤消管理器来撤消上一个操作。可以是粘贴操作,也可以输入文本或换行符。
这里有一个快速的技巧,可以检查文本的高度是否太接近textView的高度。还会检查textView rect是否包含文本rect。您可能需要对此进行更多的修改以满足您的需要。
-(void)textViewDidChange:(UITextView *)textView {
if ([self isTooBig:textView]) {
FLOG(@" too big so undo");
[[textView undoManager] undo];
}
}
/** Checks if the frame of the selection is bigger than the frame of the textView
*/
- (bool)isTooBig:(UITextView *)textView {
FLOG(@" called");
// Get the rect for the full range
CGRect rect = [textView.layoutManager usedRectForTextContainer:textView.textContainer];
// Now convert to textView coordinates
CGRect rectRange = [textView convertRect:rect fromView:textView.textInputView];
// Now convert to contentView coordinates
CGRect rectText = [self.contentView convertRect:rectRange fromView:textView];
// Get the textView frame
CGRect rectTextView = textView.frame;
// Check the height
if (rectText.size.height > rectTextView.size.height - 16) {
FLOG(@" rectText height too close to rectTextView");
return YES;
}
// Find the intersection of the two (in the same coordinate space)
if (CGRectContainsRect(rectTextView, rectText)) {
FLOG(@" rectTextView contains rectText");
return NO;
} else
return YES;
}
另一种选择-在这里,我们检查大小,如果它太大,防止任何新字符被键入,除非它是删除。这并不美观,因为如果超过高度,这也会阻止在顶部填充一行。
bool _isFull;
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
FLOG(@" called");
// allow deletes
if (text.length == 0)
return YES;
// Check if the text exceeds the size of the UITextView
if (_isFull) {
return NO;
}
return YES;
}
-(void)textViewDidChange:(UITextView *)textView {
FLOG(@" called");
if ([self isTooBig:textView]) {
FLOG(@" text is too big!");
_isFull = YES;
} else {
FLOG(@" text is not too big!");
_isFull = NO;
}
}
/** Checks if the frame of the selection is bigger than the frame of the textView
*/
- (bool)isTooBig:(UITextView *)textView {
FLOG(@" called");
// Get the rect for the full range
CGRect rect = [textView.layoutManager usedRectForTextContainer:textView.textContainer];
// Now convert to textView coordinates
CGRect rectRange = [textView convertRect:rect fromView:textView.textInputView];
// Now convert to contentView coordinates
CGRect rectText = [self.contentView convertRect:rectRange fromView:textView];
// Get the textView frame
CGRect rectTextView = textView.frame;
// Check the height
if (rectText.size.height >= rectTextView.size.height - 10) {
return YES;
}
// Find the intersection of the two (in the same coordinate space)
if (CGRectContainsRect(rectTextView, rectText)) {
return NO;
} else
return YES;
}
https://stackoverflow.com/questions/21889657
复制相似问题