【问题标题】:UITextView's textContainer line break and maximum number of lines not workingUITextView 的 textContainer 换行和最大行数不起作用
【发布时间】:2014-03-19 03:50:05
【问题描述】:

我在它自己的类中有一个 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

【问题讨论】:

  • 你试过self.numberOfLines = 9。我在maximumNumberOfLines 上找不到任何东西。
  • maximumNumberOfLines 是 UITextView 的 textContainer 属性的一个属性
  • 那没有回答问题。你试过self.numberOfLines = 9吗?
  • UITextView 上没有 numberOfLines 属性
  • 哦,垃圾... UILabel 有numberOfLines,而不是UITextView

标签: ios objective-c ios7 uitextview


【解决方案1】:

不幸的是,我最终选择了一种不同的换行模式来解决这个问题:我意识到不换行空格字符应该不是错误,就像股票消息应用程序那样,我只是认为不换行字符是不寻常的,包括空格。

- (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;
}

【讨论】:

    猜你喜欢
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多