zoukankan      html  css  js  c++  java
  • 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 绘制。

  • 相关阅读:
    【转】Maven实战(九)---模块聚合和继承
    【转】Maven实战(八)---模块划分
    Oracle ORA-01034,ORA-27101,ORA-00600
    【spring源代码分析】--Bean的解析与注冊
    ArcGIS For Flex报错
    江西省委常委赵智勇被免 无被查字眼 媒体推測窝案
    软考——(4)数据库
    matlab 2014a 改为英文版本号
    POJ 2299 Ultra-QuickSort (求序列的逆序对数)
    工厂方法模式(factory method pattern)
  • 原文地址:https://www.cnblogs.com/hbf369/p/3368318.html
Copyright © 2011-2022 走看看