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 绘制。

你可能感兴趣的:(NSString)