objective-c - 如何在iOS7上设置NSString的背景cornerRadius

标签 objective-c background cornerradius

我想在 iOS7 上设置 NSString 的背景 cornerRadius。 但是,NSString 没有图层...

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

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

最佳答案

您可以通过使用带有 NSLayoutManager 子类的 UITextView 来完成此操作,该子类覆盖 -fillBackgroundRectArray:count:forCharacterRange:color:。 只是一个小例子如何做到这一点:

@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

sample image

关于objective-c - 如何在iOS7上设置NSString的背景cornerRadius,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21857408/

相关文章:

iphone - 自动更改到下一个 ViewController

iPhone iOS 如何发送带有 CSS 样式的 HTML 电子邮件?

ios - CFRelease 期间出现 exc_bad_exception

bash - 在后台 BASH 上运行 Matlab

background - 在 :hover element 上设置背景的高度和宽度

ios - 为 UITableViewCell 加载自定义 XIB

qt - 如何设置行的背景颜色?

ios - UICollectionViewCell 不是圆角

ios - 如何为 aspect fit 模式设置圆角的 UIImageView

ios - 如何使用 Xcode 8 将 UIView 设置为圆角半径