简体   繁体   中英

How can I set a background image for a UILabel and adjust its alpha without changing the transparency of the text?

How can I create a UILabel with a background image and adjust the transparency (alpha channel) of the background image without changing the transparency of the text?

If there's no easy way to accomplish this within the UILabel object by itself, what about creating a custom UIView object that's composed of a UIImageView and a UILabel , which would allow me to adjust the alpha channel of the image independently of the UILabel 's alpha channel? Is that more complicated than it needs to be?

Thanks so much for your wisdom!

A Google codesearch for UIImage, alpha and CGImageRef doesn't turn anything that does exactly what you need, but lots of related code (see for example this ). The way I would do follows the same pattern, getting a CGImageRef from your image, drawing it with a composition mode with your transparency, and getting back a UIImage from that using [UIImage imageWithCGImage:] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM