Ios 为什么我的NSString不适合使用我的NSString宽度创建的框架?
在我看来,我正在处理的代码应该是这样写的:Ios 为什么我的NSString不适合使用我的NSString宽度创建的框架?,ios,objective-c,uitextview,Ios,Objective C,Uitextview,在我看来,我正在处理的代码应该是这样写的: if (!instructions) { instructions = [[UITextView alloc] init]; instructions.backgroundColor=[UIColor clearColor]; instructions.font = [UIFont fontWithName:@"Helvetica Neue" size:12]; instructions.editable=NO;
if (!instructions)
{
instructions = [[UITextView alloc] init];
instructions.backgroundColor=[UIColor clearColor];
instructions.font = [UIFont fontWithName:@"Helvetica Neue" size:12];
instructions.editable=NO;
instructions.text = @"\nFor millennia, the Japanese haiku has allowed great\nthinkers to express their ideas about the world in three\nlines of five, seven, and five syllables respectively.";
//THIS IS THE RELEVANT LINE:
NSString *t = @"thinkers to express their ideas about the world in three"; //Using this to set width since it's the longest line of the string.
CGSize thisSize = [t sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:12]];
float textWidth = thisSize.width;
const int INSTRUCTIONS_HEIGHT=10; //I know this is an ugly hack.
float textHeight = thisSize.height*INSTRUCTIONS_HEIGHT;
instructions.frame = CGRectMake(screenWidth/2-textWidth/2, screenHeight/2-textHeight/2, textWidth, textHeight);
}
但是输出是这样的,行必须环绕:
当我调整代码以在设置宽度的行中添加几个字符时:
{
instructions = [[UITextView alloc] init];
instructions.backgroundColor=[UIColor clearColor];
instructions.font = [UIFont fontWithName:@"Helvetica Neue" size:12];
instructions.editable=NO;
instructions.text = @"\nFor millennia, the Japanese haiku has allowed great\nthinkers to express their ideas about the world in three\nlines of five, seven, and five syllables respectively.";
//THIS IS THE LINE I CHANGED:
NSString *t = @"thinkers to express their ideas about the world in three lin"; //
CGSize thisSize = [t sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:12]];
float textWidth = thisSize.width;
const int INSTRUCTIONS_HEIGHT=6; //Since there's no wraparound here I could reduce this number.
float textHeight = thisSize.height*INSTRUCTIONS_HEIGHT;
instructions.frame = CGRectMake(screenWidth/2-textWidth/2, screenHeight/2-textHeight/2, textWidth, textHeight);
}
UIEdgeInsets edgeInsets = instructions.contentInset;
CGFloat totalwidth = instructions.frame.size.width - edgeInsets.left - edgeInsets.right;
输出看起来像我想要的:
为什么线条思考者用三个字来表达他们对世界的想法不适合用线条思考者用三个字来表达他们对世界的想法的宽度设置的文本视图
显然,我需要学习如何在屏幕上定位事物。举例来说,我还没有弄清楚这个中心,我怀疑它能解决我的很多问题。但这正是我目前正在使用的,我很好奇。UITextView默认情况下有一些填充。如果需要,您可以删除它:UITextView默认情况下有一些填充。如果需要,可以将其删除:您需要将sizeWithFont限制为文本视图的宽度 比如:
drawnSize = [t sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:12]
constrainedToSize:CGSizeMake(instructions.frame.size.width, 3000)
lineBreakMode:NSLineBreakByWordWrapping ] ;
您可能还需要从文本视图中获取插图,并从宽度中减去它们:
{
instructions = [[UITextView alloc] init];
instructions.backgroundColor=[UIColor clearColor];
instructions.font = [UIFont fontWithName:@"Helvetica Neue" size:12];
instructions.editable=NO;
instructions.text = @"\nFor millennia, the Japanese haiku has allowed great\nthinkers to express their ideas about the world in three\nlines of five, seven, and five syllables respectively.";
//THIS IS THE LINE I CHANGED:
NSString *t = @"thinkers to express their ideas about the world in three lin"; //
CGSize thisSize = [t sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:12]];
float textWidth = thisSize.width;
const int INSTRUCTIONS_HEIGHT=6; //Since there's no wraparound here I could reduce this number.
float textHeight = thisSize.height*INSTRUCTIONS_HEIGHT;
instructions.frame = CGRectMake(screenWidth/2-textWidth/2, screenHeight/2-textHeight/2, textWidth, textHeight);
}
UIEdgeInsets edgeInsets = instructions.contentInset;
CGFloat totalwidth = instructions.frame.size.width - edgeInsets.left - edgeInsets.right;
您需要将sizeWithFont约束为文本视图的宽度 比如:
drawnSize = [t sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:12]
constrainedToSize:CGSizeMake(instructions.frame.size.width, 3000)
lineBreakMode:NSLineBreakByWordWrapping ] ;
您可能还需要从文本视图中获取插图,并从宽度中减去它们:
{
instructions = [[UITextView alloc] init];
instructions.backgroundColor=[UIColor clearColor];
instructions.font = [UIFont fontWithName:@"Helvetica Neue" size:12];
instructions.editable=NO;
instructions.text = @"\nFor millennia, the Japanese haiku has allowed great\nthinkers to express their ideas about the world in three\nlines of five, seven, and five syllables respectively.";
//THIS IS THE LINE I CHANGED:
NSString *t = @"thinkers to express their ideas about the world in three lin"; //
CGSize thisSize = [t sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:12]];
float textWidth = thisSize.width;
const int INSTRUCTIONS_HEIGHT=6; //Since there's no wraparound here I could reduce this number.
float textHeight = thisSize.height*INSTRUCTIONS_HEIGHT;
instructions.frame = CGRectMake(screenWidth/2-textWidth/2, screenHeight/2-textHeight/2, textWidth, textHeight);
}
UIEdgeInsets edgeInsets = instructions.contentInset;
CGFloat totalwidth = instructions.frame.size.width - edgeInsets.left - edgeInsets.right;
一个原因是您正在使用文本视图。你所做的对UILabel来说很好。标签可能有多行。因此,如果您不需要文本视图来滚动文本,那么请考虑使用Lable代替。p>
我不记得确切的数值了。但是对于文本视图,考虑一些模糊的昆虫值,文本本身的实际宽度/空间小于文本视图。p> 一个原因是您正在使用文本视图。你所做的对UILabel来说很好。标签可能有多行。因此,如果您不需要文本视图来滚动文本,那么请考虑使用Lable代替。p>
我不记得确切的数值了。但是对于文本视图,考虑一些模糊的昆虫值,文本本身的实际宽度/空间小于文本视图。p> 有些利润是不被考虑的。有些利润是不被考虑的。是的,我也建议使用约束。但是,这并不能解决UITextView在滚动条等上有昆虫的问题。没错,您可能还需要找到textview的contentInset属性,并从宽度中减去左右插入。@jsd contentInset属性如何?@jsd我没有看到UITextView的contentInset。仅适用于UIScrollView和UIPrintFormatter。能否为UITextView提供Apple文档?UITextView是UIScrollView的一个子类。是的,我也建议使用约束。但是,这并不能解决UITextView在滚动条等上有昆虫的问题。没错,您可能还需要找到textview的contentInset属性,并从宽度中减去左右插入。@jsd contentInset属性如何?@jsd我没有看到UITextView的contentInset。仅适用于UIScrollView和UIPrintFormatter。能否为UITextView提供Apple文档?UITextView是UIScrollView的一个子类。请看,标签对我所做的并不起作用,模糊插入值到目前为止已被证明是无用的。现在我要坚持我已经添加了三个字符,但是我将把这个文件作为一个问题来考虑。谢谢,我真的很感激这个建议。一个标签对于我所做的并不是真的起作用。现在,我将坚持我所添加的三个字符,但是我将把这个问题作为进一步考虑的问题。谢谢,我认为我很欣赏这个建议。奇怪的是,在这个问题中提出的方法应该是可行的,但仍然给我带来了太早的问题。现在,我将坚持我所添加的三个字符,但是我将把这个问题作为进一步考虑的问题。奇怪的是,在这个问题中提出的方法应该是可行的,但仍然给我带来了太早的问题。现在,我将坚持我已经添加了三个字符,但我打算把这个作为一个问题进一步考虑。