可以滚动的Label标签

    xiaoxiao2021-03-25  115

    虽然现在手机屏幕越来越大,但是手机屏幕毕竟有限,而各种各样的信息越来越缤纷复杂,所以合理利用手机屏幕尤为重要。

    我们经常遇到,在有限的空间里,只能放下有限大小的Label标签,但是需要展示的信息却很多,这样一来,可以滚动的Label标签就可以尽量用最小的布局展示更大的信息量。

    下面就自己实现一个这样的可以滚动的Label标签。

    先创建一个继承于UISrollView的RollLabel类

    #import <UIKit/UIKit.h>

    #define kConstrainedSize CGSizeMake(10000,40)

    @interface RollLabel : UIScrollView

    @end

    搞一个“+”方法,方便外部直接调用,然后将实现方法全写到RollLabel.m文件中

    + (void)rollLabelTitle:(NSString *)title color:(UIColor *)color font:(UIFont *)font superView:(UIView *)superView fram:(CGRect)rect;

    RollLabel.m文件中的全部实现方法如下

    - (id)initWithFrame:(CGRect)frame Withsize:(CGSize)size

    {

        self = [super initWithFrame:frame];

        if (self) {

            self.showsVerticalScrollIndicator   = NO;

            self.showsHorizontalScrollIndicator = NO;//水平滚动条

            self.contentSize = size;//滚动大小

            self.backgroundColor = [UIColor colorWithHue:0.1 saturation:0.6 brightness:1.0 alpha:1.0];

        }

        return self;

    }

    + (void)rollLabelTitle:(NSString *)title color:(UIColor *)color font:(UIFont *)font superView:(UIView *)superView fram:(CGRect)rect {

        NSMutableParagraphStyle *style=[[NSMutableParagraphStyle defaultParagraphStyle]mutableCopy];

        [style setLineBreakMode:NSLineBreakByWordWrapping];

        

        NSDictionary *dis=[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, style, NSParagraphStyleAttributeName, nil];

        //文字大小,设置label的大小和uiscroll的大小

        CGSize size=[title boundingRectWithSize:kConstrainedSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dis context:nil].size;

        

        CGRect frame = CGRectMake(0, 0, size.width, rect.size.height);

        RollLabel *roll = [[RollLabel alloc]initWithFrame:rect Withsize:size];

        UILabel *label = [[UILabel alloc]initWithFrame:frame];

        label.text = title;

        label.font = font;

        label.textColor = color;

        [roll addSubview:label];

      

        [superView addSubview:roll];

     

    }

    导入RollLabel.h文件,调用“+”方法。

    #import "RollLabel.h"

    [RollLabel rollLabelTitle:@"有时候,我们错过的不是时间,是感觉" color:[UIColor blackColor] font:[UIFont systemFontOfSize:30] superView:self.view fram:CGRectMake(60, 150, 200, 40)];

    OK,搞定,快来试试吧。

    转载请注明原文地址: https://ju.6miu.com/read-22143.html

    最新回复(0)