我想限制我的iPad
应用程序的用户输入最多20行时,输出的pdf。用户将文本输入到UITextView
(如下图所示),然后将其转换为pdf文档。
我意识到,我将不得不做一些计算,这取决于输入的文本,因为他们键入。这里有什么方法可以帮助我吗?textFieldDidEndEditing
只允许我在输入之后检查它。
而且,由于这样做要简单得多,是否有办法限制UITextField
可以的行数?
发布于 2013-10-20 15:08:06
您与textFieldDidEndEditing
委托方法关系密切。
不过,更好的方法是响应textView:shouldChangeTextInRange:replacementText:
委托方法,然后查看即将发生的更改是否会导致允许的更多行。
下面是一个如何做到这一点的例子:
- (BOOL) textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
static const NSUInteger MAX_NUMBER_OF_LINES_ALLOWED = 3;
NSMutableString *t = [NSMutableString stringWithString:
self.textView.text];
[t replaceCharactersInRange: range withString: text];
NSUInteger numberOfLines = 0;
for (NSUInteger i = 0; i < t.length; i++) {
if ([[NSCharacterSet newlineCharacterSet]
characterIsMember: [t characterAtIndex: i]]) {
numberOfLines++;
}
}
return (numberOfLines < MAX_NUMBER_OF_LINES_ALLOWED);
}
我在https://github.com/st3fan/StackOverflow/tree/master/TextViewWithLineLimit网上做了一个完整的例子。在iOS 7.0.2上进行测试。享受吧!
发布于 2013-11-24 04:54:33
下面的内容与'\n‘+包装行支持一起工作。
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.AutoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.AutocorrectionType = UITextAutocorrectionTypeNo;
self.KeyboardAppearance = UIKeyboardAppearanceAlert;
self.font = [UIFont fontWithName:@"Avenir" size:14];
self.maximumNumberOfLinesAllowed = 4;
self.delegate = self;
}
return self;
}
- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString*)aText
{
NSMutableString *t = [NSMutableString stringWithString: self.text];
[t replaceCharactersInRange: aRange withString: aText];
// First check for standard '\n' (newline) type characters.
NSUInteger numberOfLines = 0;
for (NSUInteger i = 0; i < t.length; i++) {
if ([[NSCharacterSet newlineCharacterSet] characterIsMember: [t characterAtIndex: i]]) {
numberOfLines++;
}
}
if (numberOfLines >= self.maximumNumberOfLinesAllowed)
return NO;
// Now check for word wrapping onto newline.
NSAttributedString *t2 = [[NSAttributedString alloc]
initWithString:[NSMutableString stringWithString:t] attributes:@{NSFontAttributeName:self.font}];
__block NSInteger lineCount = 0;
CGFloat maxWidth = self.frame.size.width;
NSLog(@"maxWidth = %.02f", maxWidth);
NSTextContainer *tc = [[NSTextContainer alloc] initWithSize:CGSizeMake(maxWidth, CGFLOAT_MAX)];
NSLayoutManager *lm = [[NSLayoutManager alloc] init];
NSTextStorage *ts = [[NSTextStorage alloc] initWithAttributedString:t2];
[ts addLayoutManager:lm];
[lm addTextContainer:tc];
[lm enumerateLineFragmentsForGlyphRange:NSMakeRange(0,lm.numberOfGlyphs)
usingBlock:^(CGRect rect,
CGRect usedRect,
NSTextContainer *textContainer,
NSRange glyphRange,
BOOL *stop)
{
lineCount++;
}];
// NSLog(@"%d", lineCount);
return (lineCount <= self.maximumNumberOfLinesAllowed);
}
发布于 2014-04-02 14:17:35
为了限制UITextView中的行数,我只需要:
在以下UITextViewDelegate方法中:
- (void)textViewDidChange:(UITextView *)textView
{
NSUInteger maxNumberOfLines = 5;
NSUInteger numLines = textView.contentSize.height/textView.font.lineHeight;
if (numLines > maxNumberOfLines)
{
textView.text = [textView.text substringToIndex:textView.text.length - 1];
}
}
https://stackoverflow.com/questions/19478679
复制相似问题