• ios drawRect NSString 绘制


    - (void)drawRectFor7
    {
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
        
        UIFont *font = [UIFont boldSystemFontOfSize:_fontSize];
        
        NSDictionary *attributes = nil;
        NSDictionary *strokeAttributes = nil;
        if (_useLightText)
        {
            strokeAttributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, _strokeColor, NSStrokeColorAttributeName, @-10.0, NSStrokeWidthAttributeName, nil];
            
            attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, _lightColor, NSForegroundColorAttributeName, nil];
        }
        else
        {
            attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, _normalColor, NSForegroundColorAttributeName, nil];
        }
        
        // draw text
        int i = 0;
        double unitStartX = 0.0;
        for (NSString *str in _strings)
        {
            
            CGSize size = [str sizeWithAttributes:attributes];
            double startX = _segmentLengthInPixels * i - size.width / 2.0 + kScaleSegmentMargin;
            
            ++i;
            
            // draw units string position
            if (_withUnits && i == _strings.count)
            {
                startX = unitStartX;
            }
            else
            {
                unitStartX = _segmentLengthInPixels * (i - 1) + size.width / 2.0 + kScaleSegmentMargin * 2;
            }
            
            if (strokeAttributes != nil)
            {
                [str drawAtPoint:CGPointMake(startX, 0) withAttributes:strokeAttributes];
            }
            
            [str drawAtPoint:CGPointMake(startX, 0) withAttributes:attributes];
        }
        
    #endif
    }
    - (void)drawRectFor6
    {
        // obtain current context
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        // save context state first
        CGContextSaveGState(context);
        
        // set text color in context
        if (_useLightText)
        {
            CGContextSetFillColorWithColor(context, _lightColor.CGColor);
        }
        else
        {
            CGContextSetFillColorWithColor(context, _normalColor.CGColor);
        }
        
        UIFont *font = [UIFont boldSystemFontOfSize:_fontSize];
        
        // draw text
        int i = 0;
        double unitStartX = 0.0;
        for (NSString *str in _strings)
        {
    
            CGSize size = [str sizeWithFont:font];
            double startX = _segmentLengthInPixels * i - size.width / 2.0 + kScaleSegmentMargin;
            
            ++i;
            
            // draw units string position
            if (_withUnits && i == _strings.count)
            {
                startX = unitStartX;
            }
            else
            {
                unitStartX = _segmentLengthInPixels * (i - 1) + size.width / 2.0 + kScaleSegmentMargin * 2;
            }
    
            //draw stroke
            if (_useLightText)
            {
                CGContextSaveGState(context);
                CGContextSetTextDrawingMode(context, kCGTextStroke);
                CGContextSetStrokeColorWithColor(context, _strokeColor.CGColor);
                [str drawAtPoint:CGPointMake(startX, 0) withFont:font];
                CGContextRestoreGState(context);
            }
            
            [str drawAtPoint:CGPointMake(startX, 0) withFont:font];
            
        }
        
        // restore context state
        CGContextRestoreGState(context);
    }

    userLightText模式下绘制白底黑边字符串,普通模式下绘制黑色字。

    ios7 下使用 

    - (void)drawAtPoint:(CGPoint)point withAttributes:(NSDictionary *)attrs  进行绘制。

    需要定义attributes,对样式进行定义。

    ios7 之前使用 

    - (CGSize)drawAtPoint:(CGPoint)point withFont:(UIFont *)font 绘制。

  • 相关阅读:
    国内鲜为人知的“操作系统” Friend OS {Ep.1}
    好久没有发布什么内容了,今天推荐一个网站:Viritual x86
    温馨提示:yueming124.xyz的邮箱已经被我停用。
    Python
    Python -面试题
    码云-拉取远程代码
    mysql
    git 合并代码
    python 字典添加键值对 键相同值被覆盖的问题
    python 字典的减法
  • 原文地址:https://www.cnblogs.com/hbf369/p/3368318.html
Copyright © 2020-2023  润新知