开发者

Should I cut the NSString before I load it to a UILabel?

I have many quite long NSString, say 500 - 1000 of words for each.

I also have a number of UILabel. The NSStrings are loaded to the UILabels dynamically.

The UILabel has fixed size and may only show 50 - 100 words at most.

So 80% of the words won't be able to be shown on the screen.

So, my question is should I cut the 80% of words off the NSStrings before I load it to the UILabels开发者_开发问答?

My concerns are

  1. Memory usage. if I cut and just load 20% of each NSString to UILabel, will that save memory for me?

  2. Loading performance. I don't know how iOS is really doing about the 80% of the NSString. Will iOS still draw (but no where obviously because of the limited visual size of UILabel) or just cleverly stop drawing for anything out of visual area.

thanks


I suspect UILabel does not bother drawing the pieces of string that you can't see. In any case, they will not use up silly amounts of memory because the bitmap context they're drawn into is only as big as the view bounds. They might use up a few extra kilobytes, but that really isn't a big deal; remember that a pixel is 4 bytes.

Until you've measured a performance problem, don't bother with complicated optimizations.


You have choices with UILabel, you could either use an single line or multiline using lineBreakMode , this mode will determine the performance of your UILabel.

There is not direct answer for your questions because it's not visible to us how apple have implemented the UILabel.

If showing the complete text is not in your requirement, you could only pass the 20% of the string and can be considered as optimization.


" premature optimization is the root of all evil" -Donald Knuth

Pass the whole thing, and then if it's slow, then think about this.

  1. Theoretically yes, since UILabel.text stores the text printed on the screen. But, we don't run on machines that have 16k memory anymore. See quote above.

  2. I believe Quartz will do this optimization. In general, drawing that occurs outside of the CGRect of the bounds of the UIView won't be done.


The obvious question is why you would want to pass a string to a UILabel and not even show it all? Either way, it isn't very memory efficient as it will store the full data in memory, but as tc. mentions, for as string of reasonable size length as you are dealing with, it's hardly going to cause too much of a memory issue.

I would recommend just to leave it unless you are experiencing noticable memory issues in your app.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜