【问题标题】:UITextView does not fit NSTextContainer exactlyUITextView 不完全适合 NSTextContainer
【发布时间】:2018-02-09 22:24:34
【问题描述】:

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

我尝试了NSTextContainerUITextView 文本框的各种设置组合。以下语句最接近预期结果,但白色边框仍然存在。

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

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

我完成了有关 TextKit herehere 的教程,并搜索了 Apple 文档 here herehere。我还检查了closest question,是的,我的appDelegate

    [self.window makeKeyAndVisible];

更新

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

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

【问题讨论】:

  • 在您当前的代码中添加 textView.textContainerInset = UIEdgeInsetsZero;textContainer.lineFragmentPadding = 0;stackoverflow.com/a/18987810/1801544 ?
  • Larme,白色边框不见了。谢谢。查看更新。
  • 还有问题吗?
  • 尝试使用 CGSize containerSize = CGSizeMake(textFrame.size.width, CGFLOAT_MAX); 摆脱锯齿状的最后一行NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:containerSize];
  • 还通过将“\r”添加到字符串数组来扩展背景 - 例如".....deserunt mollit anim id est labourum。\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r" ]," - 但这是非常糟糕的做法

标签: objective-c ios7 uitextview nsmutableattributedstring nstextcontainer


【解决方案1】:

这两个问题现在都解决了。使用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;
}

【讨论】:

    猜你喜欢
    • 2012-03-09
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 1970-01-01
    相关资源
    最近更新 更多