1

我似乎NSTextAttachment在使用NSAttributedString.

我有一个字符串,我想淡入和淡出,但似乎附加到字符串的图像不会与文本一起淡出。

我尝试将NSBackgroundColorAttributeNameandNSForegroundColorAttributeName属性都设置为所需的 alpha,但这不起作用。

我也没有看到 alpha 或 opacity 属性NSTextAttachment,所以我看到的唯一选择是附加一个UIImage带有更正 alpha 的新属性,但这种方法将是最后的手段。

我希望那里有人有一种方法来改变阿尔法,而我不必这样做。

Objective-C 或 Swift 代码都适合我。

4

1 回答 1

0

截至目前,这就是我正在做的事情:

import Foundation
import UIKit
import CoreGraphics
extension UIImage
{
    /// copyWithAlpha: Creates a copy of a UIImage with the new alpha
    /// - Parameter alpha: Alpha value to set new image to. Between (0.0 - 1.0)
    /// - Returns: UIImage
    ///```swift
    ///   let copyImage = originalImage.copyWithAlpha(1.0)
    func copyWithAlpha(alpha:CGFloat) -> UIImage
    {
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale);
        drawAtPoint(CGPointZero,blendMode:.Normal, alpha:alpha);
        let alphaImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return alphaImage;
    }
}

这种方法是免费的,任何人都可以使用。但是,我对这种方法并不满意,因为它需要我保留一份原始副本UIImage,并且每次都将 alpha 应用于原始文件,我无法继续将其重新应用于UIImage返回的那个。

于 2016-02-22T05:01:22.660 回答