提问人:Greg 提问时间:2/10/2018 最后编辑:Greg 更新时间:2/11/2018 访问量:1455
UITextView 不完全适合 NSTextContainer
UITextView does not fit NSTextContainer exactly
问:
我刚刚使用堆栈创建了一个自定义方法,但对大小和帧大小之间的差异感到困惑。文本有一个白色边框,我想知道如何摆脱它(见更新)。最后一个字符后面的区域也是白色的。我已经设置了一个属性来使用与字符串长度匹配的范围,但到目前为止,我还没有找到将此属性应用于整个区域的方法。TextKit
NSTextContainer
NSTextView
backgroundColor
我尝试了各种设置和文本框架的组合。以下语句最接近所需结果,但白色边框仍然存在。NSTextContainer
UITextView
// A
textContainer = [[NSTextContainer alloc] initWithSize:textFrame.size];
// D
UITextView *textView = [[UITextView alloc] initWithFrame: textFrame textContainer: textContainer];
我在这里和这里上浏览了教程,并在这里和这里搜索了 Apple 文档。 我还检查了最近的问题,是的,我的TextKit
appDelegate
[self.window makeKeyAndVisible];
更新
按照这里的答案,白色边框已被消除(感谢 Larme)
在方法的末尾添加了以下语句textView
textView.textContainerInset = UIEdgeInsetsZero;
textView.textContainer.lineFragmentPadding = 0;
如果相关,该应用程序是在 iOS 4 中启动的。它现在使用 Xcode 版本 9.2 运行 iOS 11。它已经使用并且现在正在被考虑(不情愿地)作为解决应用程序测试人员许多请求的一种方式。因此,任何可能有助于解释和解决此问题的指针都将受到欢迎。textView
TextKit
谢谢期待。
下面是该方法的代码。 从数组中读取(未显示)。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;
}
答:
这两个问题现在都解决了。白色边框被删除,用于清除默认设置和(谢谢,Larme)。我隐藏了最后一行的未突出显示部分,方法是删除背景颜色作为字符属性,并将其定义为添加的属性,以最大程度地减少 中的行数。这是最终代码UIEdgeInsetsZero
textContainer.lineFragmentPadding = 0
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;
}
评论
textView.textContainerInset = UIEdgeInsetsZero;
textContainer.lineFragmentPadding = 0;