如果 :(为什么没有赋值?)(If : (Why is there no value assigned?))
任何人都可以告诉为什么在
if begin:
的条件之后没有分配值if begin:
而只是用冒号结束? 我可以知道它是什么意思吗?if begin: begin = int(begin) end = int(raw_input("End:")) print "word[", begin,":",end,"] ", print word[begin:end] raw_input("\n\nPress the Enter Key to exit.")
谢谢!
Can anybody tell why is there no value assigned to
begin
after the condition atif begin:
and just end with a colon instead? May I know what does it mean?if begin: begin = int(begin) end = int(raw_input("End:")) print "word[", begin,":",end,"] ", print word[begin:end] raw_input("\n\nPress the Enter Key to exit.")
Thank You!
原文:https://stackoverflow.com/questions/29001294
最满意答案
在你的问题中有两个问题:
上面的代码不起作用。 为什么?
起初,我认为文本没有出现,因为它是用白色绘制的。 虽然它可能仍然如此,但这不是唯一的问题。
您将文本绘制到(r)中的矩形与您正在绘制的图层的边界相关。即,如果要绘制图层的左上角,则矩形的原点应为(0, 0)。 上面的代码仍然没有工作。
如果你检查CGSize drawInRect:withFont:... returns(渲染文本的大小),你可以看到渲染文本的大小是0。 这表示矩形绘制的高度为100是不够的。 增加矩形的高度解决了这个问题,现在文本是可见的。 如果你在图层中绘制一些文本,也许最明显的矩形是图层的边界(
CGRect r = [layer bounds];
)。我做了什么来实现它的工作:
- 改变了文字颜色
- 将
r
原点更改为(0,0)r
高度增加有什么不同 [...] ?
首先,你要将文本绘制到图层的图形上下文中(drawLayer:inContext:应该做什么),但在秒示例中,您将向图层的层次结构中添加另一图层,而不是在图层中绘制图层。 每当您的图层重新绘制时,都会添加一个新的文本图层。 所以实际上没有任何东西被绘制到图层上下文中。
接下来发生的事情是,在图层自己绘制之后(空),图层的子图层将自己绘制出来,并且它们的内容将在图层顶部进行组合。 这就是为什么你会看到文字图层的文字。
如果您添加这样的文本图层,您可以通过不必进行任何手动绘图来提高代码效率。 这就要求文本图层在某个早期点(只有一次)被添加到图层中,并且您不需要执行任何自定义绘图(它通常很慢)。
There are two questions in you question:
The above code is not working. Why?
At first I thought that the text didn't show up because it was drawn with white on white. While it may still have been so, that is not the only problem.
The rectangle that you are drawing your text into (r) is relative to the bounds of the layer you are drawing in. I.e. if you want to draw in the top left corner of the layer, the origin of the rect should be (0, 0). The above code is still not working though.
If you inspect the CGSize that drawInRect:withFont:... returns (the size of the rendered text) you can see that the size of the rendered text is 0 wide. This is an indication that the height of 100 for the rectangle to draw in is not enough. Increasing the height of the rectangle solves this and now the text is visible. If you are drawing some text inside a layer, maybe the most sensible rectangle to draw in is the bounds of the layer (
CGRect r = [layer bounds];
).What I did to make it work:
- Changed text color
- Changed origin of
r
to (0,0)- Increased height of
r
What is the difference [...] ?
In the first you are drawing text into the graphics context of the layer (what drawLayer:inContext: is supposed to do) but in the seconds example you are adding another layer to the hierarchy of the layer instead of drawing inside it. Every time your layer will redraw, a new text layer will be added to it. So nothing is actually drawn into the layers graphics context.
What happens next, after your layer has drawn itself (empty), is that the sublayers of your layers draw themselves and their content is composed on top of your layer. That is why you see the text of the text layer.
If you add a text layer like this you can make your code more efficient by not having to do any manual drawing. This requires that the text layer is added to your layer at some earlier point (only once) and that you don't do any custom drawing (it's often slow).
相关问答
更多-
无法为drawInRect设置字体(Can't set font for drawInRect)[2023-08-09]
不要使用字符串作为属性字典键,使用框架提供的常量。 由NSFontAttributeName替换@"NSFontAttributeName"用NSFontAttributeName替换@"NSFontAttributeName" 。 Do not use strings for your attributes dictionary keys, use the constants provided by the framework. Replace @"NSFontAttributeName" by NSFo ... -
垂直对齐,你必须自己做((视图的高度+字符串的高度)/ 2)。 水平对齐可以做: NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; style.alignment = NSTextAlignmentCenter; NSDictionary *attr = [NSDictionary dictionaryWithObject:style forKey:NSParagraphStyle ...
-
设置文本的段落样式(包括换行模式,文本对齐等)有一个关键。 来自文档 : NSParagraphStyleAttributeName 此属性的值为NSParagraphStyle对象。 使用此属性将多个属性应用于一系列文本。 如果不指定此属性,则该字符串使用默认段落属性,由NSParagraphStyle的defaultParagraphStyle方法NSParagraphStyle 。 所以,您可以尝试以下操作: UIFont *font = [UIFont fontWithName:@"Courier" ...
-
我的书提出了两点建议。 如果您知道掩码绘图的边界路径,则可以使用CGPathContainsPoint 。 或者,如果所讨论的图层在外部是透明的并且在内部是不透明的,则可以检查点击位置处的像素以查看它是否透明。 http://www.apeth.com/iOSBook/ch18.html#_hit_testing 向下滚动到“图纸的命中测试”部分。 My book gives two suggestions. If you know the bounding path of the mask drawing ...
-
如果阴影是静态的,那么最好的方法是使用两个UIImageView 。 它比CALayer更聪明,如何处理静态图像(虽然我不知道这是否会在这里产生影响),否则将具有与CALayer相同的好处,例如在GPU上完成所有合成在CPU上(如您的方法2所要求的那样)。 If the shadows are static, then the best way is to use two UIImageViews. It's even smarter than CALayer about how to deal with ...
-
在你的问题中有两个问题: 上面的代码不起作用。 为什么? 起初,我认为文本没有出现,因为它是用白色绘制的。 虽然它可能仍然如此,但这不是唯一的问题。 您将文本绘制到(r)中的矩形与您正在绘制的图层的边界相关。即,如果要绘制图层的左上角,则矩形的原点应为(0, 0)。 上面的代码仍然没有工作。 如果你检查CGSize drawInRect:withFont:... returns(渲染文本的大小),你可以看到渲染文本的大小是0。 这表示矩形绘制的高度为100是不够的。 增加矩形的高度解决了这个问题,现在文本是 ...
-
FWIW, 我经常很快地将CALayer“打开和关闭”,没有任何问题。 所以你可以做到这一点,如果你想! 删除它们并快速重新创建它们似乎不会导致任何问题。 (这不是一个很大的资源用户,我从来没有见过这样做的其他问题。)所以,如果你想要的话,那么一定要这么做! 我其实不明白“命名”图层的含义 - 当然, 就像iVars他们有一个名字 ! 你必须给你的所有CALayers命名 。 以下是一些典型的产品代码(来自.h文件): CALayer *rearLayer; CALayer ...
-
CALayer和drawRect(CALayer and drawRect)[2023-02-05]
通常,您希望尽可能避免覆盖drawRect,尤其是对于您正在设置动画的CALayer对象。 这往往导致非常可怕的表现。 你是什么意思“drawRect:方法只绘制屏幕上的内容?” 它只绘制当前可见的字符串部分? I ended up using an NSTimer. Not the most beautiful solution, but at least it works. -
如何使用drawInRect(How to use drawInRect)[2022-01-18]
这是绘制文本的代码,但这不是您通常在iOS中向屏幕添加文本的方式。 在iOS中,用于向屏幕添加文本的模型通常只是将UILabel添加到视图控制器的视图中,例如,在viewDidLoad ,您可以执行以下操作: - (void)viewDidLoad { [super viewDidLoad]; UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds]; label.textColor = [UIColor r ... -
您需要使用drawInRect:withAttributes:之类的方法之一,并使用属性字符串属性来设置线截断样式。 尝试: NSMutableParagraphStyle *ps = [[NSMutableParagraphStyle alloc] init]; [ps setLineBreakMode:NSLineBreakByTruncatingTail]; [attributes setObject:ps forKey:NSParagraphStyleAttributeName]; You nee ...