UITextView的textContainer换行符和最大行数不起作用

时间:2014-02-19 02:38:45

标签: ios objective-c ios7 uitextview

我在它自己的类中有一个UITextView,我正在尝试获得最大行数,以便文本不会超出UITextView的界限,同时字符包装工作,因为它我想永远处于自动换行模式。然而,行高(4.9)正在工作。

我不确定导致此故障的原因。我很感激你提供的帮助。

以下是我正在使用的代码:

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.contentInset = UIEdgeInsetsMake(0.5, 0, 0, 0);
        self.scrollEnabled = NO;
        self.textContainer.maximumNumberOfLines = 9;
        self.textContainer.lineBreakMode = NSLineBreakByCharWrapping;
    }
    return self;
}

- (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect
{
    return 4.9;
}

@end

1 个答案:

答案 0 :(得分:0)

我最终解决了不同的换行模式,不幸的是要解决这个问题:我意识到它不应该是一个没有包装空格字符的错误,比如股票消息应用程序,我只是认为不包装字符是不寻常的,包括空格。

- (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;
        self.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
    }
    return self;
}