如何设置iOS7的NSString的背景cornerRadius [英] How to set NSString's background cornerRadius on iOS7

查看:335
本文介绍了如何设置iOS7的NSString的背景cornerRadius的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置iOS7的NSString的背景cornerRadius。
但是,的NSString不必层...

I want to set NSString's background cornerRadius on iOS7. But,NSString don't have layer...

请告诉我,如何设置的NSString的背景cornerRadius上iOS7?

Please tell me, how to set NSString's background cornerRadius on iOS7?

例如
\"http://a5.mzstatic.com/jp/r30/Purple/v4/ba/d3/75/bad3753e-0e54-43a2-6b8a-e56d4966b5bf/screen568x568.jpeg\"

example

推荐答案

您可以使用的UITextView 的子类NSLayoutManager <做到这一点/ code>,即覆盖 -fillBackgroundRectArray:数:forCharacterRange:颜色:
只是一个小样本如何这样的:

You can do this by using an UITextView with a subclass of NSLayoutManager, that override -fillBackgroundRectArray:count:forCharacterRange:color:. Just a little sample how to this :

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];

    // setup text handling
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."];

    // use our subclass of NSLayoutManager
    MyLayoutManager *textLayout = [[MyLayoutManager alloc] init];

    [textStorage addLayoutManager:textLayout];

    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.view.bounds.size];

    [textLayout addTextContainer:textContainer];
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0,20,self.view.bounds.size.width,self.view.bounds.size.height-20)
                                               textContainer:textContainer];
    [self.view addSubview:textView];

    // set some background color to our text
    [textView.textStorage setAttributes:[NSDictionary dictionaryWithObject:[UIColor blueColor] forKey:NSBackgroundColorAttributeName] range:NSMakeRange(22, textView.text.length - 61)];
}
@end

@interface MyLayoutManager : NSLayoutManager
@end

- (void)fillBackgroundRectArray:(const CGRect *)rectArray count:(NSUInteger)rectCount forCharacterRange:(NSRange)charRange color:(UIColor *)color
{
     CGFloat halfLineWidth = 4.; // change this to change corners radius

     CGMutablePathRef path = CGPathCreateMutable();

     if (rectCount == 1
         || (rectCount == 2 && (CGRectGetMaxX(rectArray[1]) < CGRectGetMinX(rectArray[0])))
        )
    {
        // 1 rect or 2 rects without edges in contact

         CGPathAddRect(path, NULL, CGRectInset(rectArray[0], halfLineWidth, halfLineWidth));
         if (rectCount == 2)
            CGPathAddRect(path, NULL, CGRectInset(rectArray[1], halfLineWidth, halfLineWidth));
    }
     else
    {
        // 2 or 3 rects
         NSUInteger lastRect = rectCount - 1;

         CGPathMoveToPoint(path, NULL, CGRectGetMinX(rectArray[0]) + halfLineWidth, CGRectGetMaxY(rectArray[0]) + halfLineWidth);

         CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rectArray[0]) + halfLineWidth, CGRectGetMinY(rectArray[0]) + halfLineWidth);
         CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rectArray[0]) - halfLineWidth, CGRectGetMinY(rectArray[0]) + halfLineWidth);

         CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rectArray[0]) - halfLineWidth, CGRectGetMinY(rectArray[lastRect]) - halfLineWidth);
         CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rectArray[lastRect]) - halfLineWidth, CGRectGetMinY(rectArray[lastRect]) - halfLineWidth);

         CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rectArray[lastRect]) - halfLineWidth, CGRectGetMaxY(rectArray[lastRect]) - halfLineWidth);
         CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rectArray[lastRect]) + halfLineWidth, CGRectGetMaxY(rectArray[lastRect]) - halfLineWidth);

         CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rectArray[lastRect]) + halfLineWidth, CGRectGetMaxY(rectArray[0]) + halfLineWidth);

         CGPathCloseSubpath(path);
    }

    [color set]; // set fill and stroke color

     CGContextRef ctx = UIGraphicsGetCurrentContext();
     CGContextSetLineWidth(ctx, halfLineWidth * 2.);
     CGContextSetLineJoin(ctx, kCGLineJoinRound);

     CGContextAddPath(ctx, path);
     CGPathRelease(path);

     CGContextDrawPath(ctx, kCGPathFillStroke);
}
@end

这篇关于如何设置iOS7的NSString的背景cornerRadius的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆