zoukankan      html  css  js  c++  java
  • 等级显示小控件

    等级控件效果图:

    DAE73EAE 24D9 4400 A97B E7F5F6467B66

    实现方式:
     
    自定义小控件,通过frame来获取它的坐标,然后通过富文本的方式来实现不同文字,由于文字粗体和斜体要同时使用,所以通过富文本实现比较方便;
     
    声明文件:
     
    其中有两个方法,一个是初始化,一个是改变等级需要调用的方法;
     
    #import <UIKit/UIKit.h>

    @interface IDSLevelconView : UIImageView

    @property (nonatomic, strong) UILabel *levelabel;

    - (
    instancetype)initWithIconLevel:(NSInteger)level;

    - (
    void)updateLevel:(NSInteger)level;

    @end
     
     
    实现代码
     

    #import "IDSLevelconView.h"

    @implementation IDSLevelconView

    - (
    instancetype)initWithIconLevel:(NSInteger)level
    {
       
       
    if (self = [superinit]) {
            [
    selfsetupView:level];
        }
       
    returnself;
    }


    - (
    void)setupView:(NSInteger)level
    {
       
    _levelabel = [[UILabelalloc] initWithFrame:CGRectMake(2, 0, 0, 0)];
       
       
       
    NSMutableString * subtitlelabelStr = [NSMutableStringstringWithFormat:@"Lv%ld", level];
       
    NSString *content = subtitlelabelStr;
       
    NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc] initWithString:content];
       
        [attributedString
    setAttributes:@{NSForegroundColorAttributeName:NF_Color_C1,NSFontAttributeName:[UIFontboldSystemFontOfSize:Near_Final_Font_T12]}range:NSMakeRange(0, 2)];
        [attributedString
    setAttributes:@{NSForegroundColorAttributeName:NF_Color_C1,NSFontAttributeName:[UIFontboldSystemFontOfSize:Near_Final_Font_T2]}range:NSMakeRange(2, [content length]-2)];
        [attributedString
    addAttributes:@{NSObliquenessAttributeName:@0.2}range:NSMakeRange(0, [content length])];
        [
    _levelabelsetAttributedText:attributedString];
        [
    _levelabelsizeToFit];
       
       
    if (level<=5) {
           
    self.backgroundColor = NF_Color_C24;
        }
       
    elseif (level<=10) {
           
    self.backgroundColor = NF_Color_C27;
        }
       
    elseif (level<=15) {
           
    self.backgroundColor = NF_Color_C23;
        }
       
    elseif (level<=20) {
           
    self.backgroundColor = NF_Color_C5;
        }
        [
    selfaddSubview:_levelabel];
       
    self.frame = CGRectMake(0, 0, 35, 14);
       
    _levelabel.centerX = self.frame.size.width/2;
       
    _levelabel.centerY = self.frame.size.height/2;
       
    self.layer.cornerRadius = self.frame.size.height/2;
       
    self.layer.masksToBounds = YES;
    }

    - (
    void)updateLevel:(NSInteger)level
    {
       
    NSMutableString * subtitlelabelStr = [NSMutableStringstringWithFormat:@"Lv%ld", level];
       
    NSString *content = subtitlelabelStr;
       
    NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc] initWithString:content];
       
        [attributedString
    setAttributes:@{NSForegroundColorAttributeName:NF_Color_C1,NSFontAttributeName:[UIFontboldSystemFontOfSize:Near_Final_Font_T12]}range:NSMakeRange(0, 2)];
        [attributedString
    setAttributes:@{NSForegroundColorAttributeName:NF_Color_C1,NSFontAttributeName:[UIFontboldSystemFontOfSize:Near_Final_Font_T2]}range:NSMakeRange(2, [content length]-2)];
        [attributedString
    addAttributes:@{NSObliquenessAttributeName:@0.2}range:NSMakeRange(0, [content length])];
        [
    _levelabelsetAttributedText:attributedString];
        [
    _levelabelsizeToFit];
       
       
    if (level<=5) {
           
    self.backgroundColor = NF_Color_C24;
        }
       
    elseif (level<=10) {
           
    self.backgroundColor = NF_Color_C27;
        }
       
    elseif (level<=15) {
           
    self.backgroundColor = NF_Color_C23;
        }
       
    elseif (level<=20) {
           
    self.backgroundColor = NF_Color_C5;
        }
       
    _levelabel.centerX = self.frame.size.width/2;
       
    _levelabel.centerY = self.frame.size.height/2;
    }

    @end
     
  • 相关阅读:
    我的第二个思维导图,用来介绍框架
    如何减少基于DataSet框架的代码输入量(一)
    近日
    关于客户端如何获取服务器时间的方法
    匹配用逗号分隔的数字(防sql注入检查)
    十六进制字符串转整形
    sql获取自增行号
    body不出现滚动条方法
    vs2010 无法调试 无法进入断点 断点无效
    Textarea 高度自适应 根据内容自适应高度
  • 原文地址:https://www.cnblogs.com/firstrate/p/7236207.html
Copyright © 2011-2022 走看看