Fade out end of text label strings that don't fit (instead of truncate)

走远了吗. 提交于 2019-12-23 09:01:49

问题


I'm not sure if "fade out" is the correct term to use, as it seems to refer to the animation effect a lot, but what I'd like to achieve can be seen in the address bar of the iPhone Safari app. When a URL is too long to display, the end of the string "faded out" rather than truncated with "...".

I thought this could be easily done by changing the Line Breaks setting from "Truncate Tail" to "Clip" in the XIB file and then using an image with transparency to create the effect. But setting to "Clip" seems to clip at the end of a word, rather than the middle of a word, or the middle of a letter even, as seen in Safari or Chrome for iPhone. This doesn't quite work for me, and it actually gives the impression that the text is complete, when upon closer inspection, the user will notice that the text doesn't make sense.

What's the best way to "fade out" strings that don't fit in text labels? Thanks in advance.


回答1:


Take a look at the Google Toolbox for Mac GTMFadeTruncatingLabel, which is a reusable component that does exactly this.




回答2:


You could also use a combination of the category found here : (https://stackoverflow.com/a/24508153/2654425) and a gradient.

And do:

if([myLabel isTruncated]){

  CAGradientLayer *l = [CAGradientLayer layer];
            l.frame = myLabel.bounds;
            l.colors = @[(id)[UIColor whiteColor].CGColor, (id)[UIColor clearColor].CGColor];
            l.startPoint = CGPointMake(1.f, .1f);
            l.endPoint = CGPointMake(0.95f, 1.0f);
            myLabel.layer.mask = l;



}

This works in iOS 7 & iOS8.



来源:https://stackoverflow.com/questions/12154313/fade-out-end-of-text-label-strings-that-dont-fit-instead-of-truncate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!