• 星级评价 实现


    设置评价星星

      字体的大小适合布局的大小

    [Lable名  sizeToFit];

    .h

    typedef enum kRatingViewStyle

    {

        kSmallStyle = 0,

        kNormalStyle = 1

     

    }kRatingViewStyle;

     

    @interface RatingView : UIView

    {

     @private

        UIView *_baseView;//透明的承载yellowStar的图层

        NSMutableArray *_yellowStarArray;

        NSMutableArray *_grayStarArray;

        UILabel *_ratingLable;

        CGFloat  _ratingScore;

    }

     

    @property(nonatomic,assign)kRatingViewStyle style;

     

    @property(nonatomic,assign)CGFloat ratingScore;

    .m

    #define  kNormalWidth 35

    #define  kNormalHeight 33

     

    #define  kSmallWidth  15

    #define  kSmallHeight  14

     

    #define  kBaseViewWidth 10

     

    #define  kNormalFontSize 25

     

    #define  kSmallFontSize  12

    初始化的时候调用:

    - (id)initWithFrame:(CGRect)frame

    {

        self = [super initWithFrame:frame];

        if (self) {

            [self initGrayStar];

            [self initYellowStar];

            [self ratingLable];

         

        }

        return self;

     

    }

    几星的布局是根据:

    1.设置为灰色星星的层

     

    -(void)initGrayStar

    {

        _grayStarArray = [[NSMutableArray alloc] initWithCapacity:5];

        for (int index = 0; index < 5; index++) {

            UIImageView * crayStar = [[UIImageView alloc] initWithFrame:CGRectZero];

            crayStar.image = [UIImage imageNamed:@"gray"];

            [self addSubview: crayStar];

            [crayStar release];

            

            [_grayStarArray addObject:crayStar];

        }

     

     

    }//灰色星星的初始化

    2.设置黄色星星的层,并设置一个透明层,用来放置黄色的星星,然后用clipsToBounds UIView的属性 这会把多余的截掉

     

    -(void)initYellowStar

    {

        _baseView = [[UIView alloc] initWithFrame:CGRectZero];

        _baseView.backgroundColor = [UIColor clearColor];

        _baseView.clipsToBounds = YES;

        [self addSubview:_baseView];

       

        _yellowStarArray = [[NSMutableArray alloc] initWithCapacity:5];

        for (int index = 0; index < 5; index++) {

            UIImageView *yellowStar = [[UIImageView alloc] initWithFrame:CGRectZero];

            yellowStar.image = [UIImage imageNamed:@"yellow"];

            [_baseView addSubview: yellowStar];

            [yellowStar release];

            

            [_yellowStarArray addObject:yellowStar];

        }

     

    }//黄色星星的初始化和透明层的初始化(_baseView 与 yellowStar)

    3.设置RatingLable 的比率框

    -(void)ratingLable

    {

        _ratingLable = [[UILabel alloc] initWithFrame:CGRectZero];

        _ratingLable.backgroundColor = [UIColor clearColor];

        _ratingLable.textColor  = [UIColor purpleColor];

        [self addSubview:_ratingLable];

        

     

    }//-_atingLable评分框的初始化

    4.在设置全局的RatingScore,用来接收和设置Rating 的样式

    -(void)setRatingScore:(CGFloat)ratingScore

    {

        _ratingScore = ratingScore;

        

        _ratingLable.text = [NSString stringWithFormat:@"%0.1f",_ratingScore];

        

     

    }//设置评分的大小,赋值给_ratingLable

     

    5.layoutSubviews 来布局

    -(void)layoutSubviews

    {

        [super layoutSubviews];

        int width = 0;

        for (int index = 0; index < 5; index++) {

            UIView *yellowStar = _yellowStarArray[index];

            UIView *grayStar   = _grayStarArray[index];

            if (self.style == kSmallStyle) {

                yellowStar.frame = CGRectMake(0+width , 0 , kSmallWidth, kSmallHeight);

                grayStar.frame   = CGRectMake(0+width, 0, kSmallWidth, kSmallHeight);

                

                width +=kSmallWidth;

                

            }else{

                yellowStar.frame = CGRectMake(0+width , 0 , kNormalWidth, kNormalHeight);

                grayStar.frame   = CGRectMake(0+width, 0, kNormalWidth, kNormalHeight);

                

                width +=kNormalWidth;

            }

            }//将两种星星排成一行

        float baseViewWidth = 0;

        baseViewWidth = self.ratingScore / kBaseViewWidth *width;

        

        float height = 0;

        if (self.style == kSmallStyle) {

            _baseView.frame = CGRectMake(0, 0, baseViewWidth, kSmallHeight);

            _ratingLable.font = [UIFont boldSystemFontOfSize:kSmallFontSize];

            height = kSmallHeight;

            

        }else

        {

            _baseView.frame = CGRectMake(0, 0, baseViewWidth, kNormalHeight);

            _ratingLable.font = [UIFont boldSystemFontOfSize:kNormalFontSize];

            height = kNormalHeight;

        }//设置——baseView的两种不同样式的大小,——ratingLable的字体大小

        

        //设置ratingLable 的大小和位置

        _ratingLable.frame = CGRectMake(width, 0, 0, 0);

        [_ratingLable sizeToFit];

      

        //整体的大小和位置

        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, width+_ratingLable.frame.size.width, height);

    }//星星的排版

                     

  • 相关阅读:
    CentOS安装python setuptools and pip
    Memcached集群:Magent缓存代理使用
    PHP上传类 图片上传 upload class实现image crop resize 缩略图
    CentOS全自动一键安装PHP,MySQL,phpmyadmin与Nginx
    【转】浅析linux内存模型
    【转】深入浅出异步I/O模型
    【转】客户/服务器程序设计范式
    【转】如何保证睡眠的情况下把各种事情做好
    【转】非教育网中IPv4网络访问IPv6资源
    busybox介绍
  • 原文地址:https://www.cnblogs.com/meixian/p/5371135.html
Copyright © 2020-2023  润新知