UITextView的文字超越了界限 [英] UITextView's text going beyond bounds

查看:117
本文介绍了UITextView的文字超越了界限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不可滚动的UITextView,它的layoutManager maximumNumberOfLines设置为9,工作正常,但是,我似乎无法在NSLayoutManager中找到一个限制文本不超出UITextView框架的方法。

I have a non-scrollable UITextView with it's layoutManager maximumNumberOfLines set to 9, which works fine, but, I cannot seem to find a method in NSLayoutManager that restricts the text to not go beyond the frame of the UITextView.

例如,在此屏幕截图中,光标位于第9行(第1行被剪切在屏幕截图的顶部,因此请忽略它)。如果用户继续键入新字符,空格或点击返回键,则光标将继续在屏幕外显示,并且UITextView的字符串将继续变长。

Take for example in this screenshot, the cursor is on the 9th line (the 1st line is clipped at top of screenshot, so disregard that). If the user continues to type new characters, spaces, or hit the return key, the cursor continues off screen and the UITextView's string continues to get longer.

我不想限制金额UITextView的字符,由于外国字符大小不同。

I don't want to limit the amount of characters of the UITextView, due to foreign characters being different sizes.

我一直试图解决这个问题几周;我非常感谢任何帮助。

I've been trying to fix this for several weeks; I'd greatly appreciate any help.

CustomTextView.h

CustomTextView.h

#import <UIKit/UIKit.h>

@interface CustomTextView : UITextView <NSLayoutManagerDelegate>

@end

CustomTextView.m

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

更新,仍然未解决

推荐答案

我认为这是一个更好的答案。每当调用shouldChangeTextInRange委托方法时,我们调用我们的didFit:string:range函数来查看结果文本高度是否超出视图高度。如果确实如此,我们返回NO以防止发生更改。

Here is a better answer I think. Whenever the shouldChangeTextInRange delegate method is called we call our doesFit:string:range function to see whether the resulting text height exceeds the view height. If it does we return NO to prevent the change from taking place.

-(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
    return [self doesFit:textView string:text range:range];

}
- (float)doesFit:(UITextView*)textView string:(NSString *)myString range:(NSRange) range;
{
    // Get the textView frame
    float viewHeight = textView.frame.size.height;
    float width = textView.textContainer.size.width;

    NSMutableAttributedString *atrs = [[NSMutableAttributedString alloc] initWithAttributedString: textView.textStorage];
    [atrs replaceCharactersInRange:range withString:myString];

    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:atrs];
    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize: CGSizeMake(width, FLT_MAX)];
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];

    [layoutManager addTextContainer:textContainer];
    [textStorage addLayoutManager:layoutManager];
    float textHeight = [layoutManager
            usedRectForTextContainer:textContainer].size.height;
    FLOG(@" viewHeight = %f", viewHeight);
    FLOG(@" textHeight = %f", textHeight);

    if (textHeight >= viewHeight - 1) {
        FLOG(@" textHeight >= viewHeight - 1");
        return NO;
    } else
        return YES;
}

编辑
如果你还需要添加一些支票更改文本的格式。在我的情况下,用户可以更改字体或使其变粗,更改段落样式等。所以现在任何这些更改也可能导致文本超出textView边框。

EDIT OK you will also need to add some checks if you change the format of the text. In my case the user can change the font or make it bold, change paragraph style, etc.. So now any of these changes could also cause the text to exceed the textView borders.

首先,您需要确保使用textViews undoManager注册这些更改。请参阅下面的示例(我只是复制整个attributionString,以便在调用撤消时将其放回)。

So first you need to make sure you are registering these changes with the textViews undoManager. See below for an example (I just copy the whole attributedString so I can put it back if undo is called).

// This is in my UITextView subclass but could be anywhere

// This gets called to undo any formatting changes 
- (void)setMyAttributedString:(NSAttributedString*) atstr {
    self.attributedText = atstr;
    self.selectedRange = _undoSelection;
}
// Before we make any format changes save the attributed string with undoManager
// Also save the current selection (maybe should save this with undoManager as well using a custom object containing selection and attributedString)
- (void)formatText:(id)sender {
    //LOG(@"formatText: called");
    NSAttributedString *atstr = [[NSAttributedString alloc] initWithAttributedString:self.textStorage];
    [[self undoManager] registerUndoWithTarget:self
                               selector:@selector(setMyAttributedString:)
                                 object:atstr];
    // Remember selection
    _undoSelection = self.selectedRange;

   // Add text formatting attributes
   ...
   // Now tell the delegate that something changed
   [self.delegate textViewDidChange:self];
}

现在检查委托中的大小,如果不适合则撤消。

Now check the size in the delegate and undo if it does not fit.

-(void)textViewDidChange:(UITextView *)textView {
    FLOG(@" called");
    if ([self isTooBig:textView]) {
        FLOG(@" text is too big so undo it!");
        @try {
            [[textView undoManager] undo];
        }
        @catch (NSException *exception) {
            FLOG(@" exception undoing things %@", exception);
        }
    }
}

这篇关于UITextView的文字超越了界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆