美文网首页
前两天被问到的问题,在此记录一下~

前两天被问到的问题,在此记录一下~

作者: 永远都能 | 来源:发表于2016-03-27 00:19 被阅读566次

    问题:
    1.如何销毁单例?
    我回答了:先清空里面的数据再把单例=nil。

    我呆逼了,不知道当时的脑袋里怎么不断的浮现以前是赋值过而且也没有问题的,后来实验了真的会报错,后来去查了,一般单例我们是不用管理和销毁的,要是需要手动销毁那也不叫单例了。

    2.问了一个要实现一个动态加那个(自营)的文字.如效果图


    test.png

    我回答了:可以使用NSAttributedString/NSTextAttachment/YYText/结合两个控件都可以实现。后来也说不出一个所以然来。

    事后回去验证了一下:
    (1)NSAttributedString/NSTextAttachment 这两个我都做不到那个效果,也许是我对他们没有深入的了解。
    (2)YYText我没去试,不过我相信它绝对可以~~~
    (3)我用了两个控件实现了,如下:

    #import "ViewController.h"
    
    #import <CoreText/CoreText.h>
    
    @interface ViewController ()
    
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIFont *font = [UIFont systemFontOfSize:12];
        
        NSString *test= @"好东西好东西大特卖啊大特大特卖快来吧来啊水大特卖快来吧啊好东西好东西大特卖啊大特大特卖快来吧啊水是大特";
        NSString *ziying = @"自营";
        CGSize size2 = [test boundingRectWithSize:CGSizeMake(self.view.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
        
        //创建文本Label
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width , size2.height)];
        label.text = test;
        label.font = font;
        label.numberOfLines = 0;
        label.backgroundColor = [UIColor grayColor];
        [self.view addSubview:label];
        
        //获取文字数组
        NSArray *array = [self.class getSeparatedLinesFromLabel:label];
        //最后一行文字的Size
        CGSize lastSize = [[array lastObject] boundingRectWithSize:CGSizeMake(self.view.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
        //自营size
        CGSize ziyingSize = [ziying boundingRectWithSize:CGSizeMake(self.view.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
        
        //创建 自营 标签
        UILabel * ziyingLabel = [[UILabel alloc]init];
        ziyingLabel.text = ziying;
        ziyingLabel.textColor = [UIColor whiteColor];
        ziyingLabel.font = font;
        ziyingLabel.layer.backgroundColor = [UIColor blueColor].CGColor;
        ziyingLabel.layer.cornerRadius = 3;
        [self.view addSubview:ziyingLabel];
        
        //计算frame
        if(lastSize.width + 5 + ziyingSize.width >= self.view.frame.size.width){
            ziyingLabel.frame = CGRectMake(CGRectGetMinX(label.frame), CGRectGetMaxY(label.frame), ziyingSize.width, ziyingSize.height);
        }else{
             ziyingLabel.frame = CGRectMake(lastSize.width + 5, CGRectGetMaxY(label.frame)- lastSize.height, ziyingSize.width, ziyingSize.height);
        }
        [self.view addSubview:ziyingLabel];
     
        
    }
    /**
     *  切割每一行文字
     *
     *  @param label 需要切换的控件
     *
     *  @return 每一行文字数组
     */
    + (NSArray *)getSeparatedLinesFromLabel:(UILabel *)label
    {
        NSString *text = [label text];
        UIFont   *font = [label font];
        CGRect    rect = [label frame];
        
        CTFontRef myFont = CTFontCreateWithName((__bridge CFStringRef)([font fontName]), [font pointSize], NULL);
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
        [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)myFont range:NSMakeRange(0, attStr.length)];
        
        CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attStr);
        
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000));
        
        CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
        
        NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frame);
        NSMutableArray *linesArray = [[NSMutableArray alloc]init];
        
        for (id line in lines)
        {
            CTLineRef lineRef = (__bridge CTLineRef )line;
            CFRange lineRange = CTLineGetStringRange(lineRef);
            NSRange range = NSMakeRange(lineRange.location, lineRange.length);
            
            NSString *lineString = [text substringWithRange:range];
            [linesArray addObject:lineString];
        }
        return (NSArray *)linesArray;
    }
    @end
    

    没有做过多的封装,只是单纯的把功能给实现了。方法就是拿到最后一行文字的高度进行判断一下就好了。

    相关文章

      网友评论

          本文标题:前两天被问到的问题,在此记录一下~

          本文链接:https://www.haomeiwen.com/subject/gvndlttx.html