ios 自定义消息提示框

本文介绍了一种自定义提示框的实现方式,并提供了完整的代码示例。该提示框可在3秒后自动消失,并能自由设置字体及显示内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义提示框,3秒钟后自动消失。如上图显示效果。

提示框加载代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //将view背景颜色变更为黄色
    self.view.backgroundColor = [UIColor yellowColor];
    
    //在self.view上加载提示框
    [[BIDNoteView sharedInstance] showNoteView:@"显示提示信息" subView:self.view];
}

BIDNoteView.h 代码:

//
//  BIDNoteView.h
//  label上画横线
//
//  Created by eJiupi on 14-7-23.
//  Copyright (c) 2014年 xujinzhong. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BIDNoteView : NSObject

+ (BIDNoteView*)sharedInstance;

- (void)showNoteView:(NSString*)noteText subView:(UIView*)subView;

-(void)setFont:(UIFont*)font;

@end

BIDNoteView.m 实现代码:

//
//  BIDNoteView.m
//  label上画横线
//
//  Created by eJiupi on 14-7-23.
//  Copyright (c) 2014年 xujinzhong. All rights reserved.
//

#import "BIDNoteView.h"


@interface BIDNoteView ()

//@property (strong, nonatomic) UIView *subView;

@property (strong, nonatomic) UIView *noteView;
@property (strong, nonatomic) UILabel *noteLable;

@property (strong, nonatomic) NSTimer *timer;

@end


@implementation BIDNoteView

+ (BIDNoteView*)sharedInstance
{
    static BIDNoteView* instance = nil;
    if (instance == nil)
    {
        instance = [[BIDNoteView alloc] init];
    }
    return instance;
}

- (id)init
{
    self = [super init];
    if (self)
    {
        NSInteger w = 220;
        NSInteger h = 50;
        NSInteger x = ([UIScreen mainScreen].bounds.size.width-w)/2;
        NSInteger y = [UIScreen mainScreen].bounds.size.height-60-h;
       
        self.noteView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        self.noteView.layer.cornerRadius = 5.0;
        self.noteView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        
        self.noteLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, h)];
        self.noteLable.text = @"亲,没有更多商品信息了哦";
        self.noteLable.numberOfLines=2;
        self.noteLable.textColor = [UIColor whiteColor];
        self.noteLable.textAlignment = NSTextAlignmentCenter;
        self.noteLable.backgroundColor = [UIColor clearColor];
        [self.noteView addSubview:self.noteLable];
    }
    return self;
}

-(void)setFont:(UIFont*)font
{
    self.noteLable.font=font;
}

- (void)showNoteView:(NSString*)noteText subView:(UIView*)subView
{
    if (self.timer != nil && [self.timer isValid]) {
        [self.timer invalidate];
        self.timer = nil;
    }
    
    if (noteText != nil && [noteText length] > 0)
        self.noteLable.text = noteText;
    
    [subView addSubview:self.noteView];
    [subView layoutIfNeeded];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];
}

- (void)timerFired:(NSTimer*)timer
{
    [self.timer invalidate];
    //[self setFont:[BIDDeviceFont font_15]];
    [self setFont:[UIFont fontWithName:@"Helvetica" size:15]];
    self.timer = nil;
    [self.noteView removeFromSuperview];
}

@end

 

转载于:https://www.cnblogs.com/xujinzhong/p/8464619.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值