UITextView完全不适合NSTextContainer

时间:2018-02-09 22:24:34

标签: objective-c ios7 uitextview nsmutableattributedstring nstextcontainer

我刚刚使用TextKit堆栈创建了一个自定义方法,但我感到困惑的是NSTextContainerNSTextView帧大小之间的差异。文本有一个白色边框,我想知道如何摆脱它(请参阅更新)。最后一个字符后面的区域也是白色的。我使用与字符串长度匹配的范围为backgroundColor设置了属性,但到目前为止,我还没有找到将此属性应用于整个区域的方法。

我尝试了NSTextContainer的各种设置组合以及UITextView的文本框架。以下语句最接近所需结果,但白色边框仍然存在。

    // A
    textContainer = [[NSTextContainer alloc] initWithSize:textFrame.size];

    // D 
    UITextView *textView = [[UITextView alloc] initWithFrame: textFrame textContainer: textContainer];

enter image description here

我在TextKit herehere上完成了教程,并通过Apple文档here herehere进行了搜索。我还检查了closest question,是的,appDelegate已经

    [self.window makeKeyAndVisible];

更新

在回答here后,白色边框已被删除(感谢Larme

enter image description here

在方法

的末尾,textView添加了以下语句
    textView.textContainerInset = UIEdgeInsetsZero;
    textView.textContainer.lineFragmentPadding = 0;

如果相关,该应用程序是在iOS 4中启动的。它现在使用Xcode版本9.2运行iOS 11。它已经使用textViewTextKit现在正被考虑(不情愿),以解决来自应用测试人员的大量请求。因此,欢迎任何可能有助于解释和解决此问题的指针。

感谢您的期待。

以下是该方法的代码。从数组中读取helpMessage未显示)。

    -(id)formatHelpText:(NSString*)helpMessage
{
    float sideMargin   = 5;
    float topMargin    = 72;

    CGFloat textWidth  = [UIScreen mainScreen].bounds.size.width - (sideMargin * 2);
    CGFloat textHeight = [UIScreen mainScreen].bounds.size.height - (topMargin * 2);
    CGRect  textFrame  = CGRectMake(sideMargin, topMargin, textWidth, textHeight); // used for A & D


    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:helpMessage];

// define paragraph attributes

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:1.5];
    [style setAlignment:NSTextAlignmentLeft];

// use attributedText to define character attributes

    float spacing        = 0.0f;
    float baselineOffSet = 1.0f;

    [attributedText setAttributes:@{
                                NSFontAttributeName:[UIFont preferredFontForTextStyle:UIFontTextStyleBody],
                                NSBaselineOffsetAttributeName:[NSNumber numberWithFloat:baselineOffSet],
                                NSForegroundColorAttributeName:[UIColor whiteColor],
                                NSBackgroundColorAttributeName:[UIColor grayColor],
                                NSParagraphStyleAttributeName:style,
                                NSKernAttributeName:@(spacing)
                                }
                        range:range];


// TextKit - non-view

    NSTextStorage *textStorage;
    NSLayoutManager *layoutManager;
    NSTextContainer *textContainer;

    textStorage   = [[NSTextStorage alloc] initWithAttributedString:attributedText];
    layoutManager = [[NSLayoutManager alloc] init];
    textContainer = [[NSTextContainer alloc] init];

    [textStorage addLayoutManager:layoutManager];


// A
    textContainer = [[NSTextContainer alloc] initWithSize:textFrame.size];
// B 
//    textContainer = [[NSTextContainer alloc] initWithSize:[UIScreen mainScreen].bounds.size];   
    [layoutManager addTextContainer:textContainer];


// C
//    UITextView *textView = [[UITextView alloc] initWithFrame: [UIScreen mainScreen].bounds textContainer: textContainer];
// D 
    UITextView *textView = [[UITextView alloc] initWithFrame: textFrame textContainer: textContainer];


// update

    textView.textContainerInset = UIEdgeInsetsZero;
    textView.textContainer.lineFragmentPadding = 0;

    return textView;
}

1 个答案:

答案 0 :(得分:0)

这两个问题现在都解决了。使用UIEdgeInsetsZero删除了白色边框以清除默认设置并textContainer.lineFragmentPadding = 0感谢,Larme )。我通过删除背景颜色作为字符属性隐藏了UITextView最后一行的未突出部分,并将其定义为textView. sizeToFit的属性,以最大限度地减少行数textView。这是最终的代码

#import "HelpTextView.h"

@implementation HelpTextView

-(id)formatHelpText:(NSString*)helpMessage
{
    float sideMargin   = 5;
    float topMargin    = 72;

    CGFloat textWidth  = [UIScreen mainScreen].bounds.size.width - (sideMargin * 2);
    CGFloat textHeight = [UIScreen mainScreen].bounds.size.height - (topMargin * 2);
    CGRect  textFrame  = CGRectMake(sideMargin, topMargin, textWidth, textHeight);

    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:helpMessage];

    // define paragraph attributes

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:0.5];
    [style setAlignment:NSTextAlignmentLeft];

    float spacing = 0.0f;
    float baselineOffSet = 1.0f;
    NSRange range = (NSRange){0,[attributedText length]};

    // use attributedText to define character attributes

    [attributedText setAttributes:@{
                                NSFontAttributeName:[UIFont preferredFontForTextStyle:UIFontTextStyleBody],
                                NSBaselineOffsetAttributeName:[NSNumber numberWithFloat:baselineOffSet],
                                NSForegroundColorAttributeName:[UIColor whiteColor],
                                NSParagraphStyleAttributeName:style,
                                NSKernAttributeName:@(spacing)
                                }
                        range:range];

    NSTextStorage* textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedText];
    NSLayoutManager *layoutManager = [NSLayoutManager new];
    [textStorage addLayoutManager:layoutManager];

    CGSize containerSize = CGSizeMake(textFrame.size.width, CGFLOAT_MAX);
    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:containerSize];

    [layoutManager addTextContainer:textContainer];

    UITextView *textView = [[UITextView alloc] initWithFrame: textFrame textContainer: textContainer];
    textView.textContainerInset = UIEdgeInsetsZero;
    textView.textContainer.lineFragmentPadding = 0;
    textView.editable = NO;
    textView.scrollEnabled = NO;
    textView.backgroundColor = [UIColor grayColor];
    [textView sizeToFit];

    return textView;
}