簡體   English   中英

如何像 Foursquare 那樣創建徽章?

[英]How to create Badges like Foursquare did?

我想創建這些藍色和紅色徽章標簽 Foursquare,如下面的屏幕截圖所示。

在此處輸入圖片說明

如何像 Foursquare 那樣創建徽章?

這可能不是世界上最好的代碼,但它應該可以工作。 您必須導入 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