4

In my app one requirement is to set background of label to some image. I have done it by setting image at the back of UIlabel by this code

.H file

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    UIImageView *lblBGIV;
} 
@end

.M file

- (void)viewDidLoad
{
    [super viewDidLoad];

    lblBGIV = [[UIImageView alloc] init];

    UIImage *lblBGImg = [[UIImage imageNamed:@"textlblbg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 8, 0, 8)];

    lblBGIV.image = lblBGImg;
}

- (void)updateLabelMethod:(NSTimer *)theTimer {

            lblBGIV.frame = CGRectMake(label.frame.origin.x-9, label.frame.origin.y-5, label.frame.size.width+18, 26);

            [self.myScrollView addSubview:lblBGIV];
            [self.myScrollView sendSubviewToBack:lblBGIV];
}
4

3 回答 3

8

您必须创建不同的代码来为不同的 iOS 设置图像。使用此代码,

UIImage *lblBGImg;
    float currentVersion = 6.0;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= currentVersion)
    {
        //device have iOS 6 or above
        lblBGImg = [[UIImage imageNamed:@"textlblbg41.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 15)  resizingMode:UIImageResizingModeStretch];
    }else{
        //device have iOS 5.1 or belove
        lblBGImg = [[UIImage imageNamed: @"textlblbg41.png"] stretchableImageWithLeftCapWidth:15.0 topCapHeight:15.0];
    }
    lblBGIV.image = lblBGImg;

这将对两个 iOS 都有效。

于 2013-07-25T06:28:46.927 回答
5

改为使用resizableImageWithCapInsets:resizingMode:

UIImage *lblBGImg = [[UIImage imageNamed:@"textlblbg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 8, 0, 8) resizingMode:UIImageResizingModeStretch];
于 2013-07-24T15:50:01.923 回答
0

迅速:

UIImage(named: "videoSliderFiller")?.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 0, 0), resizingMode: .stretch)
于 2019-02-24T12:13:35.210 回答