简体   繁体   English

如何像 Foursquare 那样创建徽章?

[英]How to create Badges like Foursquare did?

I want to create these blue and red badge labels Foursquare did as shown in the following screenshot.我想创建这些蓝色和红色徽章标签 Foursquare,如下面的屏幕截图所示。

在此处输入图片说明

How to create Badges like Foursquare did?如何像 Foursquare 那样创建徽章?

This might not be the best code in the world but it should work.这可能不是世界上最好的代码,但它应该可以工作。 You must import QuartzCore/QuartzCore.h, and don't forget to add the button as a subview.您必须导入 QuartzCore/QuartzCore.h,并且不要忘记将按钮添加为子视图。

    UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 15)];
    [btn setTitle:@“Hello” forState:UIControlStateNormal];
    [btn sizeToFit];
    CGRect frame = btn.frame;

    // adding horizontal spacing
    frame = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(frame), CGRectGetWidth(frame) + 24, CGRectGetHeight(frame));

    // decreasing vertical spacing
    frame = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(frame), CGRectGetWidth(frame), CGRectGetHeight(frame) - 8);

    btn.frame = frame;
    btn.layer.cornerRadius = CGRectGetHeight(frame)/2;
    btn.clipsToBounds = YES; // not sure if needed

    // if you want a border:
    btn.layer.borderWidth = 1;
    btn.layer.borderColor = [UIColor greenColor].CGColor;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM